simplepost.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if (0 == strcmp (key, "name"))
  62. {
  63. if ((size > 0) && (size <= MAXNAMESIZE))
  64. {
  65. char *answerstring;
  66. answerstring = malloc (MAXANSWERSIZE);
  67. if (!answerstring)
  68. return MHD_NO;
  69. snprintf (answerstring, MAXANSWERSIZE, greetingpage, data);
  70. con_info->answerstring = answerstring;
  71. }
  72. else
  73. con_info->answerstring = NULL;
  74. return MHD_NO;
  75. }
  76. return MHD_YES;
  77. }
  78. static void
  79. request_completed (void *cls, struct MHD_Connection *connection,
  80. void **con_cls, enum MHD_RequestTerminationCode toe)
  81. {
  82. struct connection_info_struct *con_info = *con_cls;
  83. if (NULL == con_info)
  84. return;
  85. if (con_info->connectiontype == POST)
  86. {
  87. MHD_destroy_post_processor (con_info->postprocessor);
  88. if (con_info->answerstring)
  89. free (con_info->answerstring);
  90. }
  91. free (con_info);
  92. *con_cls = NULL;
  93. }
  94. static int
  95. answer_to_connection (void *cls, struct MHD_Connection *connection,
  96. const char *url, const char *method,
  97. const char *version, const char *upload_data,
  98. size_t *upload_data_size, void **con_cls)
  99. {
  100. if (NULL == *con_cls)
  101. {
  102. struct connection_info_struct *con_info;
  103. con_info = malloc (sizeof (struct connection_info_struct));
  104. if (NULL == con_info)
  105. return MHD_NO;
  106. con_info->answerstring = NULL;
  107. if (0 == strcmp (method, "POST"))
  108. {
  109. con_info->postprocessor =
  110. MHD_create_post_processor (connection, POSTBUFFERSIZE,
  111. iterate_post, (void *) con_info);
  112. if (NULL == con_info->postprocessor)
  113. {
  114. free (con_info);
  115. return MHD_NO;
  116. }
  117. con_info->connectiontype = POST;
  118. }
  119. else
  120. con_info->connectiontype = GET;
  121. *con_cls = (void *) con_info;
  122. return MHD_YES;
  123. }
  124. if (0 == strcmp (method, "GET"))
  125. {
  126. return send_page (connection, askpage);
  127. }
  128. if (0 == strcmp (method, "POST"))
  129. {
  130. struct connection_info_struct *con_info = *con_cls;
  131. if (*upload_data_size != 0)
  132. {
  133. MHD_post_process (con_info->postprocessor, upload_data,
  134. *upload_data_size);
  135. *upload_data_size = 0;
  136. return MHD_YES;
  137. }
  138. else if (NULL != con_info->answerstring)
  139. return send_page (connection, con_info->answerstring);
  140. }
  141. return send_page (connection, errorpage);
  142. }
  143. int
  144. main ()
  145. {
  146. struct MHD_Daemon *daemon;
  147. daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, PORT, NULL, NULL,
  148. &answer_to_connection, NULL,
  149. MHD_OPTION_NOTIFY_COMPLETED, request_completed,
  150. NULL, MHD_OPTION_END);
  151. if (NULL == daemon)
  152. return 1;
  153. (void) getchar ();
  154. MHD_stop_daemon (daemon);
  155. return 0;
  156. }