Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <microhttpd.h>
  4.  
  5. static int dh(void *cls, struct MHD_Connection *con, const char *url, const char *method, const char *version,
  6.               const char *upload_data, size_t *upload_data_size, void **con_cls) {
  7.     const char *page = "<html><body>I love MHD!</body></html>";
  8.     struct MHD_Response *response;
  9.     int ret;
  10.     if (NULL != *con_cls) {
  11.         *con_cls = (void *) 1;
  12.         return MHD_YES;
  13.     }
  14.     /* simulating a long processing, like hard query in a database ... */
  15.     unsigned char i = 1;
  16.     while (i < 100) {
  17.         sleep(1);
  18.         i++;
  19.     }
  20.     response = MHD_create_response_from_buffer(strlen(page), (void *) page, MHD_RESPMEM_PERSISTENT);
  21.     ret = MHD_queue_response(con, MHD_HTTP_OK, response);
  22.     MHD_destroy_response(response);
  23.     return ret;
  24. }
  25.  
  26.  
  27. int main() {
  28.     struct MHD_Daemon *d;
  29.     MHD_socket fd;
  30.     d = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY | MHD_USE_ITC | MHD_USE_DEBUG, 8080, NULL, NULL, &dh, NULL,
  31.                          /* MHD_OPTION_CONNECTION_TIMEOUT, unsigned int 5, no need because I'm simulating a really busy client ... */
  32.                          MHD_OPTION_END);
  33.     getchar();
  34.     fd = MHD_quiesce_daemon(d);
  35.     if (fd != MHD_INVALID_SOCKET)
  36.         close(fd);
  37.     else
  38.         fprintf(stderr, "Server quiesce failed.\n");
  39.     MHD_stop_daemon(d);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement