largepost.c 9.5 KB

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