-
Notifications
You must be signed in to change notification settings - Fork 8.1k
sapi/cli: support Expect 100-continue in PHP dev server #23245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sjord
wants to merge
8
commits into
php:master
Choose a base branch
from
Sjord:cli-server-expect-100-continue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−1
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
346683b
sapi/cli: support Expect 100-continue in PHP dev server
Sjord 3eb541a
Move test to sapi/cli
Sjord 04b41a4
Add expect/continue test based on sockets
Sjord a6447a7
Add error handling
Sjord bb71cfe
Check return value of php_cli_server_client_send_through
Sjord 2f0506f
Clean up test
Sjord 0d4332a
Move code around
Sjord e51a45e
Only send continue on HTTP/1.1
Sjord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
sapi/cli/tests/php_cli_server_expect_100_continue_curl.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
Sjord marked this conversation as resolved.
|
||
| ?> | ||
| --EXPECT-- | ||
| int(0) | ||
| Did the PHP development server send a HTTP/1.1 100 Continue header? | ||
| bool(true) | ||
33 changes: 33 additions & 0 deletions
33
sapi/cli/tests/php_cli_server_expect_100_continue_iua.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --TEST-- | ||
| Failure to send "100 Continue" is reported with ignore_user_abort=1 | ||
| --SKIPIF-- | ||
| <?php | ||
| include "skipif.inc"; | ||
| if (!extension_loaded("sockets")) die("skip sockets extension required"); | ||
| if (PHP_OS_FAMILY === "Windows") die("skip SO_LINGER reset behaviour differs on Windows"); | ||
| ?> | ||
| --FILE-- | ||
| <?php | ||
| include "php_cli_server.inc"; | ||
| $server = php_cli_server_start('echo "Hello world";', 'index.php', ['-d', 'ignore_user_abort=1']); | ||
|
|
||
| $fp = fsockopen(PHP_CLI_SERVER_HOSTNAME, PHP_CLI_SERVER_PORT); | ||
| socket_set_option(socket_import_stream($fp), SOL_SOCKET, SO_LINGER, ['l_onoff' => 1, 'l_linger' => 0]); | ||
| fwrite($fp, "POST / HTTP/1.1\r\nExpect: 100-continue\r\nContent-Length: 4\r\n\r\n"); | ||
| fclose($fp); | ||
|
|
||
| $output = ''; | ||
| for ($i = 0; $i < 100 && !str_contains($output, 'Invalid request'); $i++) { | ||
| usleep(50000); | ||
| $output = file_get_contents($server->outputFile); | ||
| } | ||
|
|
||
| var_dump(str_contains($output, 'Invalid request'), str_contains($output, 'Unexpected EOF')); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| @unlink(__DIR__ . '/php_cli_server_expect_100_continue_iua.log') | ||
| ?> | ||
| --EXPECT-- | ||
| bool(true) | ||
| bool(false) |
86 changes: 86 additions & 0 deletions
86
sapi/cli/tests/php_cli_server_expect_100_continue_socket.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| --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 on HTTP/1.0.\n"; | ||
| $fp = php_cli_server_connect(); | ||
| fwrite($fp, "POST / HTTP/1.0\r\nExpect: 100-continue\r\nContent-Length: 4\r\nConnection: close\r\n\r\n"); | ||
| $read = [$fp]; | ||
| var_dump(stream_select($read, $write, $except, 0, 1000)); | ||
| 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 on HTTP/1.0. | ||
| int(0) | ||
| HTTP/1.0 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.