Skip to content

Commit 7fa9685

Browse files
authored
Merge pull request #95 from bausshf/master
Added host white-list
2 parents 50fbfcc + 9369595 commit 7fa9685

4 files changed

Lines changed: 48 additions & 25 deletions

File tree

core/webconfig.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ static if (isWeb)
2020
string[] staticFileRoutes;
2121
/// The paths to white-list for file-access/directory-access.
2222
@optional string[] whiteListPaths;
23+
/// A list of hosts that the server accepts.
24+
@optional string[] hostWhiteList;
2325
/// The route that's mapped to the home page.
2426
string homeRoute;
2527
/// Boolean determining whether views can be accessed by their file name.

errors/errorhandler.d

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,32 @@ static if (isWeb)
2929
{
3030
try
3131
{
32-
response.statusCode = error ? error.code : 500;
33-
auto httpStatusExcepton = cast(HTTPStatusException)e;
32+
response.statusCode = (error ? error.code : 500);
3433

35-
if ((!httpStatusExcepton || httpStatusExcepton.status != HTTPStatus.NotFound) &&
36-
(response.statusCode != 404 && response.statusCode != 200)
37-
)
38-
{
39-
// log ...
40-
}
34+
auto httpStatusException = cast(HTTPStatusException)e;
4135

42-
if (httpStatusExcepton && httpStatusExcepton.status == HTTPStatus.NotFound)
36+
if (httpStatusException)
4337
{
44-
response.statusCode = 404;
45-
46-
foreach (headerKey,headerValue; webConfig.defaultHeaders.notFound)
47-
{
48-
response.headers[headerKey] = headerValue;
49-
}
38+
response.statusCode = httpStatusException.status;
5039

51-
if (webSettings)
52-
{
53-
webSettings.onNotFound(request,response);
54-
}
55-
else
40+
if (httpStatusException.status == 404)
5641
{
57-
response.bodyWriter.write("Not found ...");
42+
foreach (headerKey,headerValue; webConfig.defaultHeaders.notFound)
43+
{
44+
response.headers[headerKey] = headerValue;
45+
}
46+
47+
if (webSettings)
48+
{
49+
webSettings.onNotFound(request,response);
50+
}
51+
else
52+
{
53+
response.bodyWriter.write("Not found ...");
54+
}
55+
56+
return;
5857
}
59-
return;
6058
}
6159

6260
foreach (headerKey,headerValue; webConfig.defaultHeaders.error)
@@ -98,7 +96,7 @@ static if (isWeb)
9896
{
9997
try
10098
{
101-
response.statusCode = error ? error.code : 500;
99+
response.statusCode = (error ? error.code : 500);
102100

103101
if (error && error.code == 404)
104102
{

http/client.d

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,10 +440,17 @@ static if (isWeb)
440440
}
441441

442442
/// Sends an unauthorized error
443-
void unauthorized() {
443+
void unauthorized()
444+
{
444445
error(HttpStatus.unauthorized);
445446
}
446447

448+
/// Sends a forbidden error
449+
void forbidden()
450+
{
451+
error(HttpStatus.forbidden);
452+
}
453+
447454
/**
448455
* Logs the client in.
449456
* Params:

init/web.d

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static if (isWeb)
181181
}
182182
}
183183
}
184-
184+
185185
/// The static file handlers.
186186
__gshared HTTPServerRequestDelegateS[string] _staticFiles;
187187

@@ -272,6 +272,13 @@ static if (isWeb)
272272

273273
try
274274
{
275+
import std.algorithm : canFind;
276+
277+
if (webConfig.hostWhiteList && !webConfig.hostWhiteList.canFind(client.host))
278+
{
279+
client.forbidden();
280+
}
281+
275282
if (handleSpecializedRoute(client))
276283
{
277284
return;
@@ -299,6 +306,15 @@ static if (isWeb)
299306
handleHTTPListenInternal(client);
300307
}
301308
}
309+
catch (HTTPStatusException hse)
310+
{
311+
auto e = cast(Exception)hse;
312+
313+
if (e)
314+
{
315+
handleUserException(e,request,response,null);
316+
}
317+
}
302318
catch (Throwable t)
303319
{
304320
static if (loggingEnabled)

0 commit comments

Comments
 (0)