upload.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (c) 2014 the Civetweb developers
  2. * Copyright (c) 2004-2012 Sergey Lyubka
  3. * This file is a part of civetweb project, http://github.com/bel2125/civetweb
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <stdlib.h>
  9. #ifdef _WIN32
  10. #include <windows.h>
  11. #include <io.h>
  12. #define strtoll strtol
  13. typedef __int64 int64_t;
  14. #else
  15. #include <inttypes.h>
  16. #include <unistd.h>
  17. #endif // !_WIN32
  18. #include "civetweb.h"
  19. /* callback: used to generate all content */
  20. static int begin_request_handler(struct mg_connection *conn)
  21. {
  22. const char * tempPath = ".";
  23. #ifdef _WIN32
  24. const char * env = getenv("TEMP");
  25. if (!env) env = getenv("TMP");
  26. if (env) tempPath = env;
  27. #else
  28. tempPath = "/tmp";
  29. #endif
  30. if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) {
  31. mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n");
  32. mg_upload(conn, tempPath);
  33. } else {
  34. /* Show HTML form. */
  35. /* See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 */
  36. static const char *html_form =
  37. "<html><body>Upload example."
  38. ""
  39. /* enctype="multipart/form-data" */
  40. "<form method=\"POST\" action=\"/handle_post_request\" "
  41. " enctype=\"multipart/form-data\">"
  42. "<input type=\"file\" name=\"file\" /> <br/>"
  43. "<input type=\"file\" name=\"file2\" /> <br/>"
  44. "<input type=\"submit\" value=\"Upload\" />"
  45. "</form>"
  46. ""
  47. "</body></html>";
  48. mg_printf(conn, "HTTP/1.0 200 OK\r\n"
  49. "Content-Length: %d\r\n"
  50. "Content-Type: text/html\r\n\r\n%s",
  51. (int) strlen(html_form), html_form);
  52. }
  53. // Mark request as processed
  54. return 1;
  55. }
  56. /* callback: called after uploading a file is completed */
  57. static void upload_handler(struct mg_connection *conn, const char *path)
  58. {
  59. mg_printf(conn, "Saved [%s]", path);
  60. }
  61. /* Main program: Set callbacks and start the server. */
  62. int main(void)
  63. {
  64. /* Test server will use this port */
  65. const char * PORT = "8080";
  66. /* Startup options for the server */
  67. struct mg_context *ctx;
  68. const char *options[] = {
  69. "listening_ports", PORT,
  70. NULL};
  71. struct mg_callbacks callbacks;
  72. memset(&callbacks, 0, sizeof(callbacks));
  73. callbacks.begin_request = begin_request_handler;
  74. callbacks.upload = upload_handler;
  75. /* Display a welcome message */
  76. printf("File upload demo.\n");
  77. printf("Open http://localhost:%s/ im your browser.\n\n", PORT);
  78. /* Start the server */
  79. ctx = mg_start(&callbacks, NULL, options);
  80. /* Wait until thr user hits "enter", then stop the server */
  81. getchar();
  82. mg_stop(ctx);
  83. return 0;
  84. }