simplepost.c 4.6 KB

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