main.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2021 Cesanta Software Limited
  2. // All rights reserved
  3. #include "mongoose.h"
  4. // HTTP request handler function. It implements the following endpoints:
  5. // /upload - prints all submitted form elements
  6. // all other URI - serves web_root/ directory
  7. //
  8. // ///////////////// IMPORTANT //////////////////////////
  9. //
  10. // Mongoose has a limit on input buffer, which also limits maximum upload size.
  11. // It is controlled by the MG_MAX_RECV_SIZE constant, which is set by
  12. // default to (3 * 1024 * 1024), i.e. 3 megabytes.
  13. // Use -DMG_MAX_BUF_SIZE=NEW_LIMIT to override it.
  14. //
  15. // Also, consider changing -DMG_IO_SIZE=SOME_BIG_VALUE to increase IO buffer
  16. // increment when reading data.
  17. static void cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
  18. if (ev == MG_EV_HTTP_MSG) {
  19. struct mg_http_message *hm = (struct mg_http_message *) ev_data;
  20. MG_INFO(("New request to: [%.*s], body size: %lu", (int) hm->uri.len,
  21. hm->uri.ptr, (unsigned long) hm->body.len));
  22. if (mg_http_match_uri(hm, "/upload")) {
  23. struct mg_http_part part;
  24. size_t ofs = 0;
  25. while ((ofs = mg_http_next_multipart(hm->body, ofs, &part)) > 0) {
  26. MG_INFO(("Chunk name: [%.*s] filename: [%.*s] length: %lu bytes",
  27. (int) part.name.len, part.name.ptr, (int) part.filename.len,
  28. part.filename.ptr, (unsigned long) part.body.len));
  29. }
  30. mg_http_reply(c, 200, "", "Thank you!");
  31. } else {
  32. struct mg_http_serve_opts opts = {.root_dir = "web_root"};
  33. mg_http_serve_dir(c, ev_data, &opts);
  34. }
  35. }
  36. }
  37. int main(void) {
  38. struct mg_mgr mgr;
  39. mg_mgr_init(&mgr);
  40. mg_log_set(MG_LL_DEBUG); // Set log level
  41. mg_http_listen(&mgr, "http://localhost:8000", cb, NULL);
  42. for (;;) mg_mgr_poll(&mgr, 50);
  43. mg_mgr_free(&mgr);
  44. return 0;
  45. }