|
@@ -23,6 +23,26 @@
|
|
/* EXPERIMENTAL !!! */
|
|
/* 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
|
|
int
|
|
handle_form_data(struct mg_connection *conn, struct mg_form_data_handler *fdh)
|
|
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);
|
|
(conn->request_info.content_length > 0) || (conn->is_chunked);
|
|
char *data;
|
|
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)) {
|
|
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;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /* TODO: handle multipart request */
|
|
|
|
+ return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /* Unknown Content-Type */
|
|
return 0;
|
|
return 0;
|
|
-}
|
|
|
|
|
|
+}
|