largepost.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #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 MAXCLIENTS 2
  21. enum ConnectionType
  22. {
  23. GET = 0,
  24. POST = 1
  25. };
  26. static unsigned int nr_of_uploading_clients = 0;
  27. /**
  28. * Information we keep per connection.
  29. */
  30. struct connection_info_struct
  31. {
  32. enum ConnectionType connectiontype;
  33. /**
  34. * Handle to the POST processing state.
  35. */
  36. struct MHD_PostProcessor *postprocessor;
  37. /**
  38. * File handle where we write uploaded data.
  39. */
  40. FILE *fp;
  41. /**
  42. * HTTP response body we will return, NULL if not yet known.
  43. */
  44. const char *answerstring;
  45. /**
  46. * HTTP status code we will return, 0 for undecided.
  47. */
  48. unsigned int answercode;
  49. };
  50. const char *askpage =
  51. "<html><body>\n\
  52. Upload a file, please!<br>\n\
  53. There are %u clients uploading at the moment.<br>\n\
  54. <form action=\"/filepost\" method=\"post\" enctype=\"multipart/form-data\">\n\
  55. <input name=\"file\" type=\"file\">\n\
  56. <input type=\"submit\" value=\" Send \"></form>\n\
  57. </body></html>";
  58. const char *busypage =
  59. "<html><body>This server is busy, please try again later.</body></html>";
  60. const char *completepage =
  61. "<html><body>The upload has been completed.</body></html>";
  62. const char *errorpage =
  63. "<html><body>This doesn't seem to be right.</body></html>";
  64. const char *servererrorpage =
  65. "<html><body>Invalid request.</body></html>";
  66. const char *fileexistspage =
  67. "<html><body>This file already exists.</body></html>";
  68. const char *fileioerror =
  69. "<html><body>IO error writing to disk.</body></html>";
  70. const char *const postprocerror =
  71. "<html><head><title>Error</title></head><body>Error processing POST data</body></html>";
  72. static enum MHD_Result
  73. send_page (struct MHD_Connection *connection,
  74. const char *page,
  75. int status_code)
  76. {
  77. enum MHD_Result ret;
  78. struct MHD_Response *response;
  79. response =
  80. MHD_create_response_from_buffer (strlen (page),
  81. (void *) page,
  82. MHD_RESPMEM_MUST_COPY);
  83. if (! response)
  84. return MHD_NO;
  85. MHD_add_response_header (response,
  86. MHD_HTTP_HEADER_CONTENT_TYPE,
  87. "text/html");
  88. ret = MHD_queue_response (connection,
  89. status_code,
  90. response);
  91. MHD_destroy_response (response);
  92. return ret;
  93. }
  94. static enum MHD_Result
  95. iterate_post (void *coninfo_cls,
  96. enum MHD_ValueKind kind,
  97. const char *key,
  98. const char *filename,
  99. const char *content_type,
  100. const char *transfer_encoding,
  101. const char *data,
  102. uint64_t off,
  103. size_t size)
  104. {
  105. struct connection_info_struct *con_info = coninfo_cls;
  106. FILE *fp;
  107. (void) kind; /* Unused. Silent compiler warning. */
  108. (void) content_type; /* Unused. Silent compiler warning. */
  109. (void) transfer_encoding; /* Unused. Silent compiler warning. */
  110. (void) off; /* Unused. Silent compiler warning. */
  111. if (0 != strcmp (key, "file"))
  112. {
  113. con_info->answerstring = servererrorpage;
  114. con_info->answercode = MHD_HTTP_BAD_REQUEST;
  115. return MHD_YES;
  116. }
  117. if (! con_info->fp)
  118. {
  119. if (0 != con_info->answercode) /* something went wrong */
  120. return MHD_YES;
  121. if (NULL != (fp = fopen (filename, "rb")))
  122. {
  123. fclose (fp);
  124. con_info->answerstring = fileexistspage;
  125. con_info->answercode = MHD_HTTP_FORBIDDEN;
  126. return MHD_YES;
  127. }
  128. /* NOTE: This is technically a race with the 'fopen()' above,
  129. but there is no easy fix, short of moving to open(O_EXCL)
  130. instead of using fopen(). For the example, we do not care. */
  131. con_info->fp = fopen (filename, "ab");
  132. if (! con_info->fp)
  133. {
  134. con_info->answerstring = fileioerror;
  135. con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
  136. return MHD_YES;
  137. }
  138. }
  139. if (size > 0)
  140. {
  141. if (! fwrite (data, sizeof (char), size, con_info->fp))
  142. {
  143. con_info->answerstring = fileioerror;
  144. con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
  145. return MHD_YES;
  146. }
  147. }
  148. return MHD_YES;
  149. }
  150. static void
  151. request_completed (void *cls,
  152. struct MHD_Connection *connection,
  153. void **con_cls,
  154. enum MHD_RequestTerminationCode toe)
  155. {
  156. struct connection_info_struct *con_info = *con_cls;
  157. (void) cls; /* Unused. Silent compiler warning. */
  158. (void) connection; /* Unused. Silent compiler warning. */
  159. (void) toe; /* Unused. Silent compiler warning. */
  160. if (NULL == con_info)
  161. return;
  162. if (con_info->connectiontype == POST)
  163. {
  164. if (NULL != con_info->postprocessor)
  165. {
  166. MHD_destroy_post_processor (con_info->postprocessor);
  167. nr_of_uploading_clients--;
  168. }
  169. if (con_info->fp)
  170. fclose (con_info->fp);
  171. }
  172. free (con_info);
  173. *con_cls = NULL;
  174. }
  175. static enum MHD_Result
  176. answer_to_connection (void *cls,
  177. struct MHD_Connection *connection,
  178. const char *url,
  179. const char *method,
  180. const char *version,
  181. const char *upload_data,
  182. size_t *upload_data_size,
  183. void **con_cls)
  184. {
  185. (void) cls; /* Unused. Silent compiler warning. */
  186. (void) url; /* Unused. Silent compiler warning. */
  187. (void) version; /* Unused. Silent compiler warning. */
  188. if (NULL == *con_cls)
  189. {
  190. /* First call, setup data structures */
  191. struct connection_info_struct *con_info;
  192. if (nr_of_uploading_clients >= MAXCLIENTS)
  193. return send_page (connection,
  194. busypage,
  195. MHD_HTTP_SERVICE_UNAVAILABLE);
  196. con_info = malloc (sizeof (struct connection_info_struct));
  197. if (NULL == con_info)
  198. return MHD_NO;
  199. con_info->answercode = 0; /* none yet */
  200. con_info->fp = NULL;
  201. if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
  202. {
  203. con_info->postprocessor =
  204. MHD_create_post_processor (connection,
  205. POSTBUFFERSIZE,
  206. &iterate_post,
  207. (void *) con_info);
  208. if (NULL == con_info->postprocessor)
  209. {
  210. free (con_info);
  211. return MHD_NO;
  212. }
  213. nr_of_uploading_clients++;
  214. con_info->connectiontype = POST;
  215. }
  216. else
  217. {
  218. con_info->connectiontype = GET;
  219. }
  220. *con_cls = (void *) con_info;
  221. return MHD_YES;
  222. }
  223. if (0 == strcmp (method, MHD_HTTP_METHOD_GET))
  224. {
  225. /* We just return the standard form for uploads on all GET requests */
  226. char buffer[1024];
  227. snprintf (buffer,
  228. sizeof (buffer),
  229. askpage,
  230. nr_of_uploading_clients);
  231. return send_page (connection,
  232. buffer,
  233. MHD_HTTP_OK);
  234. }
  235. if (0 == strcmp (method, MHD_HTTP_METHOD_POST))
  236. {
  237. struct connection_info_struct *con_info = *con_cls;
  238. if (0 != *upload_data_size)
  239. {
  240. /* Upload not yet done */
  241. if (0 != con_info->answercode)
  242. {
  243. /* we already know the answer, skip rest of upload */
  244. *upload_data_size = 0;
  245. return MHD_YES;
  246. }
  247. if (MHD_YES !=
  248. MHD_post_process (con_info->postprocessor,
  249. upload_data,
  250. *upload_data_size))
  251. {
  252. con_info->answerstring = postprocerror;
  253. con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
  254. }
  255. *upload_data_size = 0;
  256. return MHD_YES;
  257. }
  258. /* Upload finished */
  259. if (NULL != con_info->fp)
  260. {
  261. fclose (con_info->fp);
  262. con_info->fp = NULL;
  263. }
  264. if (0 == con_info->answercode)
  265. {
  266. /* No errors encountered, declare success */
  267. con_info->answerstring = completepage;
  268. con_info->answercode = MHD_HTTP_OK;
  269. }
  270. return send_page (connection,
  271. con_info->answerstring,
  272. con_info->answercode);
  273. }
  274. /* Note a GET or a POST, generate error */
  275. return send_page (connection,
  276. errorpage,
  277. MHD_HTTP_BAD_REQUEST);
  278. }
  279. int
  280. main ()
  281. {
  282. struct MHD_Daemon *daemon;
  283. daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD,
  284. PORT, NULL, NULL,
  285. &answer_to_connection, NULL,
  286. MHD_OPTION_NOTIFY_COMPLETED, &request_completed,
  287. NULL,
  288. MHD_OPTION_END);
  289. if (NULL == daemon)
  290. {
  291. fprintf (stderr,
  292. "Failed to start daemon.\n");
  293. return 1;
  294. }
  295. (void) getchar ();
  296. MHD_stop_daemon (daemon);
  297. return 0;
  298. }