Skip to content
Draft
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
38 changes: 38 additions & 0 deletions ext/standard/tests/http/http_auth_userinfo.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
http wrapper attaches an Authorization: Basic header when the URL contains userinfo
--SKIPIF--
<?php require_once 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require_once 'server.inc';

$responses = [
"data://text/plain,HTTP/1.0 204 No Content\r\n\r\n",
"data://text/plain,HTTP/1.0 204 No Content\r\n\r\n",
];
['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);

$host = parse_url($uri, PHP_URL_HOST);
$port = parse_url($uri, PHP_URL_PORT);

file_get_contents("http://user@$host:$port/path");
file_get_contents("http://user:pass@$host:$port/path");

rewind($output);
$requests = stream_get_contents($output);
if (!preg_match_all('~Authorization: .*~', $requests, $matches)) {
die('fail No authorization headers found');
}

foreach ($matches[0] as $header) {
echo $header . "\n";
}

http_server_kill($pid);

?>
--EXPECT--
Authorization: Basic dXNlcjo=
Authorization: Basic dXNlcjpwYXNz
61 changes: 61 additions & 0 deletions ext/standard/tests/http/http_auto_decode.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
http.auto_decode stream context option controls chunked response decoding
--SKIPIF--
<?php require 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$chunked = "data://text/plain,HTTP/1.1 200 OK\r\n"
. "Transfer-Encoding: chunked\r\n\r\n"
. "2\r\nab\r\n2\r\ncd\r\n0\r\n\r\n";

$responses = [$chunked, $chunked, $chunked];
['pid' => $pid, 'uri' => $uri] = http_server($responses);

function test_auto_decode($auto_decode) {
global $uri;
$ctx = null;
if ($auto_decode !== null) {
$ctx = stream_context_create(['http' => ['auto_decode' => $auto_decode]]);
}
$body = file_get_contents($uri, false, $ctx);

$has_te = false;
foreach (http_get_last_response_headers() as $h) {
if (stripos($h, 'Transfer-Encoding:') === 0) {
$has_te = true;
break;
}
}

return [addcslashes($body, "\r\n"), $has_te];
}

var_dump(test_auto_decode(null));
var_dump(test_auto_decode(true));
var_dump(test_auto_decode(false));

http_server_kill($pid);
?>
--EXPECT--
array(2) {
[0]=>
string(4) "abcd"
[1]=>
bool(false)
}
array(2) {
[0]=>
string(4) "abcd"
[1]=>
bool(false)
}
array(2) {
[0]=>
string(31) "2\r\nab\r\n2\r\ncd\r\n0\r\n\r\n"
[1]=>
bool(true)
}
35 changes: 35 additions & 0 deletions ext/standard/tests/http/http_location_realloc.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
HTTP response with two Location headers (second longer) triggers erealloc
--SKIPIF--
<?php require 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$responses = array(
"data://text/plain,HTTP/1.1 301 Moved Permanently\r\nLocation: /short\r\nLocation: /a_much_longer_path_than_short\r\nContent-Length: 0\r\n\r\n",
);

['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);

$ctx = stream_context_create(['http' => ['follow_location' => 0]]);
$result = file_get_contents("$uri/", false, $ctx);
var_dump($result);
var_dump(http_get_last_response_headers());

http_server_kill($pid);
?>
--EXPECT--
string(0) ""
array(4) {
[0]=>
string(30) "HTTP/1.1 301 Moved Permanently"
[1]=>
string(16) "Location: /short"
[2]=>
string(40) "Location: /a_much_longer_path_than_short"
[3]=>
string(17) "Content-Length: 0"
}
72 changes: 72 additions & 0 deletions ext/standard/tests/http/http_proxy_auth_removed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
--TEST--
Proxy-Authorization header removed from request after CONNECT tunnel
--EXTENSIONS--
openssl
--SKIPIF--
<?php require_once 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require_once 'server.inc';

$server = http_server_init($output);

if (is_resource($server)) {
$conn = stream_socket_accept($server);

/* Read CONNECT request */
$req = '';
while (!str_contains($req, "\r\n\r\n")) {
$req .= fread($conn, 1024);
}

echo "CONNECT contains Proxy-Authorization: ";
var_dump(stripos($req, 'Proxy-Authorization:') !== false);

fwrite($conn, "HTTP/1.1 200 Connection established\r\n\r\n");
fflush($conn);

stream_context_set_option($conn, 'ssl', 'local_cert', __DIR__ . '/../../../openssl/tests/sni_server.pem');
stream_socket_enable_crypto($conn, true, STREAM_CRYPTO_METHOD_TLS_SERVER) or die('fail TLS handshake');

/* Read tunneled request */
$req2 = '';
while (!str_contains($req2, "\r\n\r\n")) {
$req2 .= fread($conn, 1024);
}

/* Must be removed */
echo "Proxied request contains Proxy-Authorization: ";
var_dump(stripos($req2, 'Proxy-Authorization:') !== false);

fwrite($conn,
"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
);

exit;
}

$host = parse_url($server['uri'], PHP_URL_HOST);
$port = parse_url($server['uri'], PHP_URL_PORT);

$ctx = stream_context_create([
'http' => [
'proxy' => "tcp://$host:$port",
'header' => [
"Proxy-Authorization: Basic Zm9vOmJhcg==",
],
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);

file_get_contents("https://www.php.net/", false, $ctx);

http_server_kill($server['pid']);
?>
--EXPECT--
CONNECT contains Proxy-Authorization: bool(true)
Proxied request contains Proxy-Authorization: bool(false)
48 changes: 48 additions & 0 deletions ext/standard/tests/http/http_proxy_ssl_connect_01.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
HTTP proxy SSL CONNECT with Proxy-Authorization header (string, multi-line)
--EXTENSIONS--
openssl
--SKIPIF--
<?php require 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$responses = array(
"data://text/plain,HTTP/1.0 200 Connection established\r\n\r\n",
"data://text/plain,",
);

['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);

$host = parse_url($uri, PHP_URL_HOST);
$port = parse_url($uri, PHP_URL_PORT);

$ctx = stream_context_create([
'http' => [
'proxy' => "tcp://$host:$port",
'header' => "X-Custom: test\r\nProxy-Authorization: Basic dXNlcjpwYXNz",
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
@$result = file_get_contents("https://www.php.net/test", false, $ctx);
var_dump($result);

http_server_kill($pid);

rewind($output);
$request = stream_get_contents($output);
var_dump(str_contains($request, 'CONNECT www.php.net:443 HTTP/1.0'));
var_dump(str_contains($request, 'Proxy-Authorization: Basic dXNlcjpwYXNz'));
var_dump(str_contains($request, 'X-Custom: test'));
?>
--EXPECT--
bool(false)
bool(true)
bool(true)
bool(false)
46 changes: 46 additions & 0 deletions ext/standard/tests/http/http_proxy_ssl_connect_02.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
HTTP proxy SSL CONNECT without Proxy-Authorization header (FAILURE path)
--EXTENSIONS--
openssl
--SKIPIF--
<?php require 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$responses = array(
"data://text/plain,HTTP/1.0 200 Connection established\r\n\r\n",
"data://text/plain,",
);

['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);

$host = parse_url($uri, PHP_URL_HOST);
$port = parse_url($uri, PHP_URL_PORT);

$ctx = stream_context_create([
'http' => [
'proxy' => "tcp://$host:$port",
'header' => "X-Custom: test\r\nX-Other: value",
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
@$result = file_get_contents("https://www.php.net/test", false, $ctx);
var_dump($result);

http_server_kill($pid);

rewind($output);
$request = stream_get_contents($output);
var_dump(str_contains($request, 'CONNECT www.php.net:443 HTTP/1.0'));
var_dump(str_contains($request, 'Proxy-Authorization'));
?>
--EXPECT--
bool(false)
bool(true)
bool(false)
48 changes: 48 additions & 0 deletions ext/standard/tests/http/http_proxy_ssl_connect_03.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--TEST--
HTTP proxy SSL CONNECT with Proxy-Authorization header (array)
--EXTENSIONS--
openssl
--SKIPIF--
<?php require 'server.inc'; http_server_skipif(); ?>
--INI--
allow_url_fopen=1
--FILE--
<?php
require 'server.inc';

$responses = array(
"data://text/plain,HTTP/1.0 200 Connection established\r\n\r\n",
"data://text/plain,",
);

['pid' => $pid, 'uri' => $uri] = http_server($responses, $output);

$host = parse_url($uri, PHP_URL_HOST);
$port = parse_url($uri, PHP_URL_PORT);

$ctx = stream_context_create([
'http' => [
'proxy' => "tcp://$host:$port",
'header' => ["X-Custom: test", "Proxy-Authorization: Basic abc123"],
],
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
],
]);
@$result = file_get_contents("https://www.php.net/test", false, $ctx);
var_dump($result);

http_server_kill($pid);

rewind($output);
$request = stream_get_contents($output);
var_dump(str_contains($request, 'CONNECT www.php.net:443 HTTP/1.0'));
var_dump(str_contains($request, 'Proxy-Authorization: Basic abc123'));
var_dump(str_contains($request, 'X-Custom'));
?>
--EXPECT--
bool(false)
bool(true)
bool(true)
bool(false)
Loading
Loading