upload.c 2.7 KB

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