test_empty_response.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2013 Christian Grothoff
  4. libmicrohttpd is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. libmicrohttpd is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with libmicrohttpd; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. /**
  18. * @file test_empty_response.c
  19. * @brief Testcase for libmicrohttpd HTTPS GET operations with emtpy reply
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "microhttpd.h"
  24. #include <limits.h>
  25. #include <sys/stat.h>
  26. #include <curl/curl.h>
  27. #ifdef MHD_HTTPS_REQUIRE_GRYPT
  28. #include <gcrypt.h>
  29. #endif /* MHD_HTTPS_REQUIRE_GRYPT */
  30. #include "tls_test_common.h"
  31. extern const char srv_key_pem[];
  32. extern const char srv_self_signed_cert_pem[];
  33. extern const char srv_signed_cert_pem[];
  34. extern const char srv_signed_key_pem[];
  35. static int oneone;
  36. static int
  37. ahc_echo (void *cls,
  38. struct MHD_Connection *connection,
  39. const char *url,
  40. const char *method,
  41. const char *version,
  42. const char *upload_data, size_t *upload_data_size,
  43. void **unused)
  44. {
  45. struct MHD_Response *response;
  46. int ret;
  47. response = MHD_create_response_from_buffer (0, NULL,
  48. MHD_RESPMEM_PERSISTENT);
  49. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  50. MHD_destroy_response (response);
  51. return ret;
  52. }
  53. static int
  54. testInternalSelectGet ()
  55. {
  56. struct MHD_Daemon *d;
  57. CURL *c;
  58. char buf[2048];
  59. struct CBC cbc;
  60. CURLM *multi;
  61. CURLMcode mret;
  62. fd_set rs;
  63. fd_set ws;
  64. fd_set es;
  65. int maxposixs; /* Max socket number unused on W32 */
  66. int running;
  67. struct CURLMsg *msg;
  68. time_t start;
  69. struct timeval tv;
  70. int port;
  71. if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
  72. port = 0;
  73. else
  74. port = 3000;
  75. multi = NULL;
  76. cbc.buf = buf;
  77. cbc.size = 2048;
  78. cbc.pos = 0;
  79. d = MHD_start_daemon (MHD_USE_ERROR_LOG | MHD_USE_TLS | MHD_USE_INTERNAL_POLLING_THREAD,
  80. port, NULL, NULL, &ahc_echo, "GET",
  81. MHD_OPTION_HTTPS_MEM_KEY, srv_key_pem,
  82. MHD_OPTION_HTTPS_MEM_CERT, srv_self_signed_cert_pem,
  83. MHD_OPTION_END);
  84. if (d == NULL)
  85. return 256;
  86. if (0 == port)
  87. {
  88. const union MHD_DaemonInfo *dinfo;
  89. dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  90. if (NULL == dinfo || 0 == dinfo->port)
  91. { MHD_stop_daemon (d); return 32; }
  92. port = (int)dinfo->port;
  93. }
  94. char *aes256_sha = "AES256-SHA";
  95. if (curl_uses_nss_ssl() == 0)
  96. {
  97. aes256_sha = "rsa_aes_256_sha";
  98. }
  99. c = curl_easy_init ();
  100. curl_easy_setopt (c, CURLOPT_URL, "https://127.0.0.1/hello_world");
  101. curl_easy_setopt (c, CURLOPT_PORT, (long)port);
  102. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
  103. curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
  104. /* TLS options */
  105. curl_easy_setopt (c, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
  106. curl_easy_setopt (c, CURLOPT_SSL_CIPHER_LIST, aes256_sha);
  107. curl_easy_setopt (c, CURLOPT_SSL_VERIFYPEER, 0);
  108. curl_easy_setopt (c, CURLOPT_SSL_VERIFYHOST, 0);
  109. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1);
  110. if (oneone)
  111. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  112. else
  113. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  114. curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
  115. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
  116. /* NOTE: use of CONNECTTIMEOUT without also
  117. setting NOSIGNAL results in really weird
  118. crashes on my system! */
  119. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1);
  120. multi = curl_multi_init ();
  121. if (multi == NULL)
  122. {
  123. curl_easy_cleanup (c);
  124. MHD_stop_daemon (d);
  125. return 512;
  126. }
  127. mret = curl_multi_add_handle (multi, c);
  128. if (mret != CURLM_OK)
  129. {
  130. curl_multi_cleanup (multi);
  131. curl_easy_cleanup (c);
  132. MHD_stop_daemon (d);
  133. return 1024;
  134. }
  135. start = time (NULL);
  136. while ((time (NULL) - start < 5) && (multi != NULL))
  137. {
  138. maxposixs = -1;
  139. FD_ZERO (&rs);
  140. FD_ZERO (&ws);
  141. FD_ZERO (&es);
  142. mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
  143. if (mret != CURLM_OK)
  144. {
  145. curl_multi_remove_handle (multi, c);
  146. curl_multi_cleanup (multi);
  147. curl_easy_cleanup (c);
  148. MHD_stop_daemon (d);
  149. return 2048;
  150. }
  151. tv.tv_sec = 0;
  152. tv.tv_usec = 1000;
  153. if (-1 != maxposixs)
  154. {
  155. if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
  156. {
  157. #ifdef MHD_POSIX_SOCKETS
  158. if (EINTR != errno)
  159. abort ();
  160. #else
  161. if (WSAEINVAL != WSAGetLastError() || 0 != rs.fd_count || 0 != ws.fd_count || 0 != es.fd_count)
  162. abort ();
  163. Sleep (1000);
  164. #endif
  165. }
  166. }
  167. else
  168. sleep (1000);
  169. curl_multi_perform (multi, &running);
  170. if (running == 0)
  171. {
  172. msg = curl_multi_info_read (multi, &running);
  173. if (msg == NULL)
  174. break;
  175. if (msg->msg == CURLMSG_DONE)
  176. {
  177. if (msg->data.result != CURLE_OK)
  178. printf ("%s failed at %s:%d: `%s'\n",
  179. "curl_multi_perform",
  180. __FILE__,
  181. __LINE__, curl_easy_strerror (msg->data.result));
  182. curl_multi_remove_handle (multi, c);
  183. curl_multi_cleanup (multi);
  184. curl_easy_cleanup (c);
  185. c = NULL;
  186. multi = NULL;
  187. }
  188. }
  189. }
  190. if (multi != NULL)
  191. {
  192. curl_multi_remove_handle (multi, c);
  193. curl_easy_cleanup (c);
  194. curl_multi_cleanup (multi);
  195. }
  196. MHD_stop_daemon (d);
  197. if (cbc.pos != 0)
  198. return 8192;
  199. return 0;
  200. }
  201. int
  202. main (int argc, char *const *argv)
  203. {
  204. unsigned int errorCount = 0;
  205. if (0 != curl_global_init (CURL_GLOBAL_ALL))
  206. {
  207. fprintf (stderr, "Error: %s\n", strerror (errno));
  208. return 99;
  209. }
  210. if (NULL == curl_version_info (CURLVERSION_NOW)->ssl_version)
  211. {
  212. fprintf (stderr, "Curl does not support SSL. Cannot run the test.\n");
  213. curl_global_cleanup ();
  214. return 77;
  215. }
  216. if (0 != (errorCount = testInternalSelectGet ()))
  217. fprintf (stderr, "Failed test: %s, error: %u.\n", argv[0], errorCount);
  218. curl_global_cleanup ();
  219. return errorCount != 0 ? 1 : 0;
  220. }