Skip to content

Commit

Permalink
Fix #3312 (#3313)
Browse files Browse the repository at this point in the history
* Fix #3312

* Fix build
  • Loading branch information
twose authored May 12, 2020
1 parent 9996328 commit 682448c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
15 changes: 14 additions & 1 deletion swoole_http2_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,20 @@ static ssize_t http2_client_build_header(zval *zobject, zval *zrequest, char *bu
}
if (!find_host)
{
headers.add(HTTP2_CLIENT_HOST_HEADER_INDEX,ZEND_STRL(":authority"), h2c->host.c_str(), h2c->host.length());
const std::string *host;
std::string _host;
#ifndef SW_USE_OPENSSL
if (h2c->port != 80)
#else
if (!h2c->ssl ? h2c->port != 80 : h2c->port != 443)
#endif
{
_host = cpp_string::format("%s:%d", h2c->host.c_str(), h2c->port);
host = &_host;
} else {
host = &h2c->host;
}
headers.add(HTTP2_CLIENT_HOST_HEADER_INDEX,ZEND_STRL(":authority"), host->c_str(), host->length());
}
// http cookies
if (ZVAL_IS_ARRAY(zcookies))
Expand Down
12 changes: 9 additions & 3 deletions swoole_http_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1011,12 +1011,18 @@ bool http_client::send()
else
{
// See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23
const std::string *_host = &host;
const std::string *_host;
std::string __host;
if (port != 80 && port != 443)
#ifndef SW_USE_OPENSSL
if (port != 80)
#else
if (!ssl ? port != 80 : port != 443)
#endif
{
__host = cpp_string::format("%s:%u", host.c_str(), port);
_host = &__host;
} else {
_host = &host;
}
http_client_swString_append_headers(buffer, ZEND_STRL("Host"), _host->c_str(), _host->length());
}
Expand Down Expand Up @@ -2235,4 +2241,4 @@ static PHP_METHOD(swoole_http_client_coro, getPeerCert)
http_client *phc = php_swoole_get_phc(ZEND_THIS);
phc->getpeercert(return_value);
}
#endif
#endif
67 changes: 67 additions & 0 deletions tests/swoole_http2_client_coro/host.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
--TEST--
swoole_http2_client_coro: host
--SKIPIF--
<?php
require __DIR__ . '/../include/skipif.inc';
?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';
$pm = new ProcessManager;
$pm->parentFunc = function ($pid) use ($pm) {
Co\run(function () use ($pm) {
$domain = '127.0.0.1';
$cli = new Swoole\Coroutine\Http2\Client($domain, $pm->getFreePort(), true);
$cli->set(['timeout' => 5]);
$cli->connect();
$request = new Swoole\Http2\Request;
for ($n = MAX_REQUESTS; $n--;) {
$request->path = '/';
$request->headers = [
'user-agent' => 'Chrome/49.0.2587.3',
'accept' => 'text/html,application/xhtml+xml,application/xml',
'connection' => 'keep-alive'
];
for ($i = 32; $i--;) {
$request->headers[md5(mt_rand(1, 65535))] = sha1(get_safe_random(32));
}
Assert::assert($cli->send($request));
$response = $cli->recv();
Assert::same($response->statusCode, 200);
Assert::same(json_encode($request->headers), $response->data);
unset(
$response->headers['host'],
$response->headers['server'],
$response->headers['date'],
$response->headers['content-type'],
$response->headers['content-length']
);
Assert::same($request->headers, $response->headers);
}
});
$pm->kill();
};
$pm->childFunc = function () use ($pm) {
$http = new swoole_http_server('127.0.0.1', $pm->getFreePort(), SWOOLE_BASE, SWOOLE_SOCK_TCP | SWOOLE_SSL);
$http->set([
// 'log_file' => '/dev/null',
'open_http2_protocol' => true,
'http_compression' => false,
'ssl_cert_file' => SSL_FILE_DIR . '/server.crt',
'ssl_key_file' => SSL_FILE_DIR . '/server.key'
]);
$http->on("WorkerStart", function () use ($pm) { $pm->wakeup(); });
$http->on("request", function (swoole_http_request $request, swoole_http_response $response) use ($http) {
Assert::same($request->header['host'], "{$http->host}:{$http->port}");
unset($request->header['host']);
foreach ($request->header as $name => $value) {
$response->header($name, $value);
}
$response->end(json_encode($request->header));
});
$http->start();
};
$pm->childFirst();
$pm->run();
?>
--EXPECTF--

0 comments on commit 682448c

Please sign in to comment.