Skip to content

Commit 14d538b

Browse files
committed
Added GCDWebServerOption_BindToLocalhost option
1 parent 3b7198b commit 14d538b

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

GCDWebServer/Core/GCDWebServer.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ extern NSString* const GCDWebServerOption_BonjourName;
9090
*/
9191
extern NSString* const GCDWebServerOption_BonjourType;
9292

93+
/**
94+
* Only accept HTTP requests coming from localhost i.e. not from the outside
95+
* network (NSNumber / BOOL).
96+
*
97+
* The default value is NO.
98+
*
99+
* @warning Bonjour should be disabled if using this option since the server
100+
* will not be reachable from the outside network anyway.
101+
*/
102+
extern NSString* const GCDWebServerOption_BindToLocalhost;
103+
93104
/**
94105
* The maximum number of incoming HTTP requests that can be queued waiting to
95106
* be handled before new ones are dropped (NSNumber / NSUInteger).

GCDWebServer/Core/GCDWebServer.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
NSString* const GCDWebServerOption_Port = @"Port";
5151
NSString* const GCDWebServerOption_BonjourName = @"BonjourName";
5252
NSString* const GCDWebServerOption_BonjourType = @"BonjourType";
53+
NSString* const GCDWebServerOption_BindToLocalhost = @"BindToLocalhost";
5354
NSString* const GCDWebServerOption_MaxPendingConnections = @"MaxPendingConnections";
5455
NSString* const GCDWebServerOption_ServerName = @"ServerName";
5556
NSString* const GCDWebServerOption_AuthenticationMethod = @"AuthenticationMethod";
@@ -495,14 +496,15 @@ - (BOOL)_start:(NSError**)error {
495496
GWS_DCHECK(_source4 == NULL);
496497

497498
NSUInteger port = [_GetOption(_options, GCDWebServerOption_Port, @0) unsignedIntegerValue];
499+
BOOL bindToLocalhost = [_GetOption(_options, GCDWebServerOption_BindToLocalhost, @NO) boolValue];
498500
NSUInteger maxPendingConnections = [_GetOption(_options, GCDWebServerOption_MaxPendingConnections, @16) unsignedIntegerValue];
499501

500502
struct sockaddr_in addr4;
501503
bzero(&addr4, sizeof(addr4));
502504
addr4.sin_len = sizeof(addr4);
503505
addr4.sin_family = AF_INET;
504506
addr4.sin_port = htons(port);
505-
addr4.sin_addr.s_addr = htonl(INADDR_ANY);
507+
addr4.sin_addr.s_addr = bindToLocalhost ? htonl(INADDR_LOOPBACK) : htonl(INADDR_ANY);
506508
int listeningSocket4 = [self _createListeningSocket:NO localAddress:&addr4 length:sizeof(addr4) maxPendingConnections:maxPendingConnections error:error];
507509
if (listeningSocket4 <= 0) {
508510
return NO;
@@ -523,7 +525,7 @@ - (BOOL)_start:(NSError**)error {
523525
addr6.sin6_len = sizeof(addr6);
524526
addr6.sin6_family = AF_INET6;
525527
addr6.sin6_port = htons(port);
526-
addr6.sin6_addr = in6addr_any;
528+
addr6.sin6_addr = bindToLocalhost ? in6addr_loopback : in6addr_any;
527529
int listeningSocket6 = [self _createListeningSocket:YES localAddress:&addr6 length:sizeof(addr6) maxPendingConnections:maxPendingConnections error:error];
528530
if (listeningSocket6 <= 0) {
529531
close(listeningSocket4);

Mac/main.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ int main(int argc, const char* argv[]) {
141141
NSString* authenticationRealm = nil;
142142
NSString* authenticationUser = nil;
143143
NSString* authenticationPassword = nil;
144+
BOOL bindToLocalhost = NO;
144145

145146
if (argc == 1) {
146-
fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | htmlFileUpload | webDAV | webUploader | streamingResponse | asyncResponse] [-record] [-root directory] [-tests directory] [-authenticationMethod Basic | Digest] [-authenticationRealm realm] [-authenticationUser user] [-authenticationPassword password]\n\n", basename((char*)argv[0]));
147+
fprintf(stdout, "Usage: %s [-mode webServer | htmlPage | htmlForm | htmlFileUpload | webDAV | webUploader | streamingResponse | asyncResponse] [-record] [-root directory] [-tests directory] [-authenticationMethod Basic | Digest] [-authenticationRealm realm] [-authenticationUser user] [-authenticationPassword password] [--localhost]\n\n", basename((char*)argv[0]));
147148
} else {
148149
for (int i = 1; i < argc; ++i) {
149150
if (argv[i][0] != '-') {
@@ -188,6 +189,8 @@ int main(int argc, const char* argv[]) {
188189
} else if (!strcmp(argv[i], "-authenticationPassword") && (i + 1 < argc)) {
189190
++i;
190191
authenticationPassword = [NSString stringWithUTF8String:argv[i]];
192+
} else if (!strcmp(argv[i], "--localhost")) {
193+
bindToLocalhost = YES;
191194
}
192195
}
193196
}
@@ -386,6 +389,7 @@ int main(int argc, const char* argv[]) {
386389
fprintf(stdout, "\n");
387390
NSMutableDictionary* options = [NSMutableDictionary dictionary];
388391
[options setObject:@8080 forKey:GCDWebServerOption_Port];
392+
[options setObject:@(bindToLocalhost) forKey:GCDWebServerOption_BindToLocalhost];
389393
[options setObject:@"" forKey:GCDWebServerOption_BonjourName];
390394
if (authenticationUser && authenticationPassword) {
391395
[options setValue:authenticationRealm forKey:GCDWebServerOption_AuthenticationRealm];

0 commit comments

Comments
 (0)