Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ typedef struct php_cli_server_client {
php_http_parser parser;
bool request_read;
bool too_large_post;
bool expect_continue;
zend_string *current_header_name;
zend_string *current_header_value;
enum { HEADER_NONE=0, HEADER_FIELD, HEADER_VALUE } last_header_element;
Expand Down Expand Up @@ -1791,6 +1792,12 @@ static int php_cli_server_client_read_request_on_headers_complete(php_http_parse
return 2;
}

zval *expect_val = zend_hash_str_find(&client->request.headers, "expect", sizeof("expect") - 1);
if (expect_val && Z_TYPE_P(expect_val) == IS_STRING
&& zend_string_equals_literal_ci(Z_STR_P(expect_val), "100-continue")) {
client->expect_continue = true;
}

return 0;
}

Expand Down Expand Up @@ -1886,6 +1893,26 @@ static int php_cli_server_client_read_request(php_cli_server_client *client, cha
}
client->parser.data = client;
nbytes_consumed = php_http_parser_execute(&client->parser, &settings, buf, nbytes_read);
if (client->expect_continue && !client->request_read) {
/* Parser completed headers with Expect: 100-continue but hasn't
* finished reading the body. Send 100 Continue before the client
* sends the request body. */
smart_str buffer = { 0 };
bool send_failed = true;
append_http_status_line(&buffer, client->parser.http_major * 100 + client->parser.http_minor, 100, 0);
smart_str_appendl(&buffer, "\r\n", 2);
smart_str_0(&buffer);
zend_try {
php_cli_server_client_send_through(client, ZSTR_VAL(buffer.s), ZSTR_LEN(buffer.s));
send_failed = false;
} zend_end_try();
smart_str_free(&buffer);
client->expect_continue = false;
if (send_failed) {
*errstr = php_socket_strerror(php_socket_errno(), NULL, 0);
return -1;
}
}
if (nbytes_consumed != (size_t)nbytes_read && !client->too_large_post) {
if (php_cli_server_log_level >= PHP_CLI_SERVER_LOG_ERROR) {
if ((buf[0] & 0x80) /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
Expand Down Expand Up @@ -1981,6 +2008,7 @@ static void php_cli_server_client_ctor(php_cli_server_client *client, php_cli_se
php_http_parser_init(&client->parser, PHP_HTTP_REQUEST);
client->request_read = false;
client->too_large_post = false;
client->expect_continue = false;

client->last_header_element = HEADER_NONE;
client->current_header_name = NULL;
Expand Down
35 changes: 35 additions & 0 deletions sapi/cli/tests/php_cli_server_expect_100_continue_curl.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
Expect 100-continue behavior in PHP development server (curl)
--SKIPIF--
<?php
include "skipif.inc";
?>
--EXTENSIONS--
curl
--FILE--
<?php
include 'php_cli_server.inc';
$server = php_cli_server_start();

// Generate a POST body larger than 1MB to trigger Expect: 100-continue
$body = str_repeat('A', 1024 * 1024 + 1);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, PHP_CLI_SERVER_ADDRESS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// Set a high timeout for 100-continue response
curl_setopt($ch, CURLOPT_EXPECT_100_TIMEOUT_MS, 2000);

curl_exec($ch);
var_dump(curl_errno($ch));

echo "Did the PHP development server send a HTTP/1.1 100 Continue header?\n";
$start_transfer_time = curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME_T);
var_dump($start_transfer_time < 1_000_000);
Comment thread
Sjord marked this conversation as resolved.
?>
--EXPECT--
int(0)
Did the PHP development server send a HTTP/1.1 100 Continue header?
bool(true)
74 changes: 74 additions & 0 deletions sapi/cli/tests/php_cli_server_expect_100_continue_socket.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--TEST--
Expect 100-continue behavior in PHP development server (sockets)
--SKIPIF--
<?php
include "skipif.inc";
?>
--FILE--
<?php
include "php_cli_server.inc";
php_cli_server_start();

echo "# Send Expect: 100-continue header, receive 100 Continue response.\n";
$fp = php_cli_server_connect();
fwrite($fp, "POST / HTTP/1.1\r\nExpect: 100-continue\r\nContent-Length: 4\r\nConnection: close\r\n\r\n");
echo fgets($fp);
echo fgets($fp);
fwrite($fp, "body");
echo fgets($fp);
fclose($fp);

echo "# Send Expect: 100-continue header and disconnect.\n";
$fp = php_cli_server_connect();
if (extension_loaded('sockets')) {
// Set SO_LINGER timeout to zero so that send fails on the server immediately
socket_set_option(
socket_import_stream($fp),
SOL_SOCKET,
SO_LINGER,
[
'l_onoff' => 1,
'l_linger' => 0,
]
);
}
stream_socket_shutdown($fp, STREAM_SHUT_RD);
fwrite($fp, "POST / HTTP/1.1\r\nExpect: 100-continue\r\nContent-Length: 4\r\nConnection: close\r\n\r\n");
fclose($fp);

$fp = php_cli_server_connect();
fwrite($fp, "GET / HTTP/1.1\r\nConnection: close\r\n\r\n");
echo fgets($fp);
fclose($fp);

echo "# GET with Expect header (no body).\n";
$fp = php_cli_server_connect();
fwrite($fp, "GET / HTTP/1.1\r\nExpect: 100-continue\r\nConnection: close\r\n\r\n");
echo fgets($fp);
fclose($fp);

echo "# POST with empty body.\n";
$fp = php_cli_server_connect();
fwrite($fp, "POST / HTTP/1.1\r\nExpect: 100-continue\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
echo fgets($fp);
fclose($fp);

echo "# Lower-case expect header.\n";
$fp = php_cli_server_connect();
fwrite($fp, "POST / HTTP/1.1\r\nexpect: 100-continue\r\nContent-Length: 4\r\nConnection: close\r\n\r\n");
echo fgets($fp);
fclose($fp);
?>
--EXPECT--
# Send Expect: 100-continue header, receive 100 Continue response.
HTTP/1.1 100 Continue

HTTP/1.1 200 OK
# Send Expect: 100-continue header and disconnect.
HTTP/1.1 200 OK
# GET with Expect header (no body).
HTTP/1.1 200 OK
# POST with empty body.
HTTP/1.1 200 OK
# Lower-case expect header.
HTTP/1.1 100 Continue
Loading