Skip to content

Commit 0a9d310

Browse files
committed
Fixed -serverURL not taking into account GCDWebServerOption_BindToLocalhost
1 parent 0f0a984 commit 0a9d310

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

GCDWebServer/Core/GCDWebServer.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ @interface GCDWebServer () {
171171
dispatch_source_t _source6;
172172
CFNetServiceRef _registrationService;
173173
CFNetServiceRef _resolutionService;
174+
BOOL _bindToLocalhost;
174175
#if TARGET_OS_IPHONE
175176
BOOL _suspendInBackground;
176177
UIBackgroundTaskIdentifier _backgroundTask;
@@ -554,6 +555,7 @@ - (BOOL)_start:(NSError**)error {
554555
_source4 = [self _createDispatchSourceWithListeningSocket:listeningSocket4 isIPv6:NO];
555556
_source6 = [self _createDispatchSourceWithListeningSocket:listeningSocket6 isIPv6:YES];
556557
_port = port;
558+
_bindToLocalhost = bindToLocalhost;
557559

558560
NSString* bonjourName = _GetOption(_options, GCDWebServerOption_BonjourName, nil);
559561
NSString* bonjourType = _GetOption(_options, GCDWebServerOption_BonjourType, @"_http._tcp");
@@ -619,6 +621,7 @@ - (void)_stop {
619621
#endif
620622
_source4 = NULL;
621623
_port = 0;
624+
_bindToLocalhost = NO;
622625

623626
_serverName = nil;
624627
_authenticationRealm = nil;
@@ -715,7 +718,7 @@ @implementation GCDWebServer (Extensions)
715718

716719
- (NSURL*)serverURL {
717720
if (_source4) {
718-
NSString* ipAddress = GCDWebServerGetPrimaryIPAddress(NO); // We can't really use IPv6 anyway as it doesn't work great with HTTP URLs in practice
721+
NSString* ipAddress = _bindToLocalhost ? @"localhost" : GCDWebServerGetPrimaryIPAddress(NO); // We can't really use IPv6 anyway as it doesn't work great with HTTP URLs in practice
719722
if (ipAddress) {
720723
if (_port != 80) {
721724
return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%i/", ipAddress, (int)_port]];

0 commit comments

Comments
 (0)