Skip to content

Commit

Permalink
Improve configuration parser about Host (#3343)
Browse files Browse the repository at this point in the history
* Improve configuration parser about Host

* Update
  • Loading branch information
twose authored May 22, 2020
1 parent 9df6be8 commit 7216f03
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/coroutine/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ bool Socket::http_proxy_handshake()

//CONNECT
int n;
const char *host = http_proxy->target_host;
int host_len = http_proxy->l_target_host;
#ifdef SW_USE_OPENSSL
if (open_ssl && ssl_option.tls_host_name)
{
host = ssl_option.tls_host_name;
host_len = strlen(ssl_option.tls_host_name);
}
#endif
if (http_proxy->password)
{
char auth_buf[256];
Expand All @@ -424,9 +433,9 @@ bool Socket::http_proxy_handshake()
swBase64_encode((unsigned char *) auth_buf, n, encode_buf);
n = sw_snprintf(
buffer->str, buffer->size,
HTTP_PROXY_FMT "Proxy-Authorization:Basic %s\r\n\r\n",
http_proxy->l_target_host, http_proxy->target_host, http_proxy->target_port,
HTTP_PROXY_FMT "Proxy-Authorization: Basic %s\r\n\r\n",
http_proxy->l_target_host, http_proxy->target_host, http_proxy->target_port,
host_len, host, http_proxy->target_port,
encode_buf
);
}
Expand All @@ -436,7 +445,7 @@ bool Socket::http_proxy_handshake()
buffer->str, buffer->size,
HTTP_PROXY_FMT "\r\n",
http_proxy->l_target_host, http_proxy->target_host, http_proxy->target_port,
http_proxy->l_target_host, http_proxy->target_host, http_proxy->target_port
host_len, host, http_proxy->target_port
);
}

Expand Down Expand Up @@ -776,7 +785,6 @@ bool Socket::connect(string _host, int _port, int flags)
#endif
if (socks5_proxy)
{
//enable socks5 proxy
socks5_proxy->target_host = sw_strndup((char *) _host.c_str(), _host.size());
socks5_proxy->l_target_host = _host.size();
socks5_proxy->target_port = _port;
Expand All @@ -786,7 +794,6 @@ bool Socket::connect(string _host, int _port, int flags)
}
else if (http_proxy)
{
//enable http proxy
http_proxy->target_host = sw_strndup((char *) _host.c_str(), _host.size());
http_proxy->l_target_host = _host.size();
http_proxy->target_port = _port;
Expand Down Expand Up @@ -824,7 +831,7 @@ bool Socket::connect(string _host, int _port, int flags)
if (!inet_pton(AF_INET, connect_host.c_str(), &socket->info.addr.inet_v4.sin_addr))
{
#ifdef SW_USE_OPENSSL
if (open_ssl)
if (open_ssl && !(socks5_proxy || http_proxy))
{
ssl_host_name = connect_host;
}
Expand Down Expand Up @@ -855,7 +862,7 @@ bool Socket::connect(string _host, int _port, int flags)
if (!inet_pton(AF_INET6, connect_host.c_str(), &socket->info.addr.inet_v6.sin6_addr))
{
#ifdef SW_USE_OPENSSL
if (open_ssl)
if (open_ssl && !(socks5_proxy || http_proxy))
{
ssl_host_name = connect_host;
}
Expand Down
24 changes: 13 additions & 11 deletions swoole_http_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,14 @@ bool http_client::send()
zcookies = sw_zend_read_property(swoole_http_client_coro_ce, zobject, ZEND_STRL("cookies"), 0);
z_download_file = sw_zend_read_property_not_null(swoole_http_client_coro_ce, zobject, ZEND_STRL("downloadFile"), 0);

// ============ host ============
zend::string str_host;

if ((ZVAL_IS_ARRAY(zheaders)) && ((zvalue = zend_hash_str_find(Z_ARRVAL_P(zheaders), ZEND_STRL("Host"))) || (zvalue = zend_hash_str_find(Z_ARRVAL_P(zheaders), ZEND_STRL("host")))))
{
str_host = zvalue;
}

// ============ download ============
if (z_download_file)
{
Expand Down Expand Up @@ -969,18 +977,13 @@ bool http_client::send()
if (socket->http_proxy)
#endif
{
zend::string str_host;
const static char *pre = "http://";
char *_host = (char *) host.c_str();
size_t _host_len = host.length();
if (ZVAL_IS_ARRAY(zheaders))
if (str_host.get())
{
if ((zvalue = zend_hash_str_find(Z_ARRVAL_P(zheaders), ZEND_STRL("Host"))))
{
str_host = zvalue;
_host = str_host.val();
_host_len = str_host.len();
}
_host = str_host.val();
_host_len = str_host.len();
}
size_t proxy_uri_len = path.length() + _host_len + strlen(pre) + 10;
char *proxy_uri = (char*) emalloc(proxy_uri_len);
Expand All @@ -1003,10 +1006,9 @@ bool http_client::send()

// As much as possible to ensure that Host is the first header.
// See: http://tools.ietf.org/html/rfc7230#section-5.4
if ((ZVAL_IS_ARRAY(zheaders)) && ((zvalue = zend_hash_str_find(Z_ARRVAL_P(zheaders), ZEND_STRL("Host"))) || (zvalue = zend_hash_str_find(Z_ARRVAL_P(zheaders), ZEND_STRL("host")))))
if (str_host.get())
{
zend::string str_value(zvalue);
http_client_swString_append_headers(buffer, ZEND_STRL("Host"), str_value.val(), str_value.len());
http_client_swString_append_headers(buffer, ZEND_STRL("Host"), str_host.val(), str_host.len());
}
else
{
Expand Down

0 comments on commit 7216f03

Please sign in to comment.