Quellcode durchsuchen

Alternative to mg_upload (Step 3/?)

bel vor 9 Jahren
Ursprung
Commit
8db8c7c9f1
3 geänderte Dateien mit 56 neuen und 36 gelöschten Zeilen
  1. 1 0
      format.bat
  2. 35 34
      src/handle_form.inl
  3. 20 2
      test/form.html

+ 1 - 0
format.bat

@@ -7,6 +7,7 @@ clang-format -i src/md5.inl
 clang-format -i src/mod_lua.inl
 clang-format -i src/mod_duktape.inl
 clang-format -i src/timer.inl
+clang-format -i src/handle_form.inl
 
 clang-format -i src/third_party/civetweb_lua.h
 

+ 35 - 34
src/handle_form.inl

@@ -23,39 +23,40 @@
 /* EXPERIMENTAL !!! */
 /********************/
 
-int handle_form_data(struct mg_connection *conn, struct mg_form_data_handler *fdh)
+int
+handle_form_data(struct mg_connection *conn, struct mg_form_data_handler *fdh)
 {
-    const char *content_type;
-    const char *boundary;
-    int has_body_data = (conn->request_info.content_length>0) || conn->is_chunked;
-    char *data;
-            
-    content_type = mg_get_header(conn, "Content-Type");
-    if (content_type == NULL) {
-        /* This request does not have a content type set */
-        return 0;
-    }
-
-    if (!mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")) {
-        /* Encoded in key/value pairs */
-
-        if (has_body_data) {
-            /* form data is in the request body data */
-            
-        } else {
-            /* form data is in the query string */
-
-        }
-    }
-
-    if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
-        /* Encoded in multipart body */
-
-        if (!has_body_data) {
-            /* Error: no form data */
-            return 0;
-        }
-    }
-
-    return 0;
+	const char *content_type;
+	const char *boundary;
+	int has_body_data =
+	    (conn->request_info.content_length > 0) || (conn->is_chunked);
+	char *data;
+
+	content_type = mg_get_header(conn, "Content-Type");
+	if (content_type == NULL) {
+		/* This request does not have a content type set */
+		return 0;
+	}
+
+	if (!mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")) {
+		/* Encoded in key/value pairs */
+
+		if (has_body_data) {
+			/* form data is in the request body data */
+
+		} else {
+			/* form data is in the query string */
+		}
+	}
+
+	if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
+		/* Encoded in multipart body */
+
+		if (!has_body_data) {
+			/* Error: no form data */
+			return 0;
+		}
+	}
+
+	return 0;
 }

+ 20 - 2
test/form.html

@@ -8,7 +8,21 @@
 </head>
 
 <body>
-  <form action="/handle_form.lua" method="POST" enctype="multipart/form-data">
+
+  <!-- HTML forms can use GET or POST, and the encoding can be application/x-www-form-urlencoded or multipart/form-data.
+       If no method is specified (like <form method="method">), GET should be the default method.
+       If no encoding is specified, application/x-www-form-urlencoded should be the default.
+       Submit buttons may overwrite action, method and enctype by using formaction, formmethod and formenctype.
+
+       References:
+       http://www.w3.org/TR/html401/interact/forms.html
+       http://www.w3schools.com/html/html_forms.asp,
+       http://www.w3schools.com/html/html_form_attributes.asp
+       http://www.w3.org/TR/html401/interact/forms.html#adef-enctype
+  -->
+
+
+  <form action="/handle_form.lua">
     See <a href="http://www.w3schools.com/html/html_form_input_types.asp">HTML form tutorial</a>.<br />
 
     <fieldset>
@@ -75,7 +89,11 @@
       <textarea name="message" rows="10" cols="30">Text area default text.</textarea>
     </fieldset>
 
-    <input type="submit" value="Submit">
+    <input type="submit" value="Submit (form default)">
+    <input type="submit" value="Submit (GET)"                 formmethod="GET">
+    <input type="submit" value="Submit (POST)"                formmethod="POST">
+    <input type="submit" value="Submit (POST, url-encoded)"   formmethod="POST"   formenctype="application/x-www-form-urlencoded">
+    <input type="submit" value="Submit (POST, form-data)"     formmethod="POST"   formenctype="multipart/form-data">
 
   </form>
 </body>