largepost.c 8.9 KB

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