Skip to content

Commit

Permalink
Fix httpclient proxy with host and port (#4124)
Browse files Browse the repository at this point in the history
* Fix proxy with host and port

* Change strstr to memchr
  • Loading branch information
Yurunsoft authored Apr 6, 2021
1 parent 6042d15 commit efb83c8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
6 changes: 5 additions & 1 deletion ext-src/swoole_http_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,11 @@ bool HttpClient::send() {
}
size_t proxy_uri_len = path.length() + _host_len + strlen(pre) + 10;
char *proxy_uri = (char *) emalloc(proxy_uri_len);
proxy_uri_len = sw_snprintf(proxy_uri, proxy_uri_len, "%s%s:%u%s", pre, _host, port, path.c_str());
if (nullptr == memchr(_host, ':', _host_len)) {
proxy_uri_len = sw_snprintf(proxy_uri, proxy_uri_len, "%s%s:%u%s", pre, _host, port, path.c_str());
} else {
proxy_uri_len = sw_snprintf(proxy_uri, proxy_uri_len, "%s%s%s", pre, _host, path.c_str());
}
buffer->append(proxy_uri, proxy_uri_len);
if (!socket->http_proxy->password.empty()) {
require_proxy_authentication = true;
Expand Down
50 changes: 50 additions & 0 deletions tests/swoole_http_client_coro/http_proxy_with_host_port.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--TEST--
swoole_http_client_coro: http client with http_proxy and host and port
--SKIPIF--
<?php
require __DIR__.'/../include/skipif.inc';
skip_if_no_http_proxy();
?>
--FILE--
<?php
require __DIR__.'/../include/bootstrap.php';

$pm = new SwooleTest\ProcessManager;
$pm->parentFunc = function () use ($pm) {
Co\Run(function () use ($pm) {
$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', $pm->getFreePort());
$cli->set([
'timeout' => 30,
'http_proxy_host' => HTTP_PROXY_HOST,
'http_proxy_port' => HTTP_PROXY_PORT,
]);
$cli->setHeaders([
'Host' => '127.0.0.1:'.$pm->getFreePort(),
]);
$result = $cli->get('/');
Assert::assert($result);
Assert::assert('Swoole' === $cli->body);
$cli->close();
$pm->kill();
echo "DONE\n";
});
};

$pm->childFunc = function () use ($pm) {
$server = new Swoole\Http\Server('0.0.0.0', $pm->getFreePort(), SWOOLE_BASE);
$server->set(['log_file' => '/dev/null']);
$server->on('workerStart', function () use ($pm) {
$pm->wakeup();
});
$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($pm) {
$response->end('Swoole');
});
$server->start();
};

$pm->childFirst();
$pm->run();

?>
--EXPECT--
DONE

0 comments on commit efb83c8

Please sign in to comment.