diff --git a/examples/big_upload/ajax_upload_demo/index.html b/examples/big_upload/ajax_upload_demo/index.html new file mode 100644 index 00000000..9ba5d63c --- /dev/null +++ b/examples/big_upload/ajax_upload_demo/index.html @@ -0,0 +1,45 @@ + + + + AJAX Upload Example + + + + + +
+
+ + +
+

+ + diff --git a/examples/big_upload/big_upload.c b/examples/big_upload/big_upload.c index 682a0c31..5514d7b7 100644 --- a/examples/big_upload/big_upload.c +++ b/examples/big_upload/big_upload.c @@ -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" + "Upload example." + "Navigate to /simple_upload_demo for " + "uploading using submit " + "or to /ajax_upload_demo for " + "uploading using ajax" + ""); + 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);