Procházet zdrojové kódy

Define return values to skip fields in mg_handle_form handling functions

see #646

(Further tests required)
bel2125 před 6 roky
rodič
revize
fc281de264
2 změnil soubory, kde provedl 61 přidání a 9 odebrání
  1. 16 2
      include/civetweb.h
  2. 45 7
      src/handle_form.inl

+ 16 - 2
include/civetweb.h

@@ -1183,7 +1183,8 @@ struct mg_form_data_handler {
 	 *   user_data: Value of the member user_data of mg_form_data_handler
 	 *   user_data: Value of the member user_data of mg_form_data_handler
 	 *
 	 *
 	 * Return value:
 	 * Return value:
-	 *   TODO: Needs to be defined.
+	 *   The return code determines how the server should continue processing
+	 *   the current request (See MG_FORM_FIELD_HANDLE_*).
 	 */
 	 */
 	int (*field_get)(const char *key,
 	int (*field_get)(const char *key,
 	                 const char *value,
 	                 const char *value,
@@ -1204,7 +1205,8 @@ struct mg_form_data_handler {
 	 *   user_data: Value of the member user_data of mg_form_data_handler
 	 *   user_data: Value of the member user_data of mg_form_data_handler
 	 *
 	 *
 	 * Return value:
 	 * Return value:
-	 *   TODO: Needs to be defined.
+	 *   The return code determines how the server should continue processing
+	 *   the current request (See MG_FORM_FIELD_HANDLE_*).
 	 */
 	 */
 	int (*field_store)(const char *path, long long file_size, void *user_data);
 	int (*field_store)(const char *path, long long file_size, void *user_data);
 
 
@@ -1241,6 +1243,18 @@ enum {
 	MG_FORM_FIELD_STORAGE_ABORT = 0x10
 	MG_FORM_FIELD_STORAGE_ABORT = 0x10
 };
 };
 
 
+/* Return values for "field_get" and "field_store" */
+enum {
+	/* Only "field_get": If there is more data in this field, get the next
+     * chunk. Otherwise: handle the next field. */
+	MG_FORM_FIELD_HANDLE_GET = 0x1,
+	/* Handle the next field */
+	MG_FORM_FIELD_HANDLE_NEXT = 0x8,
+	/* Stop parsing this request */
+	MG_FORM_FIELD_HANDLE_ABORT = 0x10
+};
+
+
 /* Process form data.
 /* Process form data.
  * Returns the number of fields handled, or < 0 in case of an error.
  * Returns the number of fields handled, or < 0 in case of an error.
  * Note: It is possible that several fields are already handled successfully
  * Note: It is possible that several fields are already handled successfully

+ 45 - 7
src/handle_form.inl

@@ -272,7 +272,14 @@ mg_handle_form_request(struct mg_connection *conn,
 				/* Call callback */
 				/* Call callback */
 				r = url_encoded_field_get(
 				r = url_encoded_field_get(
 				    conn, data, (size_t)keylen, val, (size_t)vallen, fdh);
 				    conn, data, (size_t)keylen, val, (size_t)vallen, fdh);
-				/* TODO: return value */
+				if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+					/* Stop request handling */
+					break;
+				}
+				if (r == MG_FORM_FIELD_HANDLE_NEXT) {
+					/* Skip to next field */
+					field_storage = MG_FORM_FIELD_STORAGE_SKIP;
+				}
 			}
 			}
 			if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
 			if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
 				/* Store the content to a file */
 				/* Store the content to a file */
@@ -298,7 +305,11 @@ mg_handle_form_request(struct mg_connection *conn,
 						if (r == 0) {
 						if (r == 0) {
 							/* stored successfully */
 							/* stored successfully */
 							r = field_stored(conn, path, file_size, fdh);
 							r = field_stored(conn, path, file_size, fdh);
-							/* TODO: return value */
+							if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+								/* Stop request handling */
+								break;
+							}
+
 						} else {
 						} else {
 							mg_cry_internal(conn,
 							mg_cry_internal(conn,
 							                "%s: Error saving file %s",
 							                "%s: Error saving file %s",
@@ -458,7 +469,14 @@ mg_handle_form_request(struct mg_connection *conn,
 					                          (size_t)vallen,
 					                          (size_t)vallen,
 					                          fdh);
 					                          fdh);
 					get_block++;
 					get_block++;
-					/* TODO: return value */
+					if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+						/* Stop request handling */
+						break;
+					}
+					if (r == MG_FORM_FIELD_HANDLE_NEXT) {
+						/* Skip to next field */
+						field_storage = MG_FORM_FIELD_STORAGE_SKIP;
+					}
 				}
 				}
 				if (fstore.access.fp) {
 				if (fstore.access.fp) {
 					size_t n = (size_t)
 					size_t n = (size_t)
@@ -517,7 +535,10 @@ mg_handle_form_request(struct mg_connection *conn,
 				if (r == 0) {
 				if (r == 0) {
 					/* stored successfully */
 					/* stored successfully */
 					r = field_stored(conn, path, file_size, fdh);
 					r = field_stored(conn, path, file_size, fdh);
-					/* TODO: return value */
+					if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+						/* Stop request handling */
+						break;
+					}
 				} else {
 				} else {
 					mg_cry_internal(conn,
 					mg_cry_internal(conn,
 					                "%s: Error saving file %s",
 					                "%s: Error saving file %s",
@@ -865,7 +886,14 @@ mg_handle_form_request(struct mg_connection *conn,
 					                        towrite,
 					                        towrite,
 					                        fdh);
 					                        fdh);
 					get_block++;
 					get_block++;
-					/* TODO: return value */
+					if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+						/* Stop request handling */
+						break;
+					}
+					if (r == MG_FORM_FIELD_HANDLE_NEXT) {
+						/* Skip to next field */
+						field_storage = MG_FORM_FIELD_STORAGE_SKIP;
+					}
 				}
 				}
 
 
 				if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
 				if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
@@ -922,7 +950,14 @@ mg_handle_form_request(struct mg_connection *conn,
 				                        hend,
 				                        hend,
 				                        towrite,
 				                        towrite,
 				                        fdh);
 				                        fdh);
-				/* TODO: return value */
+				if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+					/* Stop request handling */
+					break;
+				}
+				if (r == MG_FORM_FIELD_HANDLE_NEXT) {
+					/* Skip to next field */
+					field_storage = MG_FORM_FIELD_STORAGE_SKIP;
+				}
 			}
 			}
 
 
 			if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
 			if (field_storage == MG_FORM_FIELD_STORAGE_STORE) {
@@ -942,7 +977,10 @@ mg_handle_form_request(struct mg_connection *conn,
 						if (r == 0) {
 						if (r == 0) {
 							/* stored successfully */
 							/* stored successfully */
 							r = field_stored(conn, path, file_size, fdh);
 							r = field_stored(conn, path, file_size, fdh);
-							/* TODO: return value */
+							if (r == MG_FORM_FIELD_HANDLE_ABORT) {
+								/* Stop request handling */
+								break;
+							}
 						} else {
 						} else {
 							mg_cry_internal(conn,
 							mg_cry_internal(conn,
 							                "%s: Error saving file %s",
 							                "%s: Error saving file %s",