Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unix domain socket support to http.proxy #1813

Merged
merged 1 commit into from
Jul 3, 2017
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
2 changes: 1 addition & 1 deletion http/vibe/http/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ final class HTTPClientResponse : HTTPResponse {
}

/** Returns clean host string. In case of unix socket it performs urlDecode on host. */
private auto getFilteredHost(URL url)
package auto getFilteredHost(URL url)
{
version(UnixSocket)
{
Expand Down
46 changes: 32 additions & 14 deletions http/vibe/http/proxy.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ void listenHTTPReverseProxy(HTTPServerSettings settings, HTTPReverseProxySetting
/// ditto
void listenHTTPReverseProxy(HTTPServerSettings settings, string destination_host, ushort destination_port)
{
URL url;
url.schema = "http";
url.host = destination_host;
url.port = destination_port;
auto proxy_settings = new HTTPReverseProxySettings;
proxy_settings.destinationHost = destination_host;
proxy_settings.destinationPort = destination_port;
proxy_settings.destination = url;
listenHTTPReverseProxy(settings, proxy_settings);
}

Expand All @@ -58,10 +61,7 @@ HTTPServerRequestDelegateS reverseProxyRequest(HTTPReverseProxySettings settings
foreach (n; non_forward_headers)
non_forward_headers_map[n] = "";

URL url;
url.schema = "http";
url.host = settings.destinationHost;
url.port = settings.destinationPort;
auto url = settings.destination;

void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res)
@safe {
Expand All @@ -76,7 +76,7 @@ HTTPServerRequestDelegateS reverseProxyRequest(HTTPReverseProxySettings settings
}

TCPConnection ccon;
try ccon = connectTCP(settings.destinationHost, settings.destinationPort);
try ccon = connectTCP(url.getFilteredHost, url.port);
catch (Exception e) {
throw new HTTPStatusException(HTTPStatus.badGateway, "Connection to upstream server failed: "~e.msg);
}
Expand All @@ -102,7 +102,7 @@ HTTPServerRequestDelegateS reverseProxyRequest(HTTPReverseProxySettings settings
{
creq.method = req.method;
creq.headers = req.headers.dup;
creq.headers["Host"] = settings.destinationHost;
creq.headers["Host"] = url.getFilteredHost;

//handle protocol upgrades
if (!isUpgrade) {
Expand Down Expand Up @@ -195,21 +195,39 @@ HTTPServerRequestDelegateS reverseProxyRequest(HTTPReverseProxySettings settings
}
/// ditto
HTTPServerRequestDelegateS reverseProxyRequest(string destination_host, ushort destination_port)
{
URL url;
url.schema = "http";
url.host = destination_host;
url.port = destination_port;
auto settings = new HTTPReverseProxySettings;
settings.destination = url;
return reverseProxyRequest(settings);
}

/// ditto
HTTPServerRequestDelegateS reverseProxyRequest(URL destination)
{
auto settings = new HTTPReverseProxySettings;
settings.destinationHost = destination_host;
settings.destinationPort = destination_port;
settings.destination = destination;
return reverseProxyRequest(settings);
}

/**
Provides advanced configuration facilities for reverse proxy servers.
*/
final class HTTPReverseProxySettings {
/// The destination host to forward requests to
string destinationHost;
/// The destination port to forward requests to
ushort destinationPort;
/// Scheduled for deprecation - use `destination.host` instead.
@property string destinationHost() const { return destination.host; }
/// ditto
@property void destinationHost(string host) { destination.host = host; }
/// Scheduled for deprecation - use `destination.port` instead.
@property ushort destinationPort() const { return destination.port; }
/// ditto
@property void destinationPort(ushort port) { destination.port = port; }

/// The destination URL to forward requests to
URL destination = URL("http", Path(""));
/// Avoids compressed transfers between proxy and destination hosts
bool avoidCompressedRequests;
/// Handle CONNECT requests for creating a tunnel to the destination host
Expand Down