CurlHandle.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) <2002-2009> <Jean-Philippe Barrette-LaPierre>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (curlpp), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef CURLPP_CURL_HANDLE_HPP
  24. #define CURLPP_CURL_HANDLE_HPP
  25. #include "../Exception.hpp"
  26. #include "../Types.hpp"
  27. #include <curl/curl.h>
  28. #include <memory>
  29. namespace curlpp
  30. {
  31. namespace internal
  32. {
  33. /**
  34. * Wrapper for CURL * handle.
  35. */
  36. class CurlHandle
  37. {
  38. public:
  39. CurlHandle();
  40. CurlHandle(CURL * handle);
  41. std::unique_ptr<CurlHandle> clone() const;
  42. /**
  43. * Calls curl_easy_perform on the handle and throws exceptions on errors.
  44. */
  45. void perform();
  46. /**
  47. * Simply calls curl_easy_reset on the handle.
  48. */
  49. void reset();
  50. virtual ~CurlHandle();
  51. /**
  52. * Calls curl_easy_setopt on the option.
  53. */
  54. template<typename OptionType>
  55. void option(CURLoption option, OptionType value);
  56. /**
  57. * Calls curl_easy_setopt on the option.
  58. */
  59. template<typename OptionType, CURLoption optionType>
  60. void option(OptionType value);
  61. /**
  62. * This function will return the CURL * handle.
  63. *
  64. * DO NOT use this, unless you REALLY know what you
  65. * are doing.
  66. */
  67. CURL * getHandle() const;
  68. /**
  69. * Request internal information from the curl session with this function.
  70. *
  71. * The third argument MUST be a pointer to a long, a pointer to a char *,
  72. * a pointer to a struct curl_slist * or a pointer to a double.
  73. */
  74. template<typename T>
  75. void getInfo(CURLINFO info, T & value) const;
  76. template<typename FunctorType>
  77. typename FunctorType::ResultType execute(FunctorType functor, typename FunctorType::ParamList params);
  78. size_t executeWriteFunctor(char * buffer, size_t size, size_t nitems);
  79. void setWriteFunctor(curlpp::types::WriteFunctionFunctor functor)
  80. {
  81. mWriteFunctor = functor;
  82. }
  83. size_t executeHeaderFunctor(char * buffer, size_t size, size_t nitems);
  84. void setHeaderFunctor(curlpp::types::WriteFunctionFunctor functor)
  85. {
  86. mHeaderFunctor = functor;
  87. }
  88. size_t executeReadFunctor(char * buffer, size_t size, size_t nitems);
  89. void setReadFunctor(curlpp::types::ReadFunctionFunctor functor)
  90. {
  91. mReadFunctor = functor;
  92. }
  93. int executeProgressFunctor(double dltotal,
  94. double dlnow,
  95. double ultotal,
  96. double ulnow);
  97. void setProgressFunctor(curlpp::types::ProgressFunctionFunctor functor)
  98. {
  99. mProgressFunctor = functor;
  100. }
  101. int executeDebugFunctor(curl_infotype, char *, size_t);
  102. void setDebugFunctor(curlpp::types::DebugFunctionFunctor functor)
  103. {
  104. mDebugFunctor = functor;
  105. }
  106. CURLcode executeSslCtxFunctor(void * ssl_ctx);
  107. void setSslCtxFunctor(curlpp::types::SslCtxFunctionFunctor functor)
  108. {
  109. mSslFunctor = functor;
  110. }
  111. void setException(curlpp::CallbackExceptionBase * e);
  112. void throwException();
  113. private:
  114. CurlHandle(const CurlHandle & other);
  115. CurlHandle & operator=(const CurlHandle & other);
  116. /**
  117. * Provides libcURL a space to store error messages.
  118. *
  119. * Pass a char * to a buffer that the libcURL may store
  120. * human readable error messages in. This may be more
  121. * helpful than just the return code from the library.
  122. * The buffer must be at least CURL_ERROR_SIZE big.
  123. * Note: if the library does not return an error, the
  124. * buffer may not have been touched. Do not rely on the
  125. * contents in those cases.
  126. */
  127. void errorBuffer(char* buffer);
  128. private:
  129. CURL * mCurl;
  130. char mErrorBuffer[CURL_ERROR_SIZE + 1];
  131. curlpp::types::WriteFunctionFunctor mWriteFunctor;
  132. curlpp::types::WriteFunctionFunctor mHeaderFunctor;
  133. curlpp::types::ReadFunctionFunctor mReadFunctor;
  134. curlpp::types::ProgressFunctionFunctor mProgressFunctor;
  135. curlpp::types::DebugFunctionFunctor mDebugFunctor;
  136. curlpp::types::SslCtxFunctionFunctor mSslFunctor;
  137. curlpp::CallbackExceptionBase * mException;
  138. };
  139. } // namespace internal
  140. } // namespace curlpp
  141. namespace cURLpp = curlpp;
  142. #include "CurlHandle.inl"
  143. #endif // #ifndef CURLPP_CURL_HANDLE_HPP