largepost.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <microhttpd.h>
  14. #ifdef _MSC_VER
  15. #ifndef strcasecmp
  16. #define strcasecmp(a,b) _stricmp((a),(b))
  17. #endif /* !strcasecmp */
  18. #endif /* _MSC_VER */
  19. #if defined(_MSC_VER) && _MSC_VER+0 <= 1800
  20. /* Substitution is OK while return value is not used */
  21. #define snprintf _snprintf
  22. #endif
  23. #define PORT 8888
  24. #define POSTBUFFERSIZE 512
  25. #define MAXCLIENTS 2
  26. enum ConnectionType
  27. {
  28. GET = 0,
  29. POST = 1
  30. };
  31. static unsigned int nr_of_uploading_clients = 0;
  32. struct connection_info_struct
  33. {
  34. enum ConnectionType connectiontype;
  35. struct MHD_PostProcessor *postprocessor;
  36. FILE *fp;
  37. const char *answerstring;
  38. int answercode;
  39. };
  40. const char *askpage = "<html><body>\n\
  41. Upload a file, please!<br>\n\
  42. There are %u clients uploading at the moment.<br>\n\
  43. <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
  44. <input name=\"file\" type=\"file\">\n\
  45. <input type=\"submit\" value=\" Send \"></form>\n\
  46. </body></html>";
  47. const char *busypage =
  48. "<html><body>This server is busy, please try again later.</body></html>";
  49. const char *completepage =
  50. "<html><body>The upload has been completed.</body></html>";
  51. const char *errorpage =
  52. "<html><body>This doesn't seem to be right.</body></html>";
  53. const char *servererrorpage =
  54. "<html><body>An internal server error has occured.</body></html>";
  55. const char *fileexistspage =
  56. "<html><body>This file already exists.</body></html>";
  57. const char* const postprocerror =
  58. "<html><head><title>Error</title></head><body>Error processing POST data</body></html>";
  59. static int
  60. send_page (struct MHD_Connection *connection,
  61. const char *page,
  62. int status_code)
  63. {
  64. int ret;
  65. struct MHD_Response *response;
  66. response =
  67. MHD_create_response_from_buffer (strlen (page),
  68. (void *) page,
  69. MHD_RESPMEM_MUST_COPY);
  70. if (!response)
  71. return MHD_NO;
  72. MHD_add_response_header (response,
  73. MHD_HTTP_HEADER_CONTENT_TYPE,
  74. "text/html");
  75. ret = MHD_queue_response (connection,
  76. status_code,
  77. response);
  78. MHD_destroy_response (response);
  79. return ret;
  80. }
  81. static int
  82. iterate_post (void *coninfo_cls,
  83. enum MHD_ValueKind kind,
  84. const char *key,
  85. const char *filename,
  86. const char *content_type,
  87. const char *transfer_encoding,
  88. const char *data,
  89. uint64_t off,
  90. size_t size)
  91. {
  92. struct connection_info_struct *con_info = coninfo_cls;
  93. FILE *fp;
  94. con_info->answerstring = servererrorpage;
  95. con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
  96. if (0 != strcmp (key, "file"))
  97. return MHD_NO;
  98. if (! con_info->fp)
  99. {
  100. if (NULL != (fp = fopen (filename, "rb")))
  101. {
  102. fclose (fp);
  103. con_info->answerstring = fileexistspage;
  104. con_info->answercode = MHD_HTTP_FORBIDDEN;
  105. return MHD_NO;
  106. }
  107. con_info->fp = fopen (filename, "ab");
  108. if (!con_info->fp)
  109. return MHD_NO;
  110. }
  111. if (size > 0)
  112. {
  113. if (! fwrite (data, sizeof (char), size, con_info->fp))
  114. return MHD_NO;
  115. }
  116. con_info->answerstring = completepage;
  117. con_info->answercode = MHD_HTTP_OK;
  118. return MHD_YES;
  119. }
  120. static void
  121. request_completed (void *cls,
  122. struct MHD_Connection *connection,
  123. void **con_cls,
  124. enum MHD_RequestTerminationCode toe)
  125. {
  126. struct connection_info_struct *con_info = *con_cls;
  127. if (NULL == con_info)
  128. return;
  129. if (con_info->connectiontype == POST)
  130. {
  131. if (NULL != con_info->postprocessor)
  132. {
  133. MHD_destroy_post_processor (con_info->postprocessor);
  134. nr_of_uploading_clients--;
  135. }
  136. if (con_info->fp)
  137. fclose (con_info->fp);
  138. }
  139. free (con_info);
  140. *con_cls = NULL;
  141. }
  142. static int
  143. answer_to_connection (void *cls,
  144. struct MHD_Connection *connection,
  145. const char *url,
  146. const char *method,
  147. const char *version,
  148. const char *upload_data,
  149. size_t *upload_data_size,
  150. void **con_cls)
  151. {
  152. if (NULL == *con_cls)
  153. {
  154. struct connection_info_struct *con_info;
  155. if (nr_of_uploading_clients >= MAXCLIENTS)
  156. return send_page (connection,
  157. busypage,
  158. MHD_HTTP_SERVICE_UNAVAILABLE);
  159. con_info = malloc (sizeof (struct connection_info_struct));
  160. if (NULL == con_info)
  161. return MHD_NO;
  162. con_info->fp = NULL;
  163. if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
  164. {
  165. con_info->postprocessor =
  166. MHD_create_post_processor (connection,
  167. POSTBUFFERSIZE,
  168. &iterate_post,
  169. (void *) con_info);
  170. if (NULL == con_info->postprocessor)
  171. {
  172. free (con_info);
  173. return MHD_NO;
  174. }
  175. nr_of_uploading_clients++;
  176. con_info->connectiontype = POST;
  177. con_info->answercode = MHD_HTTP_OK;
  178. con_info->answerstring = completepage;
  179. }
  180. else
  181. con_info->connectiontype = GET;
  182. *con_cls = (void *) con_info;
  183. return MHD_YES;
  184. }
  185. if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET))
  186. {
  187. char buffer[1024];
  188. snprintf (buffer,
  189. sizeof (buffer),
  190. askpage,
  191. nr_of_uploading_clients);
  192. return send_page (connection,
  193. buffer,
  194. MHD_HTTP_OK);
  195. }
  196. if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
  197. {
  198. struct connection_info_struct *con_info = *con_cls;
  199. if (0 != *upload_data_size)
  200. {
  201. if (MHD_post_process (con_info->postprocessor,
  202. upload_data,
  203. *upload_data_size) != MHD_YES)
  204. {
  205. return send_page (connection,
  206. postprocerror,
  207. MHD_HTTP_BAD_REQUEST);
  208. }
  209. *upload_data_size = 0;
  210. return MHD_YES;
  211. }
  212. else
  213. {
  214. if (NULL != con_info->fp)
  215. {
  216. fclose (con_info->fp);
  217. con_info->fp = NULL;
  218. }
  219. /* Now it is safe to open and inspect the file before
  220. calling send_page with a response */
  221. return send_page (connection,
  222. con_info->answerstring,
  223. con_info->answercode);
  224. }
  225. }
  226. return send_page (connection,
  227. errorpage,
  228. MHD_HTTP_BAD_REQUEST);
  229. }
  230. int
  231. main ()
  232. {
  233. struct MHD_Daemon *daemon;
  234. daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY,
  235. PORT, NULL, NULL,
  236. &answer_to_connection, NULL,
  237. MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
  238. MHD_OPTION_END);
  239. if (NULL == daemon)
  240. return 1;
  241. (void) getchar ();
  242. MHD_stop_daemon (daemon);
  243. return 0;
  244. }