|
@@ -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 "
|
|
@@ -142,6 +145,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)
|
|
|
{
|
|
|
mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
|
|
@@ -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);
|
|
|
|