]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/examples/_obsolete/upload/upload.c
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
6 /* This example is deprecated and no longer maintained.
7 * All relevant parts have been merged into the embedded_c example. */
11 #ifndef _CRT_SECURE_NO_WARNINGS
12 #define _CRT_SECURE_NO_WARNINGS
16 #define strtoll strtol
17 typedef __int64
int64_t;
31 /* callback: used to generate all content */
32 static int begin_request_handler(struct mg_connection
*conn
)
34 const char * tempPath
= ".";
36 const char * env
= getenv("TEMP");
37 if (!env
) env
= getenv("TMP");
38 if (env
) tempPath
= env
;
43 if (!strcmp(mg_get_request_info(conn
)->uri
, "/handle_post_request")) {
45 mg_printf(conn
, "%s", "HTTP/1.0 200 OK\r\n\r\n");
46 mg_upload(conn
, tempPath
);
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."
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\" />"
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
);
69 /* Mark request as processed */
74 /* callback: called after uploading a file is completed */
75 static void upload_handler(struct mg_connection
*conn
, const char *path
)
77 mg_printf(conn
, "Saved [%s]", path
);
81 /* Main program: Set callbacks and start the server. */
84 /* Test server will use this port */
85 const char * PORT
= "8080";
87 /* Startup options for the server */
88 struct mg_context
*ctx
;
89 const char *options
[] = {
90 "listening_ports", PORT
,
92 struct mg_callbacks callbacks
;
94 memset(&callbacks
, 0, sizeof(callbacks
));
95 callbacks
.begin_request
= begin_request_handler
;
96 callbacks
.upload
= upload_handler
;
98 /* Display a welcome message */
99 printf("File upload demo.\n");
100 printf("Open http://localhost:%s/ in your browser.\n\n", PORT
);
102 /* Start the server */
103 ctx
= mg_start(&callbacks
, NULL
, options
);
105 /* Wait until thr user hits "enter", then stop the server */