mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-17 19:30:36 +00:00
Add ajax_upload to big_upload
PUBLISHED_FROM=7c919ba3a2afa74cec9947e78299b4bb7a64b4bc
This commit is contained in:
1 parent
066be0fe30
commit
aab857a66e
2 files changed
+80
-5
No files matched your search
@@ -10,6 +10,7 @@
|
||||
#include "mongoose.h"
|
||||
|
||||
static const char *s_http_port = "8000";
|
||||
static struct mg_serve_http_opts s_http_server_opts;
|
||||
|
||||
struct file_writer_data {
|
||||
FILE *fp;
|
||||
@@ -83,11 +84,33 @@ static void handle_upload(struct mg_connection *nc, int ev, void *p) {
|
||||
|
||||
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
|
||||
(void) ev_data;
|
||||
switch (ev) {
|
||||
case MG_EV_HTTP_REQUEST:
|
||||
// Invoked when the full HTTP request is in the buffer (including body).
|
||||
handle_request(nc);
|
||||
break;
|
||||
if (ev == MG_EV_HTTP_REQUEST) {
|
||||
mg_printf(nc, "%s",
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/html\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
"<html><body>Upload example."
|
||||
"Navigate to <a "
|
||||
"href=\"simple_upload_demo\">/simple_upload_demo</a> for "
|
||||
"uploading using submit "
|
||||
"or to <a href=\"/ajax_upload_demo\">/ajax_upload_demo</a> for "
|
||||
"uploading using ajax"
|
||||
"</form></body></html>");
|
||||
nc->flags |= MG_F_SEND_AND_CLOSE;
|
||||
}
|
||||
}
|
||||
|
||||
static void upload_demo_handler(struct mg_connection *nc, int ev, void *p) {
|
||||
if (ev == MG_EV_HTTP_REQUEST) {
|
||||
(void) p;
|
||||
handle_request(nc);
|
||||
}
|
||||
}
|
||||
|
||||
static void ajax_demo_handler(struct mg_connection *nc, int ev, void *p) {
|
||||
if (ev == MG_EV_HTTP_REQUEST) {
|
||||
mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,7 +121,14 @@ int main(void) {
|
||||
mg_mgr_init(&mgr, NULL);
|
||||
nc = mg_bind(&mgr, s_http_port, ev_handler);
|
||||
|
||||
s_http_server_opts.document_root = "."; // Serve current directory
|
||||
|
||||
mg_register_http_endpoint(nc, "/upload", handle_upload MG_UD_ARG(NULL));
|
||||
mg_register_http_endpoint(nc, "/ajax_upload_demo",
|
||||
ajax_demo_handler MG_UD_ARG(NULL));
|
||||
mg_register_http_endpoint(nc, "/simple_upload_demo",
|
||||
upload_demo_handler MG_UD_ARG(NULL));
|
||||
|
||||
// Set up HTTP server parameters
|
||||
mg_set_protocol_http_websocket(nc);
|
||||
|
||||
|
||||
Reference in new issue
Block a user