minimal_example.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. This file is part of libmicrohttpd
  3. Copyright (C) 2007 Christian Grothoff (and other contributing authors)
  4. Copyright (C) 2014-2022 Evgeny Grin (Karlson2k)
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. /**
  18. * @file minimal_example.c
  19. * @brief minimal example for how to use libmicrohttpd
  20. * @author Christian Grothoff
  21. * @author Karlson2k (Evgeny Grin)
  22. */
  23. #include "platform.h"
  24. #include <microhttpd.h>
  25. #define PAGE \
  26. "<html><head><title>libmicrohttpd demo</title></head>" \
  27. "<body>libmicrohttpd demo</body></html>"
  28. struct handler_param
  29. {
  30. const char *response_page;
  31. };
  32. static enum MHD_Result
  33. ahc_echo (void *cls,
  34. struct MHD_Connection *connection,
  35. const char *url,
  36. const char *method,
  37. const char *version,
  38. const char *upload_data,
  39. size_t *upload_data_size,
  40. void **req_cls)
  41. {
  42. static int aptr;
  43. struct handler_param *param = (struct handler_param *) cls;
  44. struct MHD_Response *response;
  45. enum MHD_Result ret;
  46. (void) url; /* Unused. Silent compiler warning. */
  47. (void) version; /* Unused. Silent compiler warning. */
  48. (void) upload_data; /* Unused. Silent compiler warning. */
  49. (void) upload_data_size; /* Unused. Silent compiler warning. */
  50. if (0 != strcmp (method, "GET"))
  51. return MHD_NO; /* unexpected method */
  52. if (&aptr != *req_cls)
  53. {
  54. /* do never respond on first call */
  55. *req_cls = &aptr;
  56. return MHD_YES;
  57. }
  58. *req_cls = NULL; /* reset when done */
  59. response =
  60. MHD_create_response_from_buffer_static (strlen (param->response_page),
  61. param->response_page);
  62. ret = MHD_queue_response (connection,
  63. MHD_HTTP_OK,
  64. response);
  65. MHD_destroy_response (response);
  66. return ret;
  67. }
  68. int
  69. main (int argc,
  70. char *const *argv)
  71. {
  72. struct MHD_Daemon *d;
  73. struct handler_param data_for_handler;
  74. int port;
  75. if (argc != 2)
  76. {
  77. printf ("%s PORT\n", argv[0]);
  78. return 1;
  79. }
  80. port = atoi (argv[1]);
  81. if ( (1 > port) || (port > 65535) )
  82. {
  83. fprintf (stderr,
  84. "Port must be a number between 1 and 65535.\n");
  85. return 1;
  86. }
  87. data_for_handler.response_page = PAGE;
  88. d = MHD_start_daemon (/* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
  89. MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG,
  90. /* MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
  91. /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG | MHD_USE_POLL, */
  92. /* MHD_USE_THREAD_PER_CONNECTION | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_ERROR_LOG, */
  93. (uint16_t) port,
  94. NULL, NULL, &ahc_echo, &data_for_handler,
  95. MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 120,
  96. MHD_OPTION_END);
  97. if (d == NULL)
  98. return 1;
  99. (void) getc (stdin);
  100. MHD_stop_daemon (d);
  101. return 0;
  102. }