largepost.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. 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. 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 **con_cls,
  151. enum MHD_RequestTerminationCode toe)
  152. {
  153. struct connection_info_struct *con_info = *con_cls;
  154. if (NULL == con_info)
  155. return;
  156. if (con_info->connectiontype == POST)
  157. {
  158. if (NULL != con_info->postprocessor)
  159. {
  160. MHD_destroy_post_processor (con_info->postprocessor);
  161. nr_of_uploading_clients--;
  162. }
  163. if (con_info->fp)
  164. fclose (con_info->fp);
  165. }
  166. free (con_info);
  167. *con_cls = NULL;
  168. }
  169. static int
  170. answer_to_connection (void *cls,
  171. struct MHD_Connection *connection,
  172. const char *url,
  173. const char *method,
  174. const char *version,
  175. const char *upload_data,
  176. size_t *upload_data_size,
  177. void **con_cls)
  178. {
  179. if (NULL == *con_cls)
  180. {
  181. /* First call, setup data structures */
  182. struct connection_info_struct *con_info;
  183. if (nr_of_uploading_clients >= MAXCLIENTS)
  184. return send_page (connection,
  185. busypage,
  186. MHD_HTTP_SERVICE_UNAVAILABLE);
  187. con_info = malloc (sizeof (struct connection_info_struct));
  188. if (NULL == con_info)
  189. return MHD_NO;
  190. con_info->answercode = 0; /* none yet */
  191. con_info->fp = NULL;
  192. if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
  193. {
  194. con_info->postprocessor =
  195. MHD_create_post_processor (connection,
  196. POSTBUFFERSIZE,
  197. &iterate_post,
  198. (void *) con_info);
  199. if (NULL == con_info->postprocessor)
  200. {
  201. free (con_info);
  202. return MHD_NO;
  203. }
  204. nr_of_uploading_clients++;
  205. con_info->connectiontype = POST;
  206. }
  207. else
  208. {
  209. con_info->connectiontype = GET;
  210. }
  211. *con_cls = (void *) con_info;
  212. return MHD_YES;
  213. }
  214. if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET))
  215. {
  216. /* We just return the standard form for uploads on all GET requests */
  217. char buffer[1024];
  218. snprintf (buffer,
  219. sizeof (buffer),
  220. askpage,
  221. nr_of_uploading_clients);
  222. return send_page (connection,
  223. buffer,
  224. MHD_HTTP_OK);
  225. }
  226. if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
  227. {
  228. struct connection_info_struct *con_info = *con_cls;
  229. if (0 != *upload_data_size)
  230. {
  231. /* Upload not yet done */
  232. if (0 != con_info->answercode)
  233. {
  234. /* we already know the answer, skip rest of upload */
  235. *upload_data_size = 0;
  236. return MHD_YES;
  237. }
  238. if (MHD_YES !=
  239. MHD_post_process (con_info->postprocessor,
  240. upload_data,
  241. *upload_data_size))
  242. {
  243. con_info->answerstring = postprocerror;
  244. con_info->answercode = MHD_HTTP_INTERNAL_SERVER_ERROR;
  245. }
  246. *upload_data_size = 0;
  247. return MHD_YES;
  248. }
  249. /* Upload finished */
  250. if (NULL != con_info->fp)
  251. {
  252. fclose (con_info->fp);
  253. con_info->fp = NULL;
  254. }
  255. if (0 == con_info->answercode)
  256. {
  257. /* No errors encountered, declare success */
  258. con_info->answerstring = completepage;
  259. con_info->answercode = MHD_HTTP_OK;
  260. }
  261. return send_page (connection,
  262. con_info->answerstring,
  263. con_info->answercode);
  264. }
  265. /* Note a GET or a POST, generate error */
  266. return send_page (connection,
  267. errorpage,
  268. MHD_HTTP_BAD_REQUEST);
  269. }
  270. int
  271. main ()
  272. {
  273. struct MHD_Daemon *daemon;
  274. daemon = MHD_start_daemon (MHD_USE_INTERNAL_POLLING_THREAD,
  275. PORT, NULL, NULL,
  276. &answer_to_connection, NULL,
  277. MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
  278. MHD_OPTION_END);
  279. if (NULL == daemon)
  280. {
  281. fprintf (stderr,
  282. "Failed to start daemon\n");
  283. return 1;
  284. }
  285. (void) getchar ();
  286. MHD_stop_daemon (daemon);
  287. return 0;
  288. }