From: Stephan Bosch Date: Wed, 27 Apr 2016 10:00:25 +0000 (+0200) Subject: lib-http: client: Added ability to configure a specific proxy for individual requests. X-Git-Tag: 2.2.27~230 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=09d628cf9ba8b96753d1dac7fab62d2878c955c1;p=thirdparty%2Fdovecot%2Fcore.git lib-http: client: Added ability to configure a specific proxy for individual requests. This way, a request can be routed to a specific proxy (or origin server). The destination can also be a unix socket. --- diff --git a/src/lib-http/http-client-request.c b/src/lib-http/http-client-request.c index 82702e5b3c..813cb1218b 100644 --- a/src/lib-http/http-client-request.c +++ b/src/lib-http/http-client-request.c @@ -454,6 +454,26 @@ void http_client_request_set_auth_simple(struct http_client_request *req, req->password = p_strdup(req->pool, password); } +void http_client_request_set_proxy_url(struct http_client_request *req, + const struct http_url *proxy_url) +{ + i_assert(req->state == HTTP_REQUEST_STATE_NEW || + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + + req->host_url = http_url_clone_authority(req->pool, proxy_url); + req->host_socket = NULL; +} + +void http_client_request_set_proxy_socket(struct http_client_request *req, + const char *proxy_socket) +{ + i_assert(req->state == HTTP_REQUEST_STATE_NEW || + req->state == HTTP_REQUEST_STATE_GOT_RESPONSE); + + req->host_socket = p_strdup(req->pool, proxy_socket); + req->host_url = NULL; +} + void http_client_request_delay_until(struct http_client_request *req, time_t time) { @@ -542,7 +562,8 @@ static void http_client_request_do_submit(struct http_client_request *req) struct http_client_host *host; const char *proxy_socket_path = client->set.proxy_socket_path; const struct http_url *proxy_url = client->set.proxy_url; - bool have_proxy = (proxy_socket_path != NULL) || (proxy_url != NULL); + bool have_proxy = (proxy_socket_path != NULL) || (proxy_url != NULL) || + (req->host_socket != NULL) || (req->host_url != NULL); const char *authority, *target; i_assert(req->state == HTTP_REQUEST_STATE_NEW); @@ -559,8 +580,12 @@ static void http_client_request_do_submit(struct http_client_request *req) /* determine what host to contact to submit this request */ if (have_proxy) { - if (req->origin_url.have_ssl && !client->set.no_ssl_tunnel && - !req->connect_tunnel) { + if (req->host_socket != NULL) { /* specific socket proxy */ + req->host_url = NULL; + } else if (req->host_url != NULL) { /* specific normal proxy */ + req->host_socket = NULL; + } else if (req->origin_url.have_ssl && + !client->set.no_ssl_tunnel && !req->connect_tunnel) { req->host_url = &req->origin_url; /* tunnel to origin server */ req->ssl_tunnel = TRUE; } else if (proxy_socket_path != NULL) { diff --git a/src/lib-http/http-client.h b/src/lib-http/http-client.h index 493bf19f94..6fdc214627 100644 --- a/src/lib-http/http-client.h +++ b/src/lib-http/http-client.h @@ -303,6 +303,15 @@ void http_client_request_set_max_attempts(struct http_client_request *req, void http_client_request_set_auth_simple(struct http_client_request *req, const char *username, const char *password); +/* Assign a proxy to use for this particular request. This overrides any + proxy defined in the client settings. */ +void http_client_request_set_proxy_url(struct http_client_request *req, + const struct http_url *proxy_url); +/* Like http_client_request_set_proxy_url(), but the proxy is behind a unix + socket. */ +void http_client_request_set_proxy_socket(struct http_client_request *req, + const char *proxy_socket); + /* delay handling of this request to a later time. This way, a request can be submitted that is held for some time until a certain time period has passed. */