diff --git a/examples/embedded_c/embedded_c.c b/examples/embedded_c/embedded_c.c index 531aa777..4bec9953 100644 --- a/examples/embedded_c/embedded_c.c +++ b/examples/embedded_c/embedded_c.c @@ -27,12 +27,15 @@ int ExampleHandler(struct mg_connection *conn, void *cbdata) mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"); mg_printf(conn, "
"); mg_printf(conn, "To see a page from the A handler click here
"); - mg_printf(conn, "To see a page from the A/B handler click here
"); - mg_printf(conn, "To see a page from the *.foo handler click here
"); - mg_printf(conn, "To test websocket handler click here
"); - mg_printf(conn, "To exit click here
", - EXIT_URI); + mg_printf(conn, "To see a page from the A handler click A
"); + mg_printf(conn, "To see a page from the A handler click A/A
"); + mg_printf(conn, "To see a page from the A/B handler click A/B
"); + mg_printf(conn, "To see a page from the B handler (0) click B
"); + mg_printf(conn, "To see a page from the B handler (1) click B/A
"); + mg_printf(conn, "To see a page from the B handler (2) click B/B
"); + mg_printf(conn, "To see a page from the *.foo handler click xy.foo
"); + mg_printf(conn, "To test websocket handler click websocket
"); + mg_printf(conn, "To exit click exit
", EXIT_URI); mg_printf(conn, "\n"); return 1; } @@ -67,6 +70,20 @@ int ABHandler(struct mg_connection *conn, void *cbdata) } +int BXHandler(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); + + mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"); + mg_printf(conn, ""); + mg_printf(conn, "The actual uri is %s
", req_info->uri); + mg_printf(conn, "\n"); + return 1; +} + + int FooHandler(struct mg_connection *conn, void *cbdata) { /* Handler may access the request info using mg_get_request_info */ @@ -204,12 +221,26 @@ int main(int argc, char *argv[]) memset(&callbacks, 0, sizeof(callbacks)); ctx = mg_start(&callbacks, 0, options); + /* Handler EXAMPLE_URI to explain the example */ mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0); mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0); - mg_set_request_handler(ctx, "/a", AHandler, 0); - mg_set_request_handler(ctx, "/a/b", ABHandler, 0); + + /* Handler for /A* and special handler for /A/B */ + mg_set_request_handler(ctx, "/A", AHandler, 0); + mg_set_request_handler(ctx, "/A/B", ABHandler, 0); + + /* Handler for /B, /B/A, /B/B but not for /B* */ + mg_set_request_handler(ctx, "/B$", BXHandler, (void*)0); + mg_set_request_handler(ctx, "/B/A$", BXHandler, (void*)1); + mg_set_request_handler(ctx, "/B/B$", BXHandler, (void*)2); + + /* Handler for all files with .foo extention */ mg_set_request_handler(ctx, "**.foo$", FooHandler, 0); + + /* HTTP site to open a websocket connection */ mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0); + + /* WS site for the websocket connection */ mg_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, WebsocketDataHandler, WebSocketCloseHandler, 0); printf("Browse files at http://localhost:%s/\n", PORT);