@@ -49,6 +49,7 @@ typedef struct ngx_http_modsecurity_ctx_s {
49
49
request_rec * req ;
50
50
ngx_chain_t * chain ;
51
51
ngx_buf_t buf ;
52
+ void * * loc_conf ;
52
53
unsigned request_body_in_single_buf :1 ;
53
54
unsigned request_body_in_file_only :1 ;
54
55
} ngx_http_modsecurity_ctx_t ;
@@ -393,7 +394,7 @@ modsecurity_read_body_cb(request_rec *r, char *outpos, unsigned int length,
393
394
rest -= size ;
394
395
buf -> file_pos += size ;
395
396
} else {
396
- return APR_ERROR ;
397
+ return -1 ;
397
398
}
398
399
}
399
400
@@ -443,14 +444,13 @@ static ngx_int_t
443
444
ngx_http_modsecurity_handler (ngx_http_request_t * r )
444
445
{
445
446
ngx_http_modsecurity_loc_conf_t * cf ;
446
- ngx_http_core_loc_conf_t * clcf ;
447
+ ngx_http_core_loc_conf_t * clcf , * lcf ;
447
448
ngx_http_modsecurity_ctx_t * ctx ;
448
- ngx_buf_t * buf ;
449
- size_t preread ;
450
449
ngx_list_part_t * part ;
451
450
ngx_table_elt_t * h ;
452
451
ngx_uint_t i ;
453
452
ngx_int_t rc ;
453
+ void * * loc_conf ;
454
454
455
455
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: handler" );
456
456
@@ -544,36 +544,38 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r)
544
544
if (r -> method == NGX_HTTP_POST ) {
545
545
/* Processing POST request body, should we process PUT? */
546
546
ngx_log_debug0 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 , "modSecurity: method POST" );
547
-
548
- /*
549
- * read client request body TODO: chunked request body
550
- */
551
547
552
548
clcf = ngx_http_get_module_loc_conf (r , ngx_http_core_module );
553
549
554
550
if (clcf == NULL ) {
555
551
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
556
552
}
557
-
558
- preread = r -> header_in -> last - r -> header_in -> pos ;
559
553
560
- /* force read_client_request_body to use r->header_in as body buffer */
561
- r -> headers_in .content_length_n ++ ;
562
-
563
- if ( (r -> header_in -> end - r -> header_in -> pos ) < r -> headers_in .content_length_n ) {
564
- buf = ngx_create_temp_buf (r -> pool , r -> headers_in .content_length_n );
565
- if (buf == NULL ) {
554
+ ctx -> loc_conf = r -> loc_conf ;
555
+ /* hijack loc_conf so that we can receive any body length
556
+ * TODO: nonblocking process & chuncked body
557
+ */
558
+ if (clcf -> client_body_buffer_size < r -> headers_in .content_length_n ) {
559
+
560
+ loc_conf = ngx_pcalloc (r -> pool , sizeof (void * ) * ngx_http_max_module );
561
+ if (loc_conf == NULL ) {
562
+ return NGX_HTTP_INTERNAL_SERVER_ERROR ;
563
+ }
564
+
565
+ lcf = ngx_pcalloc (r -> pool , sizeof (ngx_http_core_loc_conf_t ));
566
+ if (lcf == NULL ) {
566
567
return NGX_HTTP_INTERNAL_SERVER_ERROR ;
567
568
}
569
+
570
+ ngx_memcpy (loc_conf , r -> loc_conf , sizeof (void * ) * ngx_http_max_module );
571
+ ngx_memcpy (lcf , clcf , sizeof (ngx_http_core_loc_conf_t ));
568
572
569
- ngx_memcpy (buf -> pos + 1 , r -> header_in -> pos , preread );
570
- buf -> last += preread + 1 ;
571
- r -> header_in = buf ;
572
- * r -> header_in -> pos = 0xff ;
573
- } else {
574
- ngx_memmove (r -> header_in -> pos + 1 , r -> header_in -> pos , preread );
575
- r -> header_in -> last ++ ;
576
- * r -> header_in -> pos = 0xff ;
573
+ ctx -> loc_conf = r -> loc_conf ;
574
+ r -> loc_conf = loc_conf ;
575
+
576
+ ngx_http_get_module_loc_conf (r , ngx_http_core_module ) = lcf ;
577
+ clcf = ngx_http_get_module_loc_conf (r , ngx_http_core_module );
578
+ clcf -> client_body_buffer_size = r -> headers_in .content_length_n ;
577
579
}
578
580
579
581
ctx -> request_body_in_single_buf = r -> request_body_in_single_buf ;
@@ -582,7 +584,7 @@ ngx_http_modsecurity_handler(ngx_http_request_t *r)
582
584
r -> request_body_in_file_only = 0 ;
583
585
584
586
rc = ngx_http_read_client_request_body (r , ngx_http_modsecurity_request_body_handler );
585
- if (rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
587
+ if (rc >= NGX_HTTP_SPECIAL_RESPONSE ) {
586
588
return rc ;
587
589
}
588
590
@@ -608,19 +610,18 @@ ngx_http_modsecurity_request_body_handler(ngx_http_request_t *r)
608
610
609
611
if (ctx == NULL
610
612
|| r -> request_body -> bufs == NULL
611
- || * r -> request_body -> bufs -> buf -> pos != 0xff ) {
613
+ || r -> request_body -> bufs -> next != NULL ) {
612
614
ngx_http_finalize_request (r , NGX_HTTP_INTERNAL_SERVER_ERROR );
613
615
return ;
614
616
}
615
-
617
+
616
618
r -> request_body_in_single_buf = ctx -> request_body_in_single_buf ;
617
619
r -> request_body_in_file_only = ctx -> request_body_in_file_only ;
618
-
619
- r -> request_body -> bufs -> buf -> pos ++ ;
620
- r -> headers_in .content_length_n -- ;
621
-
620
+ r -> header_in = r -> request_body -> bufs -> buf ;
622
621
ctx -> chain = r -> request_body -> bufs ;
623
-
622
+ r -> request_body = NULL ;
623
+ r -> loc_conf = ctx -> loc_conf ;
624
+
624
625
ngx_http_finalize_request (r , ngx_http_modsecurity_pass_to_backend (r ));
625
626
return ;
626
627
}
0 commit comments