test_callback.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007, 2009, 2011 Christian Grothoff
  4. Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
  5. libmicrohttpd is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published
  7. by the Free Software Foundation; either version 2, or (at your
  8. option) any later version.
  9. libmicrohttpd is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with libmicrohttpd; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. /**
  19. * @file test_callback.c
  20. * @brief Testcase for MHD not calling the callback too often
  21. * @author Jan Seeger
  22. * @author Christian Grothoff
  23. * @author Karlson2k (Evgeny Grin)
  24. */
  25. #include "MHD_config.h"
  26. #include "platform.h"
  27. #include <curl/curl.h>
  28. #include <microhttpd.h>
  29. #include <errno.h>
  30. struct callback_closure
  31. {
  32. unsigned int called;
  33. };
  34. static ssize_t
  35. called_twice (void *cls, uint64_t pos, char *buf, size_t max)
  36. {
  37. struct callback_closure *cls2 = cls;
  38. (void) pos; /* Unused. Silence compiler warning. */
  39. (void) max;
  40. if (cls2->called == 0)
  41. {
  42. memcpy (buf, "test", 5);
  43. cls2->called = 1;
  44. return (ssize_t) strlen (buf);
  45. }
  46. if (cls2->called == 1)
  47. {
  48. cls2->called = 2;
  49. return MHD_CONTENT_READER_END_OF_STREAM;
  50. }
  51. fprintf (stderr,
  52. "Handler called after returning END_OF_STREAM!\n");
  53. abort ();
  54. return MHD_CONTENT_READER_END_WITH_ERROR;
  55. }
  56. static enum MHD_Result
  57. callback (void *cls,
  58. struct MHD_Connection *connection,
  59. const char *url,
  60. const char *method,
  61. const char *version,
  62. const char *upload_data,
  63. size_t *upload_data_size,
  64. void **req_cls)
  65. {
  66. struct callback_closure *cbc = calloc (1, sizeof(struct callback_closure));
  67. struct MHD_Response *r;
  68. enum MHD_Result ret;
  69. (void) cls;
  70. (void) url; /* Unused. Silent compiler warning. */
  71. (void) method;
  72. (void) version;
  73. (void) upload_data; /* Unused. Silent compiler warning. */
  74. (void) upload_data_size;
  75. (void) req_cls; /* Unused. Silent compiler warning. */
  76. if (NULL == cbc)
  77. return MHD_NO;
  78. r = MHD_create_response_from_callback (MHD_SIZE_UNKNOWN, 1024,
  79. &called_twice, cbc,
  80. &free);
  81. if (NULL == r)
  82. {
  83. free (cbc);
  84. return MHD_NO;
  85. }
  86. ret = MHD_queue_response (connection,
  87. MHD_HTTP_OK,
  88. r);
  89. MHD_destroy_response (r);
  90. return ret;
  91. }
  92. static size_t
  93. discard_buffer (void *ptr,
  94. size_t size,
  95. size_t nmemb,
  96. void *ctx)
  97. {
  98. (void) ptr; (void) ctx; /* Unused. Silent compiler warning. */
  99. return size * nmemb;
  100. }
  101. int
  102. main (int argc, char **argv)
  103. {
  104. struct MHD_Daemon *d;
  105. fd_set rs;
  106. fd_set ws;
  107. fd_set es;
  108. MHD_socket maxsock;
  109. #ifdef MHD_WINSOCK_SOCKETS
  110. int maxposixs; /* Max socket number unused on W32 */
  111. #else /* MHD_POSIX_SOCKETS */
  112. #define maxposixs maxsock
  113. #endif /* MHD_POSIX_SOCKETS */
  114. CURL *c;
  115. CURLM *multi;
  116. CURLMcode mret;
  117. struct CURLMsg *msg;
  118. int running;
  119. struct timeval tv;
  120. int extra;
  121. uint16_t port;
  122. (void) argc; (void) argv; /* Unused. Silent compiler warning. */
  123. if (MHD_NO != MHD_is_feature_supported (MHD_FEATURE_AUTODETECT_BIND_PORT))
  124. port = 0;
  125. else
  126. port = 1140;
  127. d = MHD_start_daemon (MHD_USE_NO_THREAD_SAFETY,
  128. port,
  129. NULL,
  130. NULL,
  131. &callback,
  132. NULL,
  133. MHD_OPTION_APP_FD_SETSIZE, (int) FD_SETSIZE,
  134. MHD_OPTION_END);
  135. if (d == NULL)
  136. return 32;
  137. if (0 == port)
  138. {
  139. const union MHD_DaemonInfo *dinfo;
  140. dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
  141. if ((NULL == dinfo) || (0 == dinfo->port) )
  142. {
  143. MHD_stop_daemon (d); return 48;
  144. }
  145. port = dinfo->port;
  146. }
  147. c = curl_easy_init ();
  148. curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/");
  149. curl_easy_setopt (c, CURLOPT_PORT, (long) port);
  150. curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &discard_buffer);
  151. curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
  152. curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  153. curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
  154. curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
  155. curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
  156. multi = curl_multi_init ();
  157. if (multi == NULL)
  158. {
  159. curl_easy_cleanup (c);
  160. MHD_stop_daemon (d);
  161. return 99;
  162. }
  163. mret = curl_multi_add_handle (multi, c);
  164. if (mret != CURLM_OK)
  165. {
  166. curl_multi_cleanup (multi);
  167. curl_easy_cleanup (c);
  168. MHD_stop_daemon (d);
  169. return 99;
  170. }
  171. extra = 10;
  172. while ( (c != NULL) || (--extra > 0) )
  173. {
  174. maxsock = MHD_INVALID_SOCKET;
  175. maxposixs = -1;
  176. FD_ZERO (&ws);
  177. FD_ZERO (&rs);
  178. FD_ZERO (&es);
  179. curl_multi_perform (multi, &running);
  180. if (NULL != multi)
  181. {
  182. mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs);
  183. if (mret != CURLM_OK)
  184. {
  185. curl_multi_remove_handle (multi, c);
  186. curl_multi_cleanup (multi);
  187. curl_easy_cleanup (c);
  188. MHD_stop_daemon (d);
  189. return 99;
  190. }
  191. }
  192. if (MHD_YES !=
  193. MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
  194. {
  195. curl_multi_remove_handle (multi, c);
  196. curl_multi_cleanup (multi);
  197. curl_easy_cleanup (c);
  198. MHD_stop_daemon (d);
  199. return 4;
  200. }
  201. tv.tv_sec = 0;
  202. tv.tv_usec = 1000;
  203. if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv))
  204. {
  205. #ifdef MHD_POSIX_SOCKETS
  206. if (EINTR != errno)
  207. {
  208. fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
  209. (int) errno, __LINE__);
  210. fflush (stderr);
  211. exit (99);
  212. }
  213. #else
  214. if ((WSAEINVAL != WSAGetLastError ()) ||
  215. (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
  216. {
  217. fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
  218. (int) WSAGetLastError (), __LINE__);
  219. fflush (stderr);
  220. exit (99);
  221. }
  222. Sleep (1);
  223. #endif
  224. }
  225. if (NULL != multi)
  226. {
  227. curl_multi_perform (multi, &running);
  228. if (0 == running)
  229. {
  230. int pending;
  231. int curl_fine = 0;
  232. while (NULL != (msg = curl_multi_info_read (multi, &pending)))
  233. {
  234. if (msg->msg == CURLMSG_DONE)
  235. {
  236. if (msg->data.result == CURLE_OK)
  237. curl_fine = 1;
  238. else
  239. {
  240. fprintf (stderr,
  241. "%s failed at %s:%d: `%s'\n",
  242. "curl_multi_perform",
  243. __FILE__,
  244. __LINE__, curl_easy_strerror (msg->data.result));
  245. abort ();
  246. }
  247. }
  248. }
  249. if (! curl_fine)
  250. {
  251. fprintf (stderr, "libcurl haven't returned OK code\n");
  252. abort ();
  253. }
  254. curl_multi_remove_handle (multi, c);
  255. curl_multi_cleanup (multi);
  256. curl_easy_cleanup (c);
  257. c = NULL;
  258. multi = NULL;
  259. }
  260. }
  261. MHD_run (d);
  262. }
  263. MHD_stop_daemon (d);
  264. return 0;
  265. }