Skip to content

Commit 4f50905

Browse files
vedranmileticMarin Martuslović
authored andcommitted
Respond without body to HEAD request on a static resource
Co-authored-by: Marin Martuslović <[email protected]>
1 parent ca011bb commit 4f50905

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

sapi/cli/php_cli_server.c

+48-40
Original file line numberDiff line numberDiff line change
@@ -1980,49 +1980,51 @@ static zend_result php_cli_server_send_error_page(php_cli_server *server, php_cl
19801980
php_cli_server_content_sender_ctor(&client->content_sender);
19811981
client->content_sender_initialized = true;
19821982

1983-
escaped_request_uri = php_escape_html_entities_ex((const unsigned char *) ZSTR_VAL(client->request.request_uri), ZSTR_LEN(client->request.request_uri), 0, ENT_QUOTES, NULL, /* double_encode */ 0, /* quiet */ 0);
1983+
if (client->request.request_method != PHP_HTTP_HEAD) {
1984+
escaped_request_uri = php_escape_html_entities_ex((const unsigned char *) ZSTR_VAL(client->request.request_uri), ZSTR_LEN(client->request.request_uri), 0, ENT_QUOTES, NULL, /* double_encode */ 0, /* quiet */ 0);
19841985

1985-
{
1986-
static const char prologue_template[] = "<!doctype html><html><head><title>%d %s</title>";
1987-
php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(strlen(prologue_template) + 3 + strlen(status_string) + 1);
1988-
if (!chunk) {
1989-
goto fail;
1986+
{
1987+
static const char prologue_template[] = "<!doctype html><html><head><title>%d %s</title>";
1988+
php_cli_server_chunk *chunk = php_cli_server_chunk_heap_new_self_contained(strlen(prologue_template) + 3 + strlen(status_string) + 1);
1989+
if (!chunk) {
1990+
goto fail;
1991+
}
1992+
snprintf(chunk->data.heap.p, chunk->data.heap.len, prologue_template, status, status_string);
1993+
chunk->data.heap.len = strlen(chunk->data.heap.p);
1994+
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
19901995
}
1991-
snprintf(chunk->data.heap.p, chunk->data.heap.len, prologue_template, status, status_string);
1992-
chunk->data.heap.len = strlen(chunk->data.heap.p);
1993-
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
1994-
}
1995-
{
1996-
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(php_cli_server_css, sizeof(php_cli_server_css) - 1);
1997-
if (!chunk) {
1998-
goto fail;
1996+
{
1997+
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(php_cli_server_css, sizeof(php_cli_server_css) - 1);
1998+
if (!chunk) {
1999+
goto fail;
2000+
}
2001+
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
19992002
}
2000-
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
2001-
}
2002-
{
2003-
static const char template[] = "</head><body>";
2004-
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(template, sizeof(template) - 1);
2005-
if (!chunk) {
2006-
goto fail;
2003+
{
2004+
static const char template[] = "</head><body>";
2005+
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(template, sizeof(template) - 1);
2006+
if (!chunk) {
2007+
goto fail;
2008+
}
2009+
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
20072010
}
2008-
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
2009-
}
2010-
{
2011-
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);
2012-
if (!chunk) {
2013-
goto fail;
2011+
{
2012+
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);
2013+
if (!chunk) {
2014+
goto fail;
2015+
}
2016+
snprintf(chunk->data.heap.p, chunk->data.heap.len, content_template, status_string, ZSTR_VAL(escaped_request_uri));
2017+
chunk->data.heap.len = strlen(chunk->data.heap.p);
2018+
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
20142019
}
2015-
snprintf(chunk->data.heap.p, chunk->data.heap.len, content_template, status_string, ZSTR_VAL(escaped_request_uri));
2016-
chunk->data.heap.len = strlen(chunk->data.heap.p);
2017-
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
2018-
}
2019-
{
2020-
static const char epilogue_template[] = "</body></html>";
2021-
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(epilogue_template, sizeof(epilogue_template) - 1);
2022-
if (!chunk) {
2023-
goto fail;
2020+
{
2021+
static const char epilogue_template[] = "</body></html>";
2022+
php_cli_server_chunk *chunk = php_cli_server_chunk_immortal_new(epilogue_template, sizeof(epilogue_template) - 1);
2023+
if (!chunk) {
2024+
goto fail;
2025+
}
2026+
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
20242027
}
2025-
php_cli_server_buffer_append(&client->content_sender.buffer, chunk);
20262028
}
20272029

20282030
{
@@ -2053,14 +2055,18 @@ static zend_result php_cli_server_send_error_page(php_cli_server *server, php_cl
20532055
if (errstr) {
20542056
pefree(errstr, 1);
20552057
}
2056-
zend_string_free(escaped_request_uri);
2058+
if (escaped_request_uri) {
2059+
zend_string_free(escaped_request_uri);
2060+
}
20572061
return SUCCESS;
20582062

20592063
fail:
20602064
if (errstr) {
20612065
pefree(errstr, 1);
20622066
}
2063-
zend_string_free(escaped_request_uri);
2067+
if (escaped_request_uri) {
2068+
zend_string_free(escaped_request_uri);
2069+
}
20642070
return FAILURE;
20652071
} /* }}} */
20662072

@@ -2115,7 +2121,9 @@ static zend_result php_cli_server_begin_send_static(php_cli_server *server, php_
21152121

21162122
php_cli_server_content_sender_ctor(&client->content_sender);
21172123
client->content_sender_initialized = true;
2118-
client->file_fd = fd;
2124+
if (client->request.request_method != PHP_HTTP_HEAD) {
2125+
client->file_fd = fd;
2126+
}
21192127

21202128
{
21212129
php_cli_server_chunk *chunk;

sapi/cli/tests/php_cli_server_013.phpt

-3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,3 @@ Date: %s
9393
Connection: close
9494
Content-Type: text/html; charset=UTF-8
9595
Content-Length: %d
96-
97-
<!doctype html><html><head><title>404 Not Found</title><style>AAA</style>
98-
</head><body><h1>Not Found</h1><p>The requested resource <code class="url">/main/foo/bar</code> was not found on this server.</p></body></html>

0 commit comments

Comments
 (0)