http.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include "libtest/yatlcon.h"
  37. #include <libtest/common.h>
  38. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  39. #include <curl/curl.h>
  40. #else
  41. class CURL;
  42. #endif
  43. static void cleanup_curl(void)
  44. {
  45. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  46. curl_global_cleanup();
  47. #endif
  48. }
  49. static void initialize_curl_startup()
  50. {
  51. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  52. if (curl_global_init(CURL_GLOBAL_ALL))
  53. {
  54. FATAL("curl_global_init(CURL_GLOBAL_ALL) failed");
  55. }
  56. #endif
  57. if (atexit(cleanup_curl))
  58. {
  59. FATAL("atexit() failed");
  60. }
  61. }
  62. static pthread_once_t start_key_once= PTHREAD_ONCE_INIT;
  63. static void initialize_curl(void)
  64. {
  65. int ret;
  66. if ((ret= pthread_once(&start_key_once, initialize_curl_startup)) != 0)
  67. {
  68. FATAL(strerror(ret));
  69. }
  70. }
  71. namespace libtest {
  72. namespace http {
  73. #define YATL_USERAGENT "YATL/1.0"
  74. static size_t http_get_result_callback(void *ptr, size_t size, size_t nmemb, void *data)
  75. {
  76. vchar_t *_body= (vchar_t*)data;
  77. _body->resize(size * nmemb);
  78. if (_body->size())
  79. {
  80. memcpy(static_cast<void*>(&_body[0]), ptr, _body->size());
  81. }
  82. return _body->size();
  83. }
  84. static void init(CURL *curl, const std::string& url)
  85. {
  86. (void)http_get_result_callback;
  87. (void)curl;
  88. (void)url;
  89. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  90. if (HAVE_LIBCURL)
  91. {
  92. assert(curl);
  93. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  94. curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT);
  95. }
  96. #endif
  97. }
  98. HTTP::HTTP(const std::string& url_arg) :
  99. _url(url_arg),
  100. _response(0)
  101. {
  102. initialize_curl();
  103. }
  104. #pragma GCC diagnostic push
  105. #pragma GCC diagnostic ignored "-Wunreachable-code"
  106. bool GET::execute()
  107. {
  108. (void)init;
  109. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  110. if (HAVE_LIBCURL)
  111. {
  112. CURL *curl= curl_easy_init();
  113. init(curl, url());
  114. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  115. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body);
  116. CURLcode retref= curl_easy_perform(curl);
  117. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  118. curl_easy_cleanup(curl);
  119. return bool(retref == CURLE_OK);
  120. }
  121. #endif
  122. return false;
  123. }
  124. #pragma GCC diagnostic pop
  125. #pragma GCC diagnostic push
  126. #pragma GCC diagnostic ignored "-Wunreachable-code"
  127. bool POST::execute()
  128. {
  129. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  130. if (HAVE_LIBCURL && _body.size())
  131. {
  132. CURL *curl= curl_easy_init();;
  133. init(curl, url());
  134. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, _body.size());
  135. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *)&_body[0]);
  136. CURLcode retref= curl_easy_perform(curl);
  137. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  138. curl_easy_cleanup(curl);
  139. return bool(retref == CURLE_OK);
  140. }
  141. #endif
  142. return false;
  143. }
  144. #pragma GCC diagnostic pop
  145. #pragma GCC diagnostic push
  146. #pragma GCC diagnostic ignored "-Wunreachable-code"
  147. bool TRACE::execute()
  148. {
  149. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  150. if (HAVE_LIBCURL && _body.size())
  151. {
  152. CURL *curl= curl_easy_init();;
  153. init(curl, url());
  154. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
  155. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  156. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body[0]);
  157. CURLcode retref= curl_easy_perform(curl);
  158. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  159. curl_easy_cleanup(curl);
  160. return retref == CURLE_OK;
  161. }
  162. #endif
  163. return false;
  164. }
  165. #pragma GCC diagnostic pop
  166. #pragma GCC diagnostic push
  167. #pragma GCC diagnostic ignored "-Wunreachable-code"
  168. bool HEAD::execute()
  169. {
  170. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  171. if (HAVE_LIBCURL)
  172. {
  173. CURL *curl= curl_easy_init();
  174. init(curl, url());
  175. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
  176. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  177. CURLcode retref= curl_easy_perform(curl);
  178. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  179. curl_easy_cleanup(curl);
  180. return retref == CURLE_OK;
  181. }
  182. #endif
  183. return false;
  184. }
  185. #pragma GCC diagnostic pop
  186. } // namespace http
  187. } // namespace libtest