hellobrowser.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Feel free to use this example code in any way
  2. you see fit (Public Domain) */
  3. #include <sys/types.h>
  4. #ifndef _WIN32
  5. #include <sys/select.h>
  6. #include <sys/socket.h>
  7. #else
  8. #include <winsock2.h>
  9. #endif
  10. #include <string.h>
  11. #include <microhttpd.h>
  12. #include <stdio.h>
  13. #define PORT 8888
  14. static int
  15. answer_to_connection (void *cls, struct MHD_Connection *connection,
  16. const char *url, const char *method,
  17. const char *version, const char *upload_data,
  18. size_t *upload_data_size, void **con_cls)
  19. {
  20. const char *page = "<html><body>Hello, browser!</body></html>";
  21. struct MHD_Response *response;
  22. int ret;
  23. (void)cls; /* Unused. Silent compiler warning. */
  24. (void)url; /* Unused. Silent compiler warning. */
  25. (void)method; /* Unused. Silent compiler warning. */
  26. (void)version; /* Unused. Silent compiler warning. */
  27. (void)upload_data; /* Unused. Silent compiler warning. */
  28. (void)upload_data_size; /* Unused. Silent compiler warning. */
  29. (void)con_cls; /* Unused. Silent compiler warning. */
  30. response =
  31. MHD_create_response_from_buffer (strlen (page), (void *) page,
  32. MHD_RESPMEM_PERSISTENT);
  33. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  34. MHD_destroy_response (response);
  35. return ret;
  36. }
  37. int
  38. main (void)
  39. {
  40. struct MHD_Daemon *daemon;
  41. daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
  42. &answer_to_connection, NULL, MHD_OPTION_END);
  43. if (NULL == daemon)
  44. return 1;
  45. (void) getchar ();
  46. MHD_stop_daemon (daemon);
  47. return 0;
  48. }