upload.c 2.9 KB

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