http.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. memcpy(&_body[0], ptr, _body->size());
  79. return _body->size();
  80. }
  81. static void init(CURL *curl, const std::string& url)
  82. {
  83. (void)http_get_result_callback;
  84. (void)curl;
  85. (void)url;
  86. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  87. if (HAVE_LIBCURL)
  88. {
  89. assert(curl);
  90. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  91. curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT);
  92. }
  93. #endif
  94. }
  95. HTTP::HTTP(const std::string& url_arg) :
  96. _url(url_arg),
  97. _response(0)
  98. {
  99. initialize_curl();
  100. }
  101. #pragma GCC diagnostic push
  102. #pragma GCC diagnostic ignored "-Wunreachable-code"
  103. bool GET::execute()
  104. {
  105. (void)init;
  106. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  107. if (HAVE_LIBCURL)
  108. {
  109. CURL *curl= curl_easy_init();
  110. init(curl, url());
  111. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  112. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body);
  113. CURLcode retref= curl_easy_perform(curl);
  114. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  115. curl_easy_cleanup(curl);
  116. return bool(retref == CURLE_OK);
  117. }
  118. #endif
  119. return false;
  120. }
  121. #pragma GCC diagnostic pop
  122. #pragma GCC diagnostic push
  123. #pragma GCC diagnostic ignored "-Wunreachable-code"
  124. bool POST::execute()
  125. {
  126. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  127. if (HAVE_LIBCURL)
  128. {
  129. CURL *curl= curl_easy_init();;
  130. init(curl, url());
  131. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, _body.size());
  132. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *)&_body[0]);
  133. CURLcode retref= curl_easy_perform(curl);
  134. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  135. curl_easy_cleanup(curl);
  136. return bool(retref == CURLE_OK);
  137. }
  138. #endif
  139. return false;
  140. }
  141. #pragma GCC diagnostic pop
  142. #pragma GCC diagnostic push
  143. #pragma GCC diagnostic ignored "-Wunreachable-code"
  144. bool TRACE::execute()
  145. {
  146. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  147. if (HAVE_LIBCURL)
  148. {
  149. CURL *curl= curl_easy_init();;
  150. init(curl, url());
  151. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
  152. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  153. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body[0]);
  154. CURLcode retref= curl_easy_perform(curl);
  155. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  156. curl_easy_cleanup(curl);
  157. return retref == CURLE_OK;
  158. }
  159. #endif
  160. return false;
  161. }
  162. #pragma GCC diagnostic pop
  163. #pragma GCC diagnostic push
  164. #pragma GCC diagnostic ignored "-Wunreachable-code"
  165. bool HEAD::execute()
  166. {
  167. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  168. if (HAVE_LIBCURL)
  169. {
  170. CURL *curl= curl_easy_init();
  171. init(curl, url());
  172. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
  173. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  174. CURLcode retref= curl_easy_perform(curl);
  175. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  176. curl_easy_cleanup(curl);
  177. return retref == CURLE_OK;
  178. }
  179. #endif
  180. return false;
  181. }
  182. #pragma GCC diagnostic pop
  183. } // namespace http
  184. } // namespace libtest