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