Skip to content
Merged
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
61 changes: 45 additions & 16 deletions src/coroutine/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ bool Socket::socks5_handshake()

bool Socket::http_proxy_handshake()
{
#define HTTP_PROXY_FMT \
"CONNECT %.*s:%d HTTP/1.1\r\n" \
"Host: %.*s:%d\r\n" \
"User-Agent: Swoole/" SWOOLE_VERSION "\r\n" \
"Proxy-Connection: Keep-Alive\r\n" \

swString *buffer = get_read_buffer();

if (!buffer)
{
return false;
}

//CONNECT
int n;
if (http_proxy->password)
Expand All @@ -357,36 +370,49 @@ bool Socket::http_proxy_handshake()
);
swBase64_encode((unsigned char *) auth_buf, n, encode_buf);
n = sw_snprintf(
http_proxy->buf, sizeof(http_proxy->buf),
"CONNECT %.*s:%d HTTP/1.1\r\nProxy-Authorization:Basic %s\r\n\r\n",
http_proxy->l_target_host, http_proxy->target_host, http_proxy->target_port, encode_buf
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->l_target_host, http_proxy->target_host, http_proxy->target_port,
encode_buf
);
}
else
{
n = sw_snprintf(
http_proxy->buf, sizeof(http_proxy->buf),
"CONNECT %.*s:%d HTTP/1.1\r\n\r\n",
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
);
}

swTraceLog(SW_TRACE_HTTP_CLIENT, "proxy request: <<EOF\n%.*sEOF", n, http_proxy->buf);
swTraceLog(SW_TRACE_HTTP_CLIENT, "proxy request: <<EOF\n%.*sEOF", n, buffer->str);

if (send(http_proxy->buf, n) != n)
if (send(buffer->str, n) != n)
{
return false;
}

n = recv(http_proxy->buf, sizeof(http_proxy->buf));
/* use eof protocol (provisional) */
bool ori_open_eof_check = open_eof_check;
uint8_t ori_package_eof_len = protocol.package_eof_len;
char ori_package_eof[SW_DATA_EOF_MAXLEN + 1];
memcpy(ori_package_eof, SW_STRS(protocol.package_eof));
open_eof_check = true;
protocol.package_eof_len = sizeof("\r\n\r\n") - 1;
memcpy(protocol.package_eof, SW_STRS("\r\n\r\n"));

n = recv_packet();
if (n <= 0)
{
return false;
}

swTraceLog(SW_TRACE_HTTP_CLIENT, "proxy response: <<EOF\n%.*sEOF", n, http_proxy->buf);
swTraceLog(SW_TRACE_HTTP_CLIENT, "proxy response: <<EOF\n%.*sEOF", n, buffer->str);

char *buf = http_proxy->buf;
bool ret = false;
char *buf = buffer->str;
int len = n;
int state = 0;
char *p = buf;
Expand Down Expand Up @@ -433,16 +459,19 @@ bool Socket::http_proxy_handshake()
{
if (strncasecmp(p, SW_STRL("Connection established")) == 0)
{
return true;
}
else
{
break;
ret = true;
}
break;
}
}
}
return false;

/* revert protocol settings */
open_eof_check = ori_open_eof_check;
protocol.package_eof_len = ori_package_eof_len;
memcpy(protocol.package_eof, SW_STRS(ori_package_eof));

return ret;
}

void Socket::init_sock_type(enum swSocket_type _sw_type)
Expand Down
2 changes: 1 addition & 1 deletion swoole_client_coro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ bool php_swoole_client_set(Socket *cli, zval *zset)
if (php_swoole_array_get_value(vht, "http_proxy_port", ztmp))
{
php_swoole_client_coro_socket_free_http_proxy(cli);
cli->http_proxy = (struct _http_proxy*) ecalloc(1, sizeof(struct _http_proxy));
cli->http_proxy = (struct _http_proxy*) ecalloc(1, sizeof(*cli->http_proxy) - sizeof(cli->http_proxy->buf));
cli->http_proxy->proxy_host = estrdup(host.val());
cli->http_proxy->proxy_port = zval_get_long(ztmp);
if (php_swoole_array_get_value(vht, "http_proxy_username", ztmp) || php_swoole_array_get_value(vht, "http_proxy_user", ztmp))
Expand Down