1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-07-29 21:01:13 +03:00

Alternative to mg_upload (Step 4/?)

This commit is contained in:
bel
2016-01-10 21:58:04 +01:00
parent 8db8c7c9f1
commit 2a676b114c
2 changed files with 44 additions and 1 deletions

View File

@ -63,6 +63,9 @@ ExampleHandler(struct mg_connection *conn, void *cbdata)
mg_printf(conn,
"<p>To see a page from the *.foo handler <a "
"href=\"xy.foo\">click xy.foo</a></p>");
mg_printf(conn,
"<p>To see a page from the FileHandler handler <a "
"href=\"form\">click form</a> (this is the form test page)</p>");
#ifdef USE_WEBSOCKET
mg_printf(conn,
"<p>To test websocket handler <a href=\"/websocket\">click "
@ -141,6 +144,34 @@ FooHandler(struct mg_connection *conn, void *cbdata)
}
int
FileHandler(struct mg_connection *conn, void *cbdata)
{
/* In this handler, we ignore the req_info and send the file "fileName". */
const char *fileName = (const char*)cbdata;
mg_send_file(conn, fileName);
return 1;
}
int
FormHandler(struct mg_connection *conn, void *cbdata)
{
/* Handler may access the request info using mg_get_request_info */
const struct mg_request_info *req_info = mg_get_request_info(conn);
int ret;
struct mg_form_data_handler *fdh = 0;
/* TODO: Checks before calling handle_form_data ? */
(void)req_info;
ret = handle_form_data(conn, fdh);
return 1;
}
int
WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
{
@ -389,6 +420,15 @@ main(int argc, char *argv[])
/* Add handler for all files with .foo extention */
mg_set_request_handler(ctx, "**.foo$", FooHandler, 0);
/* Add handler for /form (serve a file outside the document root) */
mg_set_request_handler(ctx, "/form", FileHandler, (void *)"../../test/form.html");
/* Add handler for form data */
mg_set_request_handler(ctx,
"/handle_form.embedded_c.example.callback",
FormHandler,
(void *)0);
/* Add HTTP site to open a websocket connection */
mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0);