largepost.c 9.1 KB

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