Skip to content

Commit

Permalink
Better HTTPS support
Browse files Browse the repository at this point in the history
  • Loading branch information
bausshf committed Sep 2, 2018
1 parent 917767a commit d318212
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions core/webconfig.d
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ static if (isWeb)
@optional string ipHeader;
/// Collection of webservices.
@optional WebService[] webservices;
/// An associative array of custom configurations;
@optional string[string] customConfig;
/// String of an url to redirect all connections that aren't ssl to.
@optional string forceSSLUrl;
}

/// Wrapper around a webservice.
Expand Down
5 changes: 3 additions & 2 deletions http/remote.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ void remoteJson(T, CTORARGS...)
(
string url,
HttpMethod method,
scope void delegate(T) responder = null,
scope void delegate(scope HTTPClientRequest) requester = null,
scope void delegate(T) responder,
scope void delegate(scope HTTPClientRequest) requester,
CTORARGS args
)
{
return fetchRemote
Expand Down
39 changes: 37 additions & 2 deletions init/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ static if (isWeb)
{
loadServer(ipAddresses, 443);
}
else
else if (port == 443)
{
settings.tlsContext = createTLSContext(TLSContextKind.server);
settings.tlsContext.useCertificateChainFile(webConfig.sslCertificateFile);
Expand Down Expand Up @@ -309,7 +309,14 @@ static if (isWeb)

handleWebSockets(router);

router.any("*", &handleHTTPListen);
if (port == 443)
{
router.any("*", &handleHTTPSListen);
}
else
{
router.any("*", &handleHTTPListen);
}

listenHTTP(settings, router);
}
Expand All @@ -321,11 +328,39 @@ static if (isWeb)
* response = The http response.
*/
void handleHTTPListen(HTTPServerRequest request, HTTPServerResponse response)
{
handleHTTPListenWorker(request, response, false);
}

/**
* Handler for https requests.
* Params:
* request = The http request.
* response = The http response.
*/
void handleHTTPSListen(HTTPServerRequest request, HTTPServerResponse response)
{
handleHTTPListenWorker(request, response, true);
}

/**
* Handler for http requests.
* Params:
* request = The http request.
* response = The http response.
*/
void handleHTTPListenWorker(HTTPServerRequest request, HTTPServerResponse response, bool isSSL)
{
auto client = new HttpClient(request, response);

try
{
if (!isSSL && webConfig.forceSSLUrl && webConfig.forceSSLUrl.length)
{
client.redirect(webConfig.forceSSLUrl);
return;
}

import std.algorithm : canFind;

if (webConfig.hostWhiteList && !webConfig.hostWhiteList.canFind(client.host))
Expand Down

0 comments on commit d318212

Please sign in to comment.