Переглянути джерело

Stop form handler after read timeout

Fixes #682
bel2125 6 роки тому
батько
коміт
3c2c25ded2
1 змінених файлів з 17 додано та 17 видалено
  1. 17 17
      src/handle_form.inl

+ 17 - 17
src/handle_form.inl

@@ -19,7 +19,6 @@
  * THE SOFTWARE.
  * THE SOFTWARE.
  */
  */
 
 
-
 static int
 static int
 url_encoded_field_found(const struct mg_connection *conn,
 url_encoded_field_found(const struct mg_connection *conn,
                         const char *key,
                         const char *key,
@@ -83,7 +82,6 @@ url_encoded_field_found(const struct mg_connection *conn,
 	return ret;
 	return ret;
 }
 }
 
 
-
 static int
 static int
 url_encoded_field_get(const struct mg_connection *conn,
 url_encoded_field_get(const struct mg_connection *conn,
                       const char *key,
                       const char *key,
@@ -121,7 +119,6 @@ url_encoded_field_get(const struct mg_connection *conn,
 	return ret;
 	return ret;
 }
 }
 
 
-
 static int
 static int
 unencoded_field_get(const struct mg_connection *conn,
 unencoded_field_get(const struct mg_connection *conn,
                     const char *key,
                     const char *key,
@@ -138,7 +135,6 @@ unencoded_field_get(const struct mg_connection *conn,
 	return fdh->field_get(key_dec, value, value_len, fdh->user_data);
 	return fdh->field_get(key_dec, value, value_len, fdh->user_data);
 }
 }
 
 
-
 static int
 static int
 field_stored(const struct mg_connection *conn,
 field_stored(const struct mg_connection *conn,
              const char *path,
              const char *path,
@@ -152,7 +148,6 @@ field_stored(const struct mg_connection *conn,
 	return fdh->field_store(path, file_size, fdh->user_data);
 	return fdh->field_store(path, file_size, fdh->user_data);
 }
 }
 
 
-
 static const char *
 static const char *
 search_boundary(const char *buf,
 search_boundary(const char *buf,
                 size_t buf_len,
                 size_t buf_len,
@@ -174,7 +169,6 @@ search_boundary(const char *buf,
 	return NULL;
 	return NULL;
 }
 }
 
 
-
 int
 int
 mg_handle_form_request(struct mg_connection *conn,
 mg_handle_form_request(struct mg_connection *conn,
                        struct mg_form_data_handler *fdh)
                        struct mg_form_data_handler *fdh)
@@ -384,7 +378,7 @@ mg_handle_form_request(struct mg_connection *conn,
 
 
 				size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
 				size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
 				r = mg_read(conn, buf + (size_t)buf_fill, to_read);
 				r = mg_read(conn, buf + (size_t)buf_fill, to_read);
-				if (r < 0) {
+				if ((r < 0) || ((r == 0) && all_data_read)) {
 					/* read error */
 					/* read error */
 					return -1;
 					return -1;
 				}
 				}
@@ -507,7 +501,7 @@ mg_handle_form_request(struct mg_connection *conn,
 
 
 						size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
 						size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
 						r = mg_read(conn, buf + (size_t)buf_fill, to_read);
 						r = mg_read(conn, buf + (size_t)buf_fill, to_read);
-						if (r < 0) {
+						if ((r < 0) || ((r == 0) && all_data_read)) {
 							/* read error */
 							/* read error */
 							if (fstore.access.fp) {
 							if (fstore.access.fp) {
 								mg_fclose(&fstore.access);
 								mg_fclose(&fstore.access);
@@ -579,6 +573,7 @@ mg_handle_form_request(struct mg_connection *conn,
 		const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
 		const char *content_disp, *hend, *fbeg, *fend, *nbeg, *nend;
 		const char *next;
 		const char *next;
 		unsigned part_no;
 		unsigned part_no;
+		int all_data_read = 0;
 
 
 		memset(&part_header, 0, sizeof(part_header));
 		memset(&part_header, 0, sizeof(part_header));
 
 
@@ -649,15 +644,18 @@ mg_handle_form_request(struct mg_connection *conn,
 		for (part_no = 0;; part_no++) {
 		for (part_no = 0;; part_no++) {
 			size_t towrite, fnlen, n;
 			size_t towrite, fnlen, n;
 			int get_block;
 			int get_block;
+			size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill;
 
 
-			r = mg_read(conn,
-			            buf + (size_t)buf_fill,
-			            sizeof(buf) - 1 - (size_t)buf_fill);
-			if (r < 0) {
+			r = mg_read(conn, buf + (size_t)buf_fill, to_read);
+			if ((r < 0) || ((r == 0) && all_data_read)) {
 				/* read error */
 				/* read error */
 				mg_free(boundary);
 				mg_free(boundary);
 				return -1;
 				return -1;
 			}
 			}
+			if (r != (int)to_read) {
+				all_data_read = 1;
+			}
+
 			buf_fill += r;
 			buf_fill += r;
 			buf[buf_fill] = 0;
 			buf[buf_fill] = 0;
 			if (buf_fill < 1) {
 			if (buf_fill < 1) {
@@ -922,10 +920,9 @@ mg_handle_form_request(struct mg_connection *conn,
 				hend = buf;
 				hend = buf;
 
 
 				/* Read new data */
 				/* Read new data */
-				r = mg_read(conn,
-				            buf + (size_t)buf_fill,
-				            sizeof(buf) - 1 - (size_t)buf_fill);
-				if (r < 0) {
+				to_read = sizeof(buf) - 1 - (size_t)buf_fill;
+				r = mg_read(conn, buf + (size_t)buf_fill, to_read);
+				if ((r < 0) || ((r == 0) && all_data_read)) {
 					/* read error */
 					/* read error */
 					if (fstore.access.fp) {
 					if (fstore.access.fp) {
 						mg_fclose(&fstore.access);
 						mg_fclose(&fstore.access);
@@ -934,6 +931,10 @@ mg_handle_form_request(struct mg_connection *conn,
 					mg_free(boundary);
 					mg_free(boundary);
 					return -1;
 					return -1;
 				}
 				}
+				if (r != (int)to_read) {
+					all_data_read = 1;
+				}
+
 				buf_fill += r;
 				buf_fill += r;
 				buf[buf_fill] = 0;
 				buf[buf_fill] = 0;
 				/* buf_fill is at least 8 here */
 				/* buf_fill is at least 8 here */
@@ -1018,5 +1019,4 @@ mg_handle_form_request(struct mg_connection *conn,
 	return -1;
 	return -1;
 }
 }
 
 
-
 /* End of handle_form.inl */
 /* End of handle_form.inl */