upload.c 2.9 KB

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