simplepost.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <microhttpd.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #if defined(_MSC_VER) && _MSC_VER+0 <= 1800
  15. /* Substitution is OK while return value is not used */
  16. #define snprintf _snprintf
  17. #endif
  18. #define PORT 8888
  19. #define POSTBUFFERSIZE 512
  20. #define MAXNAMESIZE 20
  21. #define MAXANSWERSIZE 512
  22. #define GET 0
  23. #define POST 1
  24. struct connection_info_struct
  25. {
  26. int connectiontype;
  27. char *answerstring;
  28. struct MHD_PostProcessor *postprocessor;
  29. };
  30. const char *askpage = "<html><body>\
  31. What's your name, Sir?<br>\
  32. <form action=\"/namepost\" method=\"post\">\
  33. <input name=\"name\" type=\"text\">\
  34. <input type=\"submit\" value=\" Send \"></form>\
  35. </body></html>";
  36. const char *greetingpage =
  37. "<html><body><h1>Welcome, %s!</center></h1></body></html>";
  38. const char *errorpage =
  39. "<html><body>This doesn't seem to be right.</body></html>";
  40. static int
  41. send_page (struct MHD_Connection *connection, const char *page)
  42. {
  43. int ret;
  44. struct MHD_Response *response;
  45. response =
  46. MHD_create_response_from_buffer (strlen (page), (void *) page,
  47. MHD_RESPMEM_PERSISTENT);
  48. if (!response)
  49. return MHD_NO;
  50. ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
  51. MHD_destroy_response (response);
  52. return ret;
  53. }
  54. static int
  55. iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
  56. const char *filename, const char *content_type,
  57. const char *transfer_encoding, const char *data, uint64_t off,
  58. size_t size)
  59. {
  60. struct connection_info_struct *con_info = coninfo_cls;
  61. (void)kind; /* Unused. Silent compiler warning. */
  62. (void)filename; /* Unused. Silent compiler warning. */
  63. (void)content_type; /* Unused. Silent compiler warning. */
  64. (void)transfer_encoding; /* Unused. Silent compiler warning. */
  65. (void)off; /* Unused. Silent compiler warning. */
  66. if (0 == strcmp (key, "name"))
  67. {
  68. if ((size > 0) && (size <= MAXNAMESIZE))
  69. {
  70. char *answerstring;
  71. answerstring = malloc (MAXANSWERSIZE);
  72. if (!answerstring)
  73. return MHD_NO;
  74. snprintf (answerstring, MAXANSWERSIZE, greetingpage, data);
  75. con_info->answerstring = answerstring;
  76. }
  77. else
  78. con_info->answerstring = NULL;
  79. return MHD_NO;
  80. }
  81. return MHD_YES;
  82. }
  83. static void
  84. request_completed (void *cls, struct MHD_Connection *connection,
  85. void **con_cls, enum MHD_RequestTerminationCode toe)
  86. {
  87. struct connection_info_struct *con_info = *con_cls;
  88. (void)cls; /* Unused. Silent compiler warning. */
  89. (void)connection; /* Unused. Silent compiler warning. */
  90. (void)toe; /* Unused. Silent compiler warning. */
  91. if (NULL == con_info)
  92. return;
  93. if (con_info->connectiontype == POST)
  94. {
  95. MHD_destroy_post_processor (con_info->postprocessor);
  96. if (con_info->answerstring)
  97. free (con_info->answerstring);
  98. }
  99. free (con_info);
  100. *con_cls = NULL;
  101. }
  102. static int
  103. answer_to_connection (void *cls, struct MHD_Connection *connection,
  104. const char *url, const char *method,
  105. const char *version, const char *upload_data,
  106. size_t *upload_data_size, void **con_cls)
  107. {
  108. (void)cls; /* Unused. Silent compiler warning. */
  109. (void)url; /* Unused. Silent compiler warning. */
  110. (void)version; /* Unused. Silent compiler warning. */
  111. if (NULL == *con_cls)
  112. {
  113. struct connection_info_struct *con_info;
  114. con_info = malloc (sizeof (struct connection_info_struct));
  115. if (NULL == con_info)
  116. return MHD_NO;
  117. con_info->answerstring = NULL;
  118. if (0 == strcmp (method, "POST"))
  119. {
  120. con_info->postprocessor =
  121. MHD_create_post_processor (connection, POSTBUFFERSIZE,
  122. iterate_post, (void *) con_info);
  123. if (NULL == con_info->postprocessor)
  124. {
  125. free (con_info);
  126. return MHD_NO;
  127. }
  128. con_info->connectiontype = POST;
  129. }
  130. else
  131. con_info->connectiontype = GET;
  132. *con_cls = (void *) con_info;
  133. return MHD_YES;
  134. }
  135. if (0 == strcmp (method, "GET"))
  136. {
  137. return send_page (connection, askpage);
  138. }
  139. if (0 == strcmp (method, "POST"))
  140. {
  141. struct connection_info_struct *con_info = *con_cls;
  142. if (*upload_data_size != 0)
  143. {
  144. MHD_post_process (con_info->postprocessor, upload_data,
  145. *upload_data_size);
  146. *upload_data_size = 0;
  147. return MHD_YES;
  148. }
  149. else if (NULL != con_info->answerstring)
  150. return send_page (connection, con_info->answerstring);
  151. }
  152. return send_page (connection, errorpage);
  153. }
  154. int
  155. main ()
  156. {
  157. struct MHD_Daemon *daemon;
  158. daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
  159. &answer_to_connection, NULL,
  160. MHD_OPTION_NOTIFY_COMPLETED, request_completed,
  161. NULL, MHD_OPTION_END);
  162. if (NULL == daemon)
  163. return 1;
  164. (void) getchar ();
  165. MHD_stop_daemon (daemon);
  166. return 0;
  167. }