Skip to content

Commit 27542db

Browse files
nikcorgsmalyshev
authored andcommitted
Respond with 501 to unknown request methods
Fixed typo Moved 501 response from dispatch to event_read_request Return return value of send_error_page
1 parent 56425ee commit 27542db

File tree

5 files changed

+75
-22
lines changed

5 files changed

+75
-22
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2012, PHP 5.4.8
44

5+
- CLI server:
6+
. Changed response to unknown HTTP method to 501 according to RFC.
7+
(Niklas Lindgren).
8+
59
- Core:
610
. Fixed bug #63093 (Segfault while load extension failed in zts-build).
711
(Laruence)

sapi/cli/php_cli_server.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ typedef struct php_cli_server_poller {
116116
} php_cli_server_poller;
117117

118118
typedef struct php_cli_server_request {
119-
enum php_http_method request_method;
119+
enum php_http_method request_method;
120120
int protocol_version;
121121
char *request_uri;
122122
size_t request_uri_len;
@@ -247,7 +247,8 @@ static php_cli_server_http_reponse_status_code_pair status_map[] = {
247247
static php_cli_server_http_reponse_status_code_pair template_map[] = {
248248
{ 400, "<h1 class=\"h\">%s</h1><p>Your browser sent a request that this server could not understand.</p>" },
249249
{ 404, "<h1 class=\"h\">%s</h1><p>The requested resource %s was not found on this server.</p>" },
250-
{ 500, "<h1 class=\"h\">%s</h1><p>The server is temporality unavaiable.</p>" }
250+
{ 500, "<h1 class=\"h\">%s</h1><p>The server is temporarily unavailable.</p>" },
251+
{ 501, "<h1 class=\"h\">%s</h1><p>Request method not supported.</p>" }
251252
};
252253

253254
static php_cli_server_ext_mime_type_pair mime_type_map[] = {
@@ -275,7 +276,7 @@ static void php_cli_server_log_response(php_cli_server_client *client, int statu
275276

276277
ZEND_DECLARE_MODULE_GLOBALS(cli_server);
277278

278-
/* {{{ static char php_cli_server_css[]
279+
/* {{{ static char php_cli_server_css[]
279280
* copied from ext/standard/info.c
280281
*/
281282
static const char php_cli_server_css[] = "<style type=\"text/css\">\n" \
@@ -543,7 +544,7 @@ static void sapi_cli_server_register_variable(zval *track_vars_array, const char
543544
}
544545
} /* }}} */
545546

546-
static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
547+
static int sapi_cli_server_register_entry_cb(char **entry TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */ {
547548
zval *track_vars_array = va_arg(args, zval *);
548549
if (hash_key->nKeyLength) {
549550
char *real_key, *key;
@@ -583,7 +584,7 @@ static void sapi_cli_server_register_variables(zval *track_vars_array TSRMLS_DC)
583584
} else {
584585
sapi_cli_server_register_variable(track_vars_array, "REMOTE_ADDR", client->addr_str TSRMLS_CC);
585586
}
586-
}
587+
}
587588
{
588589
char *tmp;
589590
spprintf(&tmp, 0, "PHP %s Development Server", PHP_VERSION);
@@ -681,7 +682,7 @@ sapi_module_struct cli_server_sapi_module = {
681682
sapi_cli_server_log_message, /* Log message */
682683
NULL, /* Get request time */
683684
NULL, /* Child terminate */
684-
685+
685686
STANDARD_SAPI_MODULE_PROPERTIES
686687
}; /* }}} */
687688

@@ -778,7 +779,7 @@ static int php_cli_server_poller_iter_on_active(php_cli_server_poller *poller, v
778779
}
779780
}
780781
}
781-
782+
782783
#else
783784
php_socket_t fd = 0;
784785
const php_socket_t max_fd = poller->max_fd;
@@ -966,7 +967,7 @@ static int php_cli_server_content_sender_send(php_cli_server_content_sender *sen
966967
} else if (nbytes_sent == chunk->data.immortal.len) {
967968
php_cli_server_chunk_dtor(chunk);
968969
pefree(chunk, 1);
969-
sender->buffer.first = next;
970+
sender->buffer.first = next;
970971
if (!next) {
971972
sender->buffer.last = NULL;
972973
}
@@ -1345,7 +1346,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
13451346
}
13461347
}
13471348
break; /* regular file */
1348-
}
1349+
}
13491350
if (prev_path) {
13501351
pefree(prev_path, 1);
13511352
*q = DEFAULT_SLASH;
@@ -1384,7 +1385,7 @@ static void php_cli_server_request_translate_vpath(php_cli_server_request *reque
13841385
if (request->vpath[i] == '\\') {
13851386
request->vpath[i] = '/';
13861387
}
1387-
}
1388+
}
13881389
}
13891390
#endif
13901391
request->sb = sb;
@@ -1452,7 +1453,7 @@ static void normalize_vpath(char **retval, size_t *retval_len, const char *vpath
14521453
}
14531454
}
14541455
}
1455-
1456+
14561457
*decoded_vpath_end = '\0';
14571458
*retval = decoded_vpath;
14581459
*retval_len = decoded_vpath_end - decoded_vpath;
@@ -1812,7 +1813,7 @@ static int php_cli_server_send_error_page(php_cli_server *server, php_cli_server
18121813
smart_str_append_generic_ex(&buffer, php_cli_server_buffer_size(&client->content_sender.buffer), 1, size_t, _unsigned);
18131814
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
18141815
smart_str_appendl_ex(&buffer, "\r\n", 2, 1);
1815-
1816+
18161817
chunk = php_cli_server_chunk_heap_new(buffer.c, buffer.c, buffer.len);
18171818
if (!chunk) {
18181819
smart_str_free_ex(&buffer, 1);
@@ -1917,7 +1918,7 @@ static int php_cli_server_begin_send_static(php_cli_server *server, php_cli_serv
19171918
}
19181919
/* }}} */
19191920

1920-
static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
1921+
static int php_cli_server_request_startup(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) { /* {{{ */
19211922
char **auth;
19221923
php_cli_server_client_populate_request_info(client, &SG(request_info));
19231924
if (SUCCESS == zend_hash_find(&client->request.headers, "Authorization", sizeof("Authorization"), (void**)&auth)) {
@@ -1942,8 +1943,8 @@ static int php_cli_server_request_shutdown(php_cli_server *server, php_cli_serve
19421943
SG(server_context) = NULL;
19431944
SG(rfc1867_uploaded_files) = NULL;
19441945
return SUCCESS;
1945-
}
1946-
/* }}} */
1946+
}
1947+
/* }}} */
19471948

19481949
static int php_cli_server_dispatch_router(php_cli_server *server, php_cli_server_client *client TSRMLS_DC) /* {{{ */
19491950
{
@@ -2002,7 +2003,7 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
20022003
destroy_request_info(&SG(request_info));
20032004
return SUCCESS;
20042005
}
2005-
}
2006+
}
20062007

20072008
if (server->router) {
20082009
if (!php_cli_server_dispatch_router(server, client TSRMLS_CC)) {
@@ -2016,7 +2017,7 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
20162017
|| SUCCESS != php_cli_server_send_error_page(server, client, 500 TSRMLS_CC)) {
20172018
php_cli_server_request_shutdown(server, client TSRMLS_CC);
20182019
return SUCCESS;
2019-
}
2020+
}
20202021
} else {
20212022
if (server->router) {
20222023
static int (*send_header_func)(sapi_headers_struct * TSRMLS_DC);
@@ -2029,7 +2030,7 @@ static int php_cli_server_dispatch(php_cli_server *server, php_cli_server_client
20292030
sapi_module.send_headers = send_header_func;
20302031
SG(sapi_headers).send_default_content_type = 1;
20312032
SG(rfc1867_uploaded_files) = NULL;
2032-
}
2033+
}
20332034
if (SUCCESS != php_cli_server_begin_send_static(server, client TSRMLS_CC)) {
20342035
php_cli_server_close_connection(server, client TSRMLS_CC);
20352036
}
@@ -2191,6 +2192,8 @@ static int php_cli_server_recv_event_read_request(php_cli_server *server, php_cl
21912192
efree(errstr);
21922193
php_cli_server_close_connection(server, client TSRMLS_CC);
21932194
return FAILURE;
2195+
} else if (status == 1 && client->request.request_method == PHP_HTTP_NOT_IMPLEMENTED) {
2196+
return php_cli_server_send_error_page(server, client, 501 TSRMLS_CC);
21942197
} else if (status == 1) {
21952198
php_cli_server_poller_remove(&server->poller, POLLIN, client->sock);
21962199
php_cli_server_dispatch(server, client TSRMLS_CC);
@@ -2311,7 +2314,7 @@ static void php_cli_server_do_event_for_each_fd(php_cli_server *server, int(*rha
23112314
static int php_cli_server_do_event_loop(php_cli_server *server TSRMLS_DC) /* {{{ */
23122315
{
23132316
int retval = SUCCESS;
2314-
while (server->is_running) {
2317+
while (server->is_running) {
23152318
static const struct timeval tv = { 1, 0 };
23162319
int n = php_cli_server_poller_poll(&server->poller, &tv);
23172320
if (n > 0) {

sapi/cli/php_http_parser.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ static const char *method_strings[] =
9999
, "NOTIFY"
100100
, "SUBSCRIBE"
101101
, "UNSUBSCRIBE"
102+
, "NOTIMPLEMENTED"
102103
};
103104

104105

@@ -589,7 +590,7 @@ size_t php_http_parser_execute (php_http_parser *parser,
589590
case 'S': parser->method = PHP_HTTP_SUBSCRIBE; break;
590591
case 'T': parser->method = PHP_HTTP_TRACE; break;
591592
case 'U': parser->method = PHP_HTTP_UNLOCK; /* or UNSUBSCRIBE */ break;
592-
default: goto error;
593+
default: parser->method = PHP_HTTP_NOT_IMPLEMENTED; break;
593594
}
594595
state = s_req_method;
595596
break;
@@ -602,7 +603,7 @@ size_t php_http_parser_execute (php_http_parser *parser,
602603
goto error;
603604

604605
matcher = method_strings[parser->method];
605-
if (ch == ' ' && matcher[index] == '\0') {
606+
if (ch == ' ' && (matcher[index] == '\0' || parser->method == PHP_HTTP_NOT_IMPLEMENTED)) {
606607
state = s_req_spaces_before_url;
607608
} else if (ch == matcher[index]) {
608609
; /* nada */
@@ -631,7 +632,7 @@ size_t php_http_parser_execute (php_http_parser *parser,
631632
} else if (index == 4 && parser->method == PHP_HTTP_PROPFIND && ch == 'P') {
632633
parser->method = PHP_HTTP_PROPPATCH;
633634
} else {
634-
goto error;
635+
parser->method = PHP_HTTP_NOT_IMPLEMENTED;
635636
}
636637

637638
++index;

sapi/cli/php_http_parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ enum php_http_method
102102
, PHP_HTTP_NOTIFY
103103
, PHP_HTTP_SUBSCRIBE
104104
, PHP_HTTP_UNSUBSCRIBE
105+
/* unknown, not implemented */
106+
, PHP_HTTP_NOT_IMPLEMENTED
105107
};
106108

107109

sapi/cli/tests/bug61679.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Bug #61679 (Error on non-standard HTTP methods)
3+
--SKIPIF--
4+
<?php
5+
include "skipif.inc";
6+
?>
7+
--FILE--
8+
<?php
9+
include "php_cli_server.inc";
10+
php_cli_server_start(<<<'PHP'
11+
echo "This should never echo";
12+
PHP
13+
);
14+
15+
list($host, $port) = explode(':', PHP_CLI_SERVER_ADDRESS);
16+
$port = intval($port)?:80;
17+
18+
$fp = fsockopen($host, $port, $errno, $errstr, 0.5);
19+
if (!$fp) {
20+
die("connect failed");
21+
}
22+
23+
// Send a request with a fictitious request method,
24+
// I like smurfs, the smurf everything.
25+
if(fwrite($fp, <<<HEADER
26+
SMURF / HTTP/1.1
27+
Host: {$host}
28+
29+
30+
HEADER
31+
)) {
32+
while (!feof($fp)) {
33+
echo fgets($fp);
34+
// Only echo the first line from the response,
35+
// the rest is not interesting
36+
break;
37+
}
38+
}
39+
40+
fclose($fp);
41+
?>
42+
--EXPECTF--
43+
HTTP/1.1 501 Not Implemented

0 commit comments

Comments
 (0)