largepost.c 9.2 KB

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