Skip to content

Commit 2c70351

Browse files
committed
mod_proxy: restore reuse of ProxyRemote connections when possible.
Fixes a regression from 2.4.59 (r1913907). For a reverse proxy setup with a worker (enablereuse=on) and a forward/CONNECT ProxyRemote to reach it, an open connection/tunnel to/through the remote proxy for the same origin server (and using the same proxy auth) should be reusable. Avoid closing them like r1913534 did. * modules/proxy/proxy_util.c: Rename the struct to remote_connect_info since it's only used for connecting through remote CONNECT proxies. Axe the use_http_connect field, always true. * modules/proxy/proxy_util.c(ap_proxy_connection_reusable): Remote CONNECT (forward) proxy connections can be reused if the auth and origin server infos are the same, so conn->forward != NULL is not a condition to prevent reusability. * modules/proxy/proxy_util.c(ap_proxy_determine_connection): Fix the checks around conn->forward reuse and connection cleanup if that's not possible.
1 parent b8de59a commit 2c70351

File tree

1 file changed

+59
-67
lines changed

1 file changed

+59
-67
lines changed

modules/proxy/proxy_util.c

Lines changed: 59 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@
4545
APLOG_USE_MODULE(proxy);
4646

4747
/*
48-
* Opaque structure containing target server info when
49-
* using a forward proxy.
50-
* Up to now only used in combination with HTTP CONNECT to ProxyRemote
48+
* Opaque structure containing infos for CONNECT-ing an origin server through a
49+
* remote (forward) proxy. Saved in the (opaque) proxy_conn_rec::forward pointer
50+
* field for backend connections kept alive, allowing for reuse when subsequent
51+
* requests should be routed through the same remote proxy.
5152
*/
5253
typedef struct {
53-
int use_http_connect; /* Use SSL Tunneling via HTTP CONNECT */
54-
const char *target_host; /* Target hostname */
55-
apr_port_t target_port; /* Target port */
5654
const char *proxy_auth; /* Proxy authorization */
57-
} forward_info;
55+
const char *target_host; /* Target/origin hostname */
56+
apr_port_t target_port; /* Target/origin port */
57+
} remote_connect_info;
5858

5959
/*
6060
* Opaque structure containing a refcounted and TTL'ed address.
@@ -1608,9 +1608,7 @@ PROXY_DECLARE(int) ap_proxy_connection_reusable(proxy_conn_rec *conn)
16081608
{
16091609
proxy_worker *worker = conn->worker;
16101610

1611-
return !(conn->close
1612-
|| conn->forward
1613-
|| worker->s->disablereuse);
1611+
return !(conn->close || worker->s->disablereuse);
16141612
}
16151613

16161614
static proxy_conn_rec *connection_make(apr_pool_t *p, proxy_worker *worker)
@@ -3329,12 +3327,10 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
33293327
uri->scheme, conn->uds_path, conn->hostname, conn->port);
33303328
}
33313329
else {
3330+
remote_connect_info *connect_info = NULL;
33323331
const char *hostname = uri->hostname;
33333332
apr_port_t hostport = uri->port;
33343333

3335-
/* Not a remote CONNECT until further notice */
3336-
conn->forward = NULL;
3337-
33383334
if (proxyname) {
33393335
hostname = proxyname;
33403336
hostport = proxyport;
@@ -3345,7 +3341,6 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
33453341
* sending our actual HTTPS requests.
33463342
*/
33473343
if (conn->is_ssl) {
3348-
forward_info *forward;
33493344
const char *proxy_auth;
33503345

33513346
/* Do we want to pass Proxy-Authorization along?
@@ -3364,13 +3359,15 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
33643359
proxy_auth = NULL;
33653360
}
33663361

3367-
/* Reset forward info if they changed */
3368-
if (!(forward = conn->forward)
3369-
|| forward->target_port != uri->port
3370-
|| ap_cstr_casecmp(forward->target_host, uri->hostname) != 0
3371-
|| (forward->proxy_auth != NULL) != (proxy_auth != NULL)
3372-
|| (forward->proxy_auth != NULL && proxy_auth != NULL &&
3373-
strcmp(forward->proxy_auth, proxy_auth) != 0)) {
3362+
/* Save our real backend data for using it later during HTTP CONNECT */
3363+
connect_info = conn->forward;
3364+
if (!connect_info
3365+
/* reset connect info if they changed */
3366+
|| connect_info->target_port != uri->port
3367+
|| ap_cstr_casecmp(connect_info->target_host, uri->hostname) != 0
3368+
|| (connect_info->proxy_auth != NULL) != (proxy_auth != NULL)
3369+
|| (connect_info->proxy_auth != NULL && proxy_auth != NULL &&
3370+
strcmp(connect_info->proxy_auth, proxy_auth) != 0)) {
33743371
apr_pool_t *fwd_pool = conn->pool;
33753372
if (worker->s->is_address_reusable) {
33763373
if (conn->fwd_pool) {
@@ -3381,26 +3378,24 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
33813378
}
33823379
fwd_pool = conn->fwd_pool;
33833380
}
3384-
forward = apr_pcalloc(fwd_pool, sizeof(forward_info));
3385-
conn->forward = forward;
3386-
3387-
/*
3388-
* Save our real backend data for using it later during HTTP CONNECT.
3389-
*/
3390-
forward->use_http_connect = 1;
3391-
forward->target_host = apr_pstrdup(fwd_pool, uri->hostname);
3392-
forward->target_port = uri->port;
3381+
connect_info = apr_pcalloc(fwd_pool, sizeof(*connect_info));
3382+
connect_info->target_host = apr_pstrdup(fwd_pool, uri->hostname);
3383+
connect_info->target_port = uri->port;
33933384
if (proxy_auth) {
3394-
forward->proxy_auth = apr_pstrdup(fwd_pool, proxy_auth);
3385+
connect_info->proxy_auth = apr_pstrdup(fwd_pool, proxy_auth);
33953386
}
3387+
conn->forward = NULL;
33963388
}
33973389
}
33983390
}
33993391

3400-
if (conn->hostname
3401-
&& (conn->port != hostport
3402-
|| ap_cstr_casecmp(conn->hostname, hostname) != 0)) {
3392+
/* Close the connection if the remote proxy or origin server don't match */
3393+
if (conn->forward != connect_info
3394+
|| (conn->hostname
3395+
&& (conn->port != hostport
3396+
|| ap_cstr_casecmp(conn->hostname, hostname) != 0))) {
34033397
conn_cleanup(conn);
3398+
conn->forward = connect_info;
34043399
}
34053400

34063401
/* Resolve the connection address with the determined hostname/port */
@@ -3444,9 +3439,8 @@ ap_proxy_determine_connection(apr_pool_t *p, request_rec *r,
34443439
if (dconf->preserve_host) {
34453440
ssl_hostname = r->hostname;
34463441
}
3447-
else if (conn->forward
3448-
&& ((forward_info *)(conn->forward))->use_http_connect) {
3449-
ssl_hostname = ((forward_info *)conn->forward)->target_host;
3442+
else if (conn->forward) {
3443+
ssl_hostname = ((remote_connect_info *)conn->forward)->target_host;
34503444
}
34513445
else {
34523446
ssl_hostname = conn->hostname;
@@ -3543,7 +3537,7 @@ PROXY_DECLARE(int) ap_proxy_is_socket_connected(apr_socket_t *sock)
35433537

35443538

35453539
/*
3546-
* Send a HTTP CONNECT request to a forward proxy.
3540+
* Send a HTTP CONNECT request to a remote proxy.
35473541
* The proxy is given by "backend", the target server
35483542
* is contained in the "forward" member of "backend".
35493543
*/
@@ -3556,24 +3550,24 @@ static apr_status_t send_http_connect(proxy_conn_rec *backend,
35563550
int complete = 0;
35573551
char buffer[HUGE_STRING_LEN];
35583552
char drain_buffer[HUGE_STRING_LEN];
3559-
forward_info *forward = (forward_info *)backend->forward;
3553+
remote_connect_info *connect_info = backend->forward;
35603554
int len = 0;
35613555

35623556
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00948)
35633557
"CONNECT: sending the CONNECT request for %s:%d "
35643558
"to the remote proxy %pI (%s)",
3565-
forward->target_host, forward->target_port,
3559+
connect_info->target_host, connect_info->target_port,
35663560
backend->addr, backend->hostname);
35673561
/* Create the CONNECT request */
35683562
nbytes = apr_snprintf(buffer, sizeof(buffer),
35693563
"CONNECT %s:%d HTTP/1.0" CRLF,
3570-
forward->target_host, forward->target_port);
3564+
connect_info->target_host, connect_info->target_port);
35713565
/* Add proxy authorization from the configuration, or initial
35723566
* request if necessary */
3573-
if (forward->proxy_auth != NULL) {
3567+
if (connect_info->proxy_auth != NULL) {
35743568
nbytes += apr_snprintf(buffer + nbytes, sizeof(buffer) - nbytes,
35753569
"Proxy-Authorization: %s" CRLF,
3576-
forward->proxy_auth);
3570+
connect_info->proxy_auth);
35773571
}
35783572
/* Set a reasonable agent and send everything */
35793573
nbytes += apr_snprintf(buffer + nbytes, sizeof(buffer) - nbytes,
@@ -3617,7 +3611,7 @@ static apr_status_t send_http_connect(proxy_conn_rec *backend,
36173611
char code_str[4];
36183612

36193613
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(00949)
3620-
"send_http_connect: response from the forward proxy: %s",
3614+
"send_http_connect: response from the remote proxy: %s",
36213615
buffer);
36223616

36233617
/* Extract the returned code */
@@ -3628,7 +3622,7 @@ static apr_status_t send_http_connect(proxy_conn_rec *backend,
36283622
}
36293623
else {
36303624
ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(00950)
3631-
"send_http_connect: the forward proxy returned code is '%s'",
3625+
"send_http_connect: the remote proxy returned code is '%s'",
36323626
code_str);
36333627
status = APR_INCOMPLETE;
36343628
}
@@ -3792,7 +3786,7 @@ PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
37923786
{
37933787
apr_status_t rv;
37943788
int loglevel;
3795-
forward_info *forward = conn->forward;
3789+
remote_connect_info *connect_info = conn->forward;
37963790
apr_sockaddr_t *backend_addr;
37973791
/* the local address to use for the outgoing connection */
37983792
apr_sockaddr_t *local_addr;
@@ -3993,27 +3987,25 @@ PROXY_DECLARE(int) ap_proxy_connect_backend(const char *proxy_function,
39933987

39943988
conn->sock = newsock;
39953989

3996-
if (forward && forward->use_http_connect) {
3997-
/*
3998-
* For HTTP CONNECT we need to prepend CONNECT request before
3999-
* sending our actual HTTPS requests.
4000-
*/
4001-
{
4002-
rv = send_http_connect(conn, s);
4003-
/* If an error occurred, loop round and try again */
4004-
if (rv != APR_SUCCESS) {
4005-
conn->sock = NULL;
4006-
apr_socket_close(newsock);
4007-
loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
4008-
ap_log_error(APLOG_MARK, loglevel, rv, s, APLOGNO(00958)
4009-
"%s: attempt to connect to %s:%hu "
4010-
"via http CONNECT through %pI (%s:%hu) failed",
4011-
proxy_function,
4012-
forward->target_host, forward->target_port,
4013-
backend_addr, conn->hostname, conn->port);
4014-
backend_addr = backend_addr->next;
4015-
continue;
4016-
}
3990+
/*
3991+
* For HTTP CONNECT we need to prepend CONNECT request before
3992+
* sending our actual HTTPS requests.
3993+
*/
3994+
if (connect_info) {
3995+
rv = send_http_connect(conn, s);
3996+
/* If an error occurred, loop round and try again */
3997+
if (rv != APR_SUCCESS) {
3998+
conn->sock = NULL;
3999+
apr_socket_close(newsock);
4000+
loglevel = backend_addr->next ? APLOG_DEBUG : APLOG_ERR;
4001+
ap_log_error(APLOG_MARK, loglevel, rv, s, APLOGNO(00958)
4002+
"%s: attempt to connect to %s:%hu "
4003+
"via http CONNECT through %pI (%s:%hu) failed",
4004+
proxy_function,
4005+
connect_info->target_host, connect_info->target_port,
4006+
backend_addr, conn->hostname, conn->port);
4007+
backend_addr = backend_addr->next;
4008+
continue;
40174009
}
40184010
}
40194011
}

0 commit comments

Comments
 (0)