@@ -174,6 +174,7 @@ typedef struct php_cli_server_client {
174174 zend_string * addr_str ;
175175 php_http_parser parser ;
176176 bool request_read ;
177+ bool too_large_post ;
177178 zend_string * current_header_name ;
178179 zend_string * current_header_value ;
179180 enum { HEADER_NONE = 0 , HEADER_FIELD , HEADER_VALUE } last_header_element ;
@@ -209,6 +210,7 @@ static const php_cli_server_http_response_status_code_pair template_map[] = {
209210 { 400 , "<h1>%s</h1><p>Your browser sent a request that this server could not understand.</p>" },
210211 { 404 , "<h1>%s</h1><p>The requested resource <code class=\"url\">%s</code> was not found on this server.</p>" },
211212 { 405 , "<h1>%s</h1><p>Requested method not allowed.</p>" },
213+ { 413 , "<h1>%s</h1><p>The request body exceeds the configured <code>post_max_size</code> of " ZEND_LONG_FMT " bytes.</p>" },
212214 { 500 , "<h1>%s</h1><p>The server is temporarily unavailable.</p>" },
213215 { 501 , "<h1>%s</h1><p>Request method not supported.</p>" }
214216};
@@ -1779,6 +1781,16 @@ static int php_cli_server_client_read_request_on_headers_complete(php_http_parse
17791781 break ;
17801782 }
17811783 client -> last_header_element = HEADER_NONE ;
1784+
1785+ if (parser -> content_length > 0
1786+ && SG (post_max_size ) > 0
1787+ && (zend_long ) parser -> content_length > SG (post_max_size )) {
1788+ client -> request .protocol_version = parser -> http_major * 100 + parser -> http_minor ;
1789+ client -> too_large_post = true;
1790+ client -> request_read = true;
1791+ return 2 ;
1792+ }
1793+
17821794 return 0 ;
17831795}
17841796
@@ -1866,7 +1878,7 @@ static int php_cli_server_client_read_request(php_cli_server_client *client, cha
18661878 }
18671879 client -> parser .data = client ;
18681880 nbytes_consumed = php_http_parser_execute (& client -> parser , & settings , buf , nbytes_read );
1869- if (nbytes_consumed != (size_t )nbytes_read ) {
1881+ if (nbytes_consumed != (size_t )nbytes_read && ! client -> too_large_post ) {
18701882 if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR ) {
18711883 if ((buf [0 ] & 0x80 ) /* SSLv2 */ || buf [0 ] == 0x16 /* SSLv3/TLSv1 */ ) {
18721884 * errstr = estrdup ("Unsupported SSL request" );
@@ -1960,6 +1972,7 @@ static void php_cli_server_client_ctor(php_cli_server_client *client, php_cli_se
19601972
19611973 php_http_parser_init (& client -> parser , PHP_HTTP_REQUEST );
19621974 client -> request_read = false;
1975+ client -> too_large_post = false;
19631976
19641977 client -> last_header_element = HEADER_NONE ;
19651978 client -> current_header_name = NULL ;
@@ -2038,11 +2051,20 @@ static zend_result php_cli_server_send_error_page(php_cli_server *server, php_cl
20382051 php_cli_server_buffer_append (& client -> content_sender .buffer , chunk );
20392052 }
20402053 {
2041- php_cli_server_chunk * chunk = php_cli_server_chunk_heap_new_self_contained (strlen (content_template ) + ZSTR_LEN (escaped_request_uri ) + 3 + strlen (status_string ) + 1 );
2042- if (!chunk ) {
2043- goto fail ;
2054+ php_cli_server_chunk * chunk ;
2055+ if (status == 413 ) {
2056+ chunk = php_cli_server_chunk_heap_new_self_contained (strlen (content_template ) + strlen (status_string ) + MAX_LENGTH_OF_LONG + 1 );
2057+ if (!chunk ) {
2058+ goto fail ;
2059+ }
2060+ snprintf (chunk -> data .heap .p , chunk -> data .heap .len , content_template , status_string , SG (post_max_size ));
2061+ } else {
2062+ chunk = php_cli_server_chunk_heap_new_self_contained (strlen (content_template ) + ZSTR_LEN (escaped_request_uri ) + 3 + strlen (status_string ) + 1 );
2063+ if (!chunk ) {
2064+ goto fail ;
2065+ }
2066+ snprintf (chunk -> data .heap .p , chunk -> data .heap .len , content_template , status_string , ZSTR_VAL (escaped_request_uri ));
20442067 }
2045- snprintf (chunk -> data .heap .p , chunk -> data .heap .len , content_template , status_string , ZSTR_VAL (escaped_request_uri ));
20462068 chunk -> data .heap .len = strlen (chunk -> data .heap .p );
20472069 php_cli_server_buffer_append (& client -> content_sender .buffer , chunk );
20482070 }
@@ -2641,6 +2663,9 @@ static zend_result php_cli_server_recv_event_read_request(php_cli_server *server
26412663 if (client -> request .request_method == PHP_HTTP_NOT_IMPLEMENTED ) {
26422664 return php_cli_server_send_error_page (server , client , 501 );
26432665 }
2666+ if (client -> too_large_post ) {
2667+ return php_cli_server_send_error_page (server , client , 413 );
2668+ }
26442669 php_cli_server_poller_remove (& server -> poller , POLLIN , client -> sock );
26452670 return php_cli_server_dispatch (server , client );
26462671 case 0 :
0 commit comments