http.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * libtest
  4. *
  5. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include <config.h>
  22. #include <libtest/common.h>
  23. #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
  24. #include <curl/curl.h>
  25. #else
  26. class CURL;
  27. #endif
  28. static void cleanup_curl(void)
  29. {
  30. #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
  31. curl_global_cleanup();
  32. #endif
  33. }
  34. static void initialize_curl_startup()
  35. {
  36. #if defined(HAVE_CURL_CURL_H) && HAVE_CURL_CURL_H
  37. if (curl_global_init(CURL_GLOBAL_ALL))
  38. {
  39. fatal_message("curl_global_init(CURL_GLOBAL_ALL) failed");
  40. }
  41. #endif
  42. if (atexit(cleanup_curl))
  43. {
  44. fatal_message("atexit() failed");
  45. }
  46. }
  47. static pthread_once_t start_key_once= PTHREAD_ONCE_INIT;
  48. void initialize_curl(void)
  49. {
  50. int ret;
  51. if (pthread_once(&start_key_once, initialize_curl_startup) != 0)
  52. {
  53. fatal_message(strerror(ret));
  54. }
  55. }
  56. namespace libtest {
  57. namespace http {
  58. #define YATL_USERAGENT "YATL/1.0"
  59. extern "C" size_t
  60. http_get_result_callback(void *ptr, size_t size, size_t nmemb, void *data)
  61. {
  62. size_t body_size= size * nmemb;
  63. vchar_t *_body= (vchar_t*)data;
  64. _body->resize(size * nmemb);
  65. memcpy(&_body[0], ptr, _body->size());
  66. return _body->size();
  67. }
  68. static void init(CURL *curl, const std::string& url)
  69. {
  70. if (HAVE_LIBCURL)
  71. {
  72. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  73. assert(curl);
  74. curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
  75. curl_easy_setopt(curl, CURLOPT_USERAGENT, YATL_USERAGENT);
  76. #endif
  77. }
  78. }
  79. HTTP::HTTP(const std::string& url_arg) :
  80. _url(url_arg),
  81. _response(0)
  82. {
  83. initialize_curl();
  84. }
  85. bool GET::execute()
  86. {
  87. if (HAVE_LIBCURL)
  88. {
  89. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  90. CURL *curl= curl_easy_init();
  91. init(curl, url());
  92. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  93. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body);
  94. CURLcode retref= curl_easy_perform(curl);
  95. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  96. curl_easy_cleanup(curl);
  97. return retref == CURLE_OK;
  98. #endif
  99. }
  100. return false;
  101. }
  102. bool POST::execute()
  103. {
  104. if (HAVE_LIBCURL)
  105. {
  106. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  107. CURL *curl= curl_easy_init();;
  108. init(curl, url());
  109. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, _body.size());
  110. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (void *)&_body[0]);
  111. CURLcode retref= curl_easy_perform(curl);
  112. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  113. curl_easy_cleanup(curl);
  114. #endif
  115. }
  116. return false;
  117. }
  118. bool TRACE::execute()
  119. {
  120. if (HAVE_LIBCURL)
  121. {
  122. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  123. CURL *curl= curl_easy_init();;
  124. init(curl, url());
  125. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "TRACE");
  126. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  127. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&_body[0]);
  128. CURLcode retref= curl_easy_perform(curl);
  129. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  130. curl_easy_cleanup(curl);
  131. return retref == CURLE_OK;
  132. #endif
  133. }
  134. return false;
  135. }
  136. bool HEAD::execute()
  137. {
  138. if (HAVE_LIBCURL)
  139. {
  140. #if defined(HAVE_LIBCURL) && HAVE_LIBCURL
  141. CURL *curl= curl_easy_init();;
  142. init(curl, url());
  143. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "HEAD");
  144. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, http_get_result_callback);
  145. CURLcode retref= curl_easy_perform(curl);
  146. curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, _response);
  147. curl_easy_cleanup(curl);
  148. return retref == CURLE_OK;
  149. #endif
  150. }
  151. return false;
  152. }
  153. } // namespace http
  154. } // namespace libtest