Explorar el Código

Alternative to mg_upload (Step 4/?)

bel hace 9 años
padre
commit
2a676b114c
Se han modificado 2 ficheros con 44 adiciones y 1 borrados
  1. 40 0
      examples/embedded_c/embedded_c.c
  2. 4 1
      test/form.html

+ 40 - 0
examples/embedded_c/embedded_c.c

@@ -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);
 

+ 4 - 1
test/form.html

@@ -22,7 +22,7 @@
   -->
 
 
-  <form action="/handle_form.lua">
+  <form action="/handle_form.embedded_c.example.callback">
     See <a href="http://www.w3schools.com/html/html_form_input_types.asp">HTML form tutorial</a>.<br />
 
     <fieldset>
@@ -89,6 +89,9 @@
       <textarea name="message" rows="10" cols="30">Text area default text.</textarea>
     </fieldset>
 
+    <input type="submit" value="Submit to Lua script"         formmethod="POST"   formenctype="multipart/form-data"
+     formaction="/handle_form.lua">
+     
     <input type="submit" value="Submit (form default)">
     <input type="submit" value="Submit (GET)"                 formmethod="GET">
     <input type="submit" value="Submit (POST)"                formmethod="POST">