Sfoglia il codice sorgente

Alternative to mg_upload (Step 6/?)

bel 9 anni fa
parent
commit
6725df7b1c
3 ha cambiato i file con 67 aggiunte e 27 eliminazioni
  1. 7 2
      examples/embedded_c/embedded_c.c
  2. 1 0
      src/civetweb.c
  3. 59 25
      src/handle_form.inl

+ 7 - 2
examples/embedded_c/embedded_c.c

@@ -155,18 +155,23 @@ FileHandler(struct mg_connection *conn, void *cbdata)
 }
 
 
+struct mg_form_data_handler {
+	int (*field_found)(const char *key, const char *value);
+};
+
+
 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;
+	struct mg_form_data_handler fdh = {0};
 
 	/* TODO: Checks before calling handle_form_data ? */
 	(void)req_info;
 
-	ret = handle_form_data(conn, fdh);
+	ret = handle_form_data(conn, &fdh);
 
 	return 1;
 }

+ 1 - 0
src/civetweb.c

@@ -6383,6 +6383,7 @@ handle_static_file_request(struct mg_connection *conn,
 	mg_fclose(filep);
 }
 
+
 void
 mg_send_file(struct mg_connection *conn, const char *path)
 {

+ 59 - 25
src/handle_form.inl

@@ -23,6 +23,26 @@
 /* EXPERIMENTAL !!! */
 /********************/
 
+void
+mirror_body___dev_helper(struct mg_connection *conn)
+{
+	/* TODO: remove this function when handle_form_data is completed. */
+	char buf[256];
+	int r;
+	mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n");
+
+	do {
+		r = mg_read(conn, buf, sizeof(buf));
+		mg_write(conn, buf, r);
+	} while (r > 0);
+}
+
+
+struct mg_form_data_handler {
+	int (*field_found)(const char *key, const char *value);
+};
+
+
 int
 handle_form_data(struct mg_connection *conn, struct mg_form_data_handler *fdh)
 {
@@ -32,42 +52,56 @@ handle_form_data(struct mg_connection *conn, struct mg_form_data_handler *fdh)
 	    (conn->request_info.content_length > 0) || (conn->is_chunked);
 	char *data;
 
-    /* There are three ways to encode data from a HTML form:
-     * 1) method: GET (default)
-     *    The form data is in the HTTP query string.
-     * 2) method: POST, enctype: "application/x-www-form-urlencoded"
-     *    The form data is in the request body. 
-     *    The body is url encoded (the default encoding for POST).
-     * 3) method: POST, enctype: "multipart/form-data".
-     *    The form data is in the request body of a multipart message.
-     *    This is the typical way to handle file upload from a form.
-     */
+	/* There are three ways to encode data from a HTML form:
+	 * 1) method: GET (default)
+	 *    The form data is in the HTTP query string.
+	 * 2) method: POST, enctype: "application/x-www-form-urlencoded"
+	 *    The form data is in the request body.
+	 *    The body is url encoded (the default encoding for POST).
+	 * 3) method: POST, enctype: "multipart/form-data".
+	 *    The form data is in the request body of a multipart message.
+	 *    This is the typical way to handle file upload from a form.
+	 */
 
-	content_type = mg_get_header(conn, "Content-Type");
-	if (content_type == NULL) {
-		/* This request does not have a content type set .. TODO: but it could be a GET requst */
-		//return 0;
+	if (!has_body_data) {
+		if (strcmp(conn->request_info.request_method, "GET")) {
+			/* No body data, but not a GET request.
+			 * This is not a valid form request. */
+			return 0;
+		}
+
+		/* GET request: form data is in the query string. */
+		data = conn->request_info.query_string;
+		/* TODO: split data in a=1&b&c=3&c=4 ... */
+		return 0;
 	}
 
-	if (!mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")) {
-		/* Encoded in key/value pairs */
+	mirror_body___dev_helper(conn);
 
-		if (has_body_data) {
-			/* form data is in the request body data */
+	content_type = mg_get_header(conn, "Content-Type");
 
-		} else {
-			/* form data is in the query string */
-		}
+	if (!content_type
+	    || !mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED")) {
+		/* The form data is in the request body data, encoded in key/value
+		 * pairs. */
+		/* TODO: read body data and split it in a=1&b&c=3&c=4 ... */
+		return 0;
 	}
 
 	if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) {
-		/* Encoded in multipart body */
+		/* The form data is in the request body data, encoded as multipart
+		 * content. */
 
-		if (!has_body_data) {
-			/* Error: no form data */
+		/* There has to be a BOUNDARY definition in the Content-Type header */
+		if (!mg_strncasecmp(content_type + 20, "BOUNDARY=", 9)) {
+			/* Malformed request */
 			return 0;
 		}
+
+		/* TODO: handle multipart request */
+		return 0;
 	}
 
+	/* Unknown Content-Type */
 	return 0;
-}
+}