33
33
#include "php_variables.h"
34
34
#include "rfc1867.h"
35
35
#include "ext/standard/php_string.h"
36
+ #include "ext/standard/php_smart_str.h"
36
37
37
38
#define DEBUG_FILE_UPLOAD ZEND_DEBUG
38
39
@@ -398,8 +399,9 @@ static int find_boundary(multipart_buffer *self, char *boundary TSRMLS_DC)
398
399
static int multipart_buffer_headers (multipart_buffer * self , zend_llist * header TSRMLS_DC )
399
400
{
400
401
char * line ;
401
- mime_header_entry prev_entry = {0 }, entry ;
402
- int prev_len , cur_len ;
402
+ mime_header_entry entry = {0 };
403
+ smart_str buf_value = {0 };
404
+ char * key = NULL ;
403
405
404
406
/* didn't find boundary, abort */
405
407
if (!find_boundary (self , self -> boundary TSRMLS_CC )) {
@@ -411,11 +413,10 @@ static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header T
411
413
while ( (line = get_line (self TSRMLS_CC )) && strlen (line ) > 0 )
412
414
{
413
415
/* add header to table */
414
- char * key = line ;
415
416
char * value = NULL ;
416
417
417
418
if (php_rfc1867_encoding_translation (TSRMLS_C )) {
418
- self -> input_encoding = zend_multibyte_encoding_detector (line , strlen (line ), self -> detect_order , self -> detect_order_size TSRMLS_CC );
419
+ self -> input_encoding = zend_multibyte_encoding_detector (( unsigned char * ) line , strlen (line ), self -> detect_order , self -> detect_order_size TSRMLS_CC );
419
420
}
420
421
421
422
/* space in the beginning means same header */
@@ -424,31 +425,33 @@ static int multipart_buffer_headers(multipart_buffer *self, zend_llist *header T
424
425
}
425
426
426
427
if (value ) {
427
- * value = 0 ;
428
- do { value ++ ; } while (isspace (* value ));
429
-
430
- entry .value = estrdup (value );
431
- entry .key = estrdup (key );
432
-
433
- } else if (zend_llist_count (header )) { /* If no ':' on the line, add to previous line */
434
-
435
- prev_len = strlen (prev_entry .value );
436
- cur_len = strlen (line );
437
-
438
- entry .value = emalloc (prev_len + cur_len + 1 );
439
- memcpy (entry .value , prev_entry .value , prev_len );
440
- memcpy (entry .value + prev_len , line , cur_len );
441
- entry .value [cur_len + prev_len ] = '\0' ;
428
+ if (buf_value .c && key ) {
429
+ /* new entry, add the old one to the list */
430
+ smart_str_0 (& buf_value );
431
+ entry .key = key ;
432
+ entry .value = buf_value .c ;
433
+ zend_llist_add_element (header , & entry );
434
+ buf_value .c = NULL ;
435
+ key = NULL ;
436
+ }
442
437
443
- entry .key = estrdup (prev_entry .key );
438
+ * value = '\0' ;
439
+ do { value ++ ; } while (isspace (* value ));
444
440
445
- zend_llist_remove_tail (header );
441
+ key = estrdup (line );
442
+ smart_str_appends (& buf_value , value );
443
+ } else if (buf_value .c ) { /* If no ':' on the line, add to previous line */
444
+ smart_str_appends (& buf_value , line );
446
445
} else {
447
446
continue ;
448
447
}
449
-
448
+ }
449
+ if (buf_value .c && key ) {
450
+ /* add the last one to the list */
451
+ smart_str_0 (& buf_value );
452
+ entry .key = key ;
453
+ entry .value = buf_value .c ;
450
454
zend_llist_add_element (header , & entry );
451
- prev_entry = entry ;
452
455
}
453
456
454
457
return 1 ;
@@ -884,7 +887,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
884
887
if (count == PG (max_input_vars ) + 1 ) {
885
888
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Input variables exceeded %ld. To increase the limit change max_input_vars in php.ini." , PG (max_input_vars ));
886
889
}
887
-
890
+
888
891
if (php_rfc1867_callback != NULL ) {
889
892
multipart_event_formdata event_formdata ;
890
893
0 commit comments