diff --git a/examples/embedded_c/embedded_c.c b/examples/embedded_c/embedded_c.c index 08bb33aa..ee3bc467 100644 --- a/examples/embedded_c/embedded_c.c +++ b/examples/embedded_c/embedded_c.c @@ -63,6 +63,9 @@ ExampleHandler(struct mg_connection *conn, void *cbdata) mg_printf(conn, "

To see a page from the *.foo handler click xy.foo

"); + mg_printf(conn, + "

To see a page from the FileHandler handler click form (this is the form test page)

"); #ifdef USE_WEBSOCKET mg_printf(conn, "

To test websocket handler 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); diff --git a/test/form.html b/test/form.html index d5bb77ae..92aaaad0 100644 --- a/test/form.html +++ b/test/form.html @@ -22,7 +22,7 @@ --> -

+ See HTML form tutorial.
@@ -89,6 +89,9 @@
+ +