sessions.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /* Feel free to use this example code in any way
  2. you see fit (Public Domain) */
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <errno.h>
  7. #include <time.h>
  8. #include <microhttpd.h>
  9. /**
  10. * Invalid method page.
  11. */
  12. #define METHOD_ERROR \
  13. "<html><head><title>Illegal request</title></head><body>Go away.</body></html>"
  14. /**
  15. * Invalid URL page.
  16. */
  17. #define NOT_FOUND_ERROR \
  18. "<html><head><title>Not found</title></head><body>Go away.</body></html>"
  19. /**
  20. * Front page. (/)
  21. */
  22. #define MAIN_PAGE \
  23. "<html><head><title>Welcome</title></head><body><form action=\"/2\" method=\"post\">What is your name? <input type=\"text\" name=\"v1\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>"
  24. #define FORM_V1 MAIN_PAGE
  25. /**
  26. * Second page. (/2)
  27. */
  28. #define SECOND_PAGE \
  29. "<html><head><title>Tell me more</title></head><body><a href=\"/\">previous</a> <form action=\"/S\" method=\"post\">%s, what is your job? <input type=\"text\" name=\"v2\" value=\"%s\" /><input type=\"submit\" value=\"Next\" /></body></html>"
  30. #define FORM_V1_V2 SECOND_PAGE
  31. /**
  32. * Second page (/S)
  33. */
  34. #define SUBMIT_PAGE \
  35. "<html><head><title>Ready to submit?</title></head><body><form action=\"/F\" method=\"post\"><a href=\"/2\">previous </a> <input type=\"hidden\" name=\"DONE\" value=\"yes\" /><input type=\"submit\" value=\"Submit\" /></body></html>"
  36. /**
  37. * Last page.
  38. */
  39. #define LAST_PAGE \
  40. "<html><head><title>Thank you</title></head><body>Thank you.</body></html>"
  41. /**
  42. * Name of our cookie.
  43. */
  44. #define COOKIE_NAME "session"
  45. /**
  46. * State we keep for each user/session/browser.
  47. */
  48. struct Session
  49. {
  50. /**
  51. * We keep all sessions in a linked list.
  52. */
  53. struct Session *next;
  54. /**
  55. * Unique ID for this session.
  56. */
  57. char sid[33];
  58. /**
  59. * Reference counter giving the number of connections
  60. * currently using this session.
  61. */
  62. unsigned int rc;
  63. /**
  64. * Time when this session was last active.
  65. */
  66. time_t start;
  67. /**
  68. * String submitted via form.
  69. */
  70. char value_1[64];
  71. /**
  72. * Another value submitted via form.
  73. */
  74. char value_2[64];
  75. };
  76. /**
  77. * Data kept per request.
  78. */
  79. struct Request
  80. {
  81. /**
  82. * Associated session.
  83. */
  84. struct Session *session;
  85. /**
  86. * Post processor handling form data (IF this is
  87. * a POST request).
  88. */
  89. struct MHD_PostProcessor *pp;
  90. /**
  91. * URL to serve in response to this POST (if this request
  92. * was a 'POST')
  93. */
  94. const char *post_url;
  95. };
  96. /**
  97. * Linked list of all active sessions. Yes, O(n) but a
  98. * hash table would be overkill for a simple example...
  99. */
  100. static struct Session *sessions;
  101. /**
  102. * Return the session handle for this connection, or
  103. * create one if this is a new user.
  104. */
  105. static struct Session *
  106. get_session (struct MHD_Connection *connection)
  107. {
  108. struct Session *ret;
  109. const char *cookie;
  110. cookie = MHD_lookup_connection_value (connection,
  111. MHD_COOKIE_KIND,
  112. COOKIE_NAME);
  113. if (cookie != NULL)
  114. {
  115. /* find existing session */
  116. ret = sessions;
  117. while (NULL != ret)
  118. {
  119. if (0 == strcmp (cookie, ret->sid))
  120. break;
  121. ret = ret->next;
  122. }
  123. if (NULL != ret)
  124. {
  125. ret->rc++;
  126. return ret;
  127. }
  128. }
  129. /* create fresh session */
  130. ret = calloc (1, sizeof (struct Session));
  131. if (NULL == ret)
  132. {
  133. fprintf (stderr, "calloc error: %s\n", strerror (errno));
  134. return NULL;
  135. }
  136. /* not a super-secure way to generate a random session ID,
  137. but should do for a simple example... */
  138. snprintf (ret->sid,
  139. sizeof (ret->sid),
  140. "%X%X%X%X",
  141. (unsigned int) rand (),
  142. (unsigned int) rand (),
  143. (unsigned int) rand (),
  144. (unsigned int) rand ());
  145. ret->rc++;
  146. ret->start = time (NULL);
  147. ret->next = sessions;
  148. sessions = ret;
  149. return ret;
  150. }
  151. /**
  152. * Type of handler that generates a reply.
  153. *
  154. * @param cls content for the page (handler-specific)
  155. * @param mime mime type to use
  156. * @param session session information
  157. * @param connection connection to process
  158. * @param #MHD_YES on success, #MHD_NO on failure
  159. */
  160. typedef enum MHD_Result (*PageHandler)(const void *cls,
  161. const char *mime,
  162. struct Session *session,
  163. struct MHD_Connection *connection);
  164. /**
  165. * Entry we generate for each page served.
  166. */
  167. struct Page
  168. {
  169. /**
  170. * Acceptable URL for this page.
  171. */
  172. const char *url;
  173. /**
  174. * Mime type to set for the page.
  175. */
  176. const char *mime;
  177. /**
  178. * Handler to call to generate response.
  179. */
  180. PageHandler handler;
  181. /**
  182. * Extra argument to handler.
  183. */
  184. const void *handler_cls;
  185. };
  186. /**
  187. * Add header to response to set a session cookie.
  188. *
  189. * @param session session to use
  190. * @param response response to modify
  191. */
  192. static void
  193. add_session_cookie (struct Session *session,
  194. struct MHD_Response *response)
  195. {
  196. char cstr[256];
  197. snprintf (cstr,
  198. sizeof (cstr),
  199. "%s=%s",
  200. COOKIE_NAME,
  201. session->sid);
  202. if (MHD_NO ==
  203. MHD_add_response_header (response,
  204. MHD_HTTP_HEADER_SET_COOKIE,
  205. cstr))
  206. {
  207. fprintf (stderr,
  208. "Failed to set session cookie header!\n");
  209. }
  210. }
  211. /**
  212. * Handler that returns a simple static HTTP page that
  213. * is passed in via 'cls'.
  214. *
  215. * @param cls a 'const char *' with the HTML webpage to return
  216. * @param mime mime type to use
  217. * @param session session handle
  218. * @param connection connection to use
  219. */
  220. static enum MHD_Result
  221. serve_simple_form (const void *cls,
  222. const char *mime,
  223. struct Session *session,
  224. struct MHD_Connection *connection)
  225. {
  226. enum MHD_Result ret;
  227. const char *form = cls;
  228. struct MHD_Response *response;
  229. /* return static form */
  230. response = MHD_create_response_from_buffer_static (strlen (form), form);
  231. add_session_cookie (session, response);
  232. MHD_add_response_header (response,
  233. MHD_HTTP_HEADER_CONTENT_ENCODING,
  234. mime);
  235. ret = MHD_queue_response (connection,
  236. MHD_HTTP_OK,
  237. response);
  238. MHD_destroy_response (response);
  239. return ret;
  240. }
  241. /**
  242. * Handler that adds the 'v1' value to the given HTML code.
  243. *
  244. * @param cls a 'const char *' with the HTML webpage to return
  245. * @param mime mime type to use
  246. * @param session session handle
  247. * @param connection connection to use
  248. */
  249. static enum MHD_Result
  250. fill_v1_form (const void *cls,
  251. const char *mime,
  252. struct Session *session,
  253. struct MHD_Connection *connection)
  254. {
  255. enum MHD_Result ret;
  256. char *reply;
  257. struct MHD_Response *response;
  258. int reply_len;
  259. (void) cls; /* Unused */
  260. /* Emulate 'asprintf' */
  261. reply_len = snprintf (NULL, 0, FORM_V1, session->value_1);
  262. if (0 > reply_len)
  263. return MHD_NO; /* Internal error */
  264. reply = (char *) malloc (reply_len + 1);
  265. if (NULL == reply)
  266. return MHD_NO; /* Out-of-memory error */
  267. if (reply_len != snprintf (reply,
  268. ((size_t) reply_len) + 1,
  269. FORM_V1,
  270. session->value_1))
  271. {
  272. free (reply);
  273. return MHD_NO; /* printf error */
  274. }
  275. /* return static form */
  276. response =
  277. MHD_create_response_from_buffer_with_free_callback ((size_t) reply_len,
  278. (void *) reply,
  279. &free);
  280. if (NULL != response)
  281. {
  282. add_session_cookie (session, response);
  283. MHD_add_response_header (response,
  284. MHD_HTTP_HEADER_CONTENT_ENCODING,
  285. mime);
  286. ret = MHD_queue_response (connection,
  287. MHD_HTTP_OK,
  288. response);
  289. MHD_destroy_response (response);
  290. }
  291. else
  292. {
  293. free (reply);
  294. ret = MHD_NO;
  295. }
  296. return ret;
  297. }
  298. /**
  299. * Handler that adds the 'v1' and 'v2' values to the given HTML code.
  300. *
  301. * @param cls a 'const char *' with the HTML webpage to return
  302. * @param mime mime type to use
  303. * @param session session handle
  304. * @param connection connection to use
  305. */
  306. static enum MHD_Result
  307. fill_v1_v2_form (const void *cls,
  308. const char *mime,
  309. struct Session *session,
  310. struct MHD_Connection *connection)
  311. {
  312. enum MHD_Result ret;
  313. char *reply;
  314. struct MHD_Response *response;
  315. int reply_len;
  316. (void) cls; /* Unused */
  317. /* Emulate 'asprintf' */
  318. reply_len = snprintf (NULL, 0, FORM_V1_V2, session->value_1,
  319. session->value_2);
  320. if (0 > reply_len)
  321. return MHD_NO; /* Internal error */
  322. reply = (char *) malloc (reply_len + 1);
  323. if (NULL == reply)
  324. return MHD_NO; /* Out-of-memory error */
  325. if (reply_len == snprintf (reply,
  326. ((size_t) reply_len) + 1,
  327. FORM_V1_V2,
  328. session->value_1,
  329. session->value_2))
  330. {
  331. free (reply);
  332. return MHD_NO; /* printf error */
  333. }
  334. /* return static form */
  335. response =
  336. MHD_create_response_from_buffer_with_free_callback ((size_t) reply_len,
  337. (void *) reply,
  338. &free);
  339. if (NULL != response)
  340. {
  341. add_session_cookie (session, response);
  342. MHD_add_response_header (response,
  343. MHD_HTTP_HEADER_CONTENT_ENCODING,
  344. mime);
  345. ret = MHD_queue_response (connection,
  346. MHD_HTTP_OK,
  347. response);
  348. MHD_destroy_response (response);
  349. }
  350. else
  351. {
  352. free (reply);
  353. ret = MHD_NO;
  354. }
  355. return ret;
  356. }
  357. /**
  358. * Handler used to generate a 404 reply.
  359. *
  360. * @param cls a 'const char *' with the HTML webpage to return
  361. * @param mime mime type to use
  362. * @param session session handle
  363. * @param connection connection to use
  364. */
  365. static enum MHD_Result
  366. not_found_page (const void *cls,
  367. const char *mime,
  368. struct Session *session,
  369. struct MHD_Connection *connection)
  370. {
  371. enum MHD_Result ret;
  372. struct MHD_Response *response;
  373. (void) cls; /* Unused. Silent compiler warning. */
  374. (void) session; /* Unused. Silent compiler warning. */
  375. /* unsupported HTTP method */
  376. response = MHD_create_response_from_buffer_static (strlen (NOT_FOUND_ERROR),
  377. NOT_FOUND_ERROR);
  378. ret = MHD_queue_response (connection,
  379. MHD_HTTP_NOT_FOUND,
  380. response);
  381. MHD_add_response_header (response,
  382. MHD_HTTP_HEADER_CONTENT_ENCODING,
  383. mime);
  384. MHD_destroy_response (response);
  385. return ret;
  386. }
  387. /**
  388. * List of all pages served by this HTTP server.
  389. */
  390. static const struct Page pages[] = {
  391. { "/", "text/html", &fill_v1_form, NULL },
  392. { "/2", "text/html", &fill_v1_v2_form, NULL },
  393. { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE },
  394. { "/F", "text/html", &serve_simple_form, LAST_PAGE },
  395. { NULL, NULL, &not_found_page, NULL } /* 404 */
  396. };
  397. /**
  398. * Iterator over key-value pairs where the value
  399. * maybe made available in increments and/or may
  400. * not be zero-terminated. Used for processing
  401. * POST data.
  402. *
  403. * @param cls user-specified closure
  404. * @param kind type of the value
  405. * @param key 0-terminated key for the value
  406. * @param filename name of the uploaded file, NULL if not known
  407. * @param content_type mime-type of the data, NULL if not known
  408. * @param transfer_encoding encoding of the data, NULL if not known
  409. * @param data pointer to size bytes of data at the
  410. * specified offset
  411. * @param off offset of data in the overall value
  412. * @param size number of bytes in data available
  413. * @return MHD_YES to continue iterating,
  414. * MHD_NO to abort the iteration
  415. */
  416. static enum MHD_Result
  417. post_iterator (void *cls,
  418. enum MHD_ValueKind kind,
  419. const char *key,
  420. const char *filename,
  421. const char *content_type,
  422. const char *transfer_encoding,
  423. const char *data, uint64_t off, size_t size)
  424. {
  425. struct Request *request = cls;
  426. struct Session *session = request->session;
  427. (void) kind; /* Unused. Silent compiler warning. */
  428. (void) filename; /* Unused. Silent compiler warning. */
  429. (void) content_type; /* Unused. Silent compiler warning. */
  430. (void) transfer_encoding; /* Unused. Silent compiler warning. */
  431. if (0 == strcmp ("DONE", key))
  432. {
  433. fprintf (stdout,
  434. "Session `%s' submitted `%s', `%s'\n",
  435. session->sid,
  436. session->value_1,
  437. session->value_2);
  438. return MHD_YES;
  439. }
  440. if (0 == strcmp ("v1", key))
  441. {
  442. if (size + off > sizeof(session->value_1))
  443. size = sizeof (session->value_1) - off;
  444. memcpy (&session->value_1[off],
  445. data,
  446. size);
  447. if (size + off < sizeof (session->value_1))
  448. session->value_1[size + off] = '\0';
  449. return MHD_YES;
  450. }
  451. if (0 == strcmp ("v2", key))
  452. {
  453. if (size + off > sizeof(session->value_2))
  454. size = sizeof (session->value_2) - off;
  455. memcpy (&session->value_2[off],
  456. data,
  457. size);
  458. if (size + off < sizeof (session->value_2))
  459. session->value_2[size + off] = '\0';
  460. return MHD_YES;
  461. }
  462. fprintf (stderr, "Unsupported form value `%s'\n", key);
  463. return MHD_YES;
  464. }
  465. /**
  466. * Main MHD callback for handling requests.
  467. *
  468. *
  469. * @param cls argument given together with the function
  470. * pointer when the handler was registered with MHD
  471. * @param connection handle to connection which is being processed
  472. * @param url the requested url
  473. * @param method the HTTP method used ("GET", "PUT", etc.)
  474. * @param version the HTTP version string (i.e. "HTTP/1.1")
  475. * @param upload_data the data being uploaded (excluding HEADERS,
  476. * for a POST that fits into memory and that is encoded
  477. * with a supported encoding, the POST data will NOT be
  478. * given in upload_data and is instead available as
  479. * part of MHD_get_connection_values; very large POST
  480. * data *will* be made available incrementally in
  481. * upload_data)
  482. * @param upload_data_size set initially to the size of the
  483. * upload_data provided; the method must update this
  484. * value to the number of bytes NOT processed;
  485. * @param req_cls pointer that the callback can set to some
  486. * address and that will be preserved by MHD for future
  487. * calls for this request; since the access handler may
  488. * be called many times (i.e., for a PUT/POST operation
  489. * with plenty of upload data) this allows the application
  490. * to easily associate some request-specific state.
  491. * If necessary, this state can be cleaned up in the
  492. * global "MHD_RequestCompleted" callback (which
  493. * can be set with the MHD_OPTION_NOTIFY_COMPLETED).
  494. * Initially, <tt>*req_cls</tt> will be NULL.
  495. * @return MHS_YES if the connection was handled successfully,
  496. * MHS_NO if the socket must be closed due to a serious
  497. * error while handling the request
  498. */
  499. static enum MHD_Result
  500. create_response (void *cls,
  501. struct MHD_Connection *connection,
  502. const char *url,
  503. const char *method,
  504. const char *version,
  505. const char *upload_data,
  506. size_t *upload_data_size,
  507. void **req_cls)
  508. {
  509. struct MHD_Response *response;
  510. struct Request *request;
  511. struct Session *session;
  512. enum MHD_Result ret;
  513. unsigned int i;
  514. (void) cls; /* Unused. Silent compiler warning. */
  515. (void) version; /* Unused. Silent compiler warning. */
  516. request = *req_cls;
  517. if (NULL == request)
  518. {
  519. request = calloc (1, sizeof (struct Request));
  520. if (NULL == request)
  521. {
  522. fprintf (stderr, "calloc error: %s\n", strerror (errno));
  523. return MHD_NO;
  524. }
  525. *req_cls = request;
  526. if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
  527. {
  528. request->pp = MHD_create_post_processor (connection, 1024,
  529. &post_iterator, request);
  530. if (NULL == request->pp)
  531. {
  532. fprintf (stderr, "Failed to setup post processor for `%s'\n",
  533. url);
  534. return MHD_NO; /* internal error */
  535. }
  536. }
  537. return MHD_YES;
  538. }
  539. if (NULL == request->session)
  540. {
  541. request->session = get_session (connection);
  542. if (NULL == request->session)
  543. {
  544. fprintf (stderr, "Failed to setup session for `%s'\n",
  545. url);
  546. return MHD_NO; /* internal error */
  547. }
  548. }
  549. session = request->session;
  550. session->start = time (NULL);
  551. if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
  552. {
  553. /* evaluate POST data */
  554. MHD_post_process (request->pp,
  555. upload_data,
  556. *upload_data_size);
  557. if (0 != *upload_data_size)
  558. {
  559. *upload_data_size = 0;
  560. return MHD_YES;
  561. }
  562. /* done with POST data, serve response */
  563. MHD_destroy_post_processor (request->pp);
  564. request->pp = NULL;
  565. method = MHD_HTTP_METHOD_GET; /* fake 'GET' */
  566. if (NULL != request->post_url)
  567. url = request->post_url;
  568. }
  569. if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
  570. (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
  571. {
  572. /* find out which page to serve */
  573. i = 0;
  574. while ( (pages[i].url != NULL) &&
  575. (0 != strcmp (pages[i].url, url)) )
  576. i++;
  577. ret = pages[i].handler (pages[i].handler_cls,
  578. pages[i].mime,
  579. session, connection);
  580. if (ret != MHD_YES)
  581. fprintf (stderr, "Failed to create page for `%s'\n",
  582. url);
  583. return ret;
  584. }
  585. /* unsupported HTTP method */
  586. response = MHD_create_response_from_buffer_static (strlen (METHOD_ERROR),
  587. METHOD_ERROR);
  588. ret = MHD_queue_response (connection,
  589. MHD_HTTP_NOT_ACCEPTABLE,
  590. response);
  591. MHD_destroy_response (response);
  592. return ret;
  593. }
  594. /**
  595. * Callback called upon completion of a request.
  596. * Decrements session reference counter.
  597. *
  598. * @param cls not used
  599. * @param connection connection that completed
  600. * @param req_cls session handle
  601. * @param toe status code
  602. */
  603. static void
  604. request_completed_callback (void *cls,
  605. struct MHD_Connection *connection,
  606. void **req_cls,
  607. enum MHD_RequestTerminationCode toe)
  608. {
  609. struct Request *request = *req_cls;
  610. (void) cls; /* Unused. Silent compiler warning. */
  611. (void) connection; /* Unused. Silent compiler warning. */
  612. (void) toe; /* Unused. Silent compiler warning. */
  613. if (NULL == request)
  614. return;
  615. if (NULL != request->session)
  616. request->session->rc--;
  617. if (NULL != request->pp)
  618. MHD_destroy_post_processor (request->pp);
  619. free (request);
  620. }
  621. /**
  622. * Clean up handles of sessions that have been idle for
  623. * too long.
  624. */
  625. static void
  626. expire_sessions (void)
  627. {
  628. struct Session *pos;
  629. struct Session *prev;
  630. struct Session *next;
  631. time_t now;
  632. now = time (NULL);
  633. prev = NULL;
  634. pos = sessions;
  635. while (NULL != pos)
  636. {
  637. next = pos->next;
  638. if (now - pos->start > 60 * 60)
  639. {
  640. /* expire sessions after 1h */
  641. if (NULL == prev)
  642. sessions = pos->next;
  643. else
  644. prev->next = next;
  645. free (pos);
  646. }
  647. else
  648. prev = pos;
  649. pos = next;
  650. }
  651. }
  652. /**
  653. * Call with the port number as the only argument.
  654. * Never terminates (other than by signals, such as CTRL-C).
  655. */
  656. int
  657. main (int argc, char *const *argv)
  658. {
  659. struct MHD_Daemon *d;
  660. struct timeval tv;
  661. struct timeval *tvp;
  662. fd_set rs;
  663. fd_set ws;
  664. fd_set es;
  665. MHD_socket max;
  666. uint64_t mhd_timeout;
  667. unsigned int port;
  668. if (argc != 2)
  669. {
  670. printf ("%s PORT\n", argv[0]);
  671. return 1;
  672. }
  673. if ( (1 != sscanf (argv[1], "%u", &port)) ||
  674. (0 == port) || (65535 < port) )
  675. {
  676. fprintf (stderr,
  677. "Port must be a number between 1 and 65535.\n");
  678. return 1;
  679. }
  680. /* initialize PRNG */
  681. srand ((unsigned int) time (NULL));
  682. d = MHD_start_daemon (MHD_USE_ERROR_LOG,
  683. (uint16_t) port,
  684. NULL, NULL,
  685. &create_response, NULL,
  686. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15,
  687. MHD_OPTION_NOTIFY_COMPLETED,
  688. &request_completed_callback, NULL,
  689. MHD_OPTION_END);
  690. if (NULL == d)
  691. return 1;
  692. while (1)
  693. {
  694. expire_sessions ();
  695. max = 0;
  696. FD_ZERO (&rs);
  697. FD_ZERO (&ws);
  698. FD_ZERO (&es);
  699. if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
  700. break; /* fatal internal error */
  701. if (MHD_get_timeout64 (d, &mhd_timeout) == MHD_YES)
  702. {
  703. #if ! defined(_WIN32) || defined(__CYGWIN__)
  704. tv.tv_sec = (time_t) (mhd_timeout / 1000);
  705. #else /* Native W32 */
  706. tv.tv_sec = (long) (mhd_timeout / 1000);
  707. #endif /* Native W32 */
  708. tv.tv_usec = ((long) (mhd_timeout % 1000)) * 1000;
  709. tvp = &tv;
  710. }
  711. else
  712. tvp = NULL;
  713. if (-1 == select ((int) max + 1, &rs, &ws, &es, tvp))
  714. {
  715. if (EINTR != errno)
  716. fprintf (stderr,
  717. "Aborting due to error during select: %s\n",
  718. strerror (errno));
  719. break;
  720. }
  721. MHD_run (d);
  722. }
  723. MHD_stop_daemon (d);
  724. return 0;
  725. }