From 0a9d3105fc1c6e2f4492114bbb8b12686a5397c7 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Latour Date: Mon, 13 Apr 2015 13:02:52 -0700 Subject: [PATCH] Fixed -serverURL not taking into account GCDWebServerOption_BindToLocalhost --- GCDWebServer/Core/GCDWebServer.m | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/GCDWebServer/Core/GCDWebServer.m b/GCDWebServer/Core/GCDWebServer.m index b005146..d057c4e 100644 --- a/GCDWebServer/Core/GCDWebServer.m +++ b/GCDWebServer/Core/GCDWebServer.m @@ -171,6 +171,7 @@ static void _ExecuteMainThreadRunLoopSources() { dispatch_source_t _source6; CFNetServiceRef _registrationService; CFNetServiceRef _resolutionService; + BOOL _bindToLocalhost; #if TARGET_OS_IPHONE BOOL _suspendInBackground; UIBackgroundTaskIdentifier _backgroundTask; @@ -554,6 +555,7 @@ static inline NSString* _EncodeBase64(NSString* string) { _source4 = [self _createDispatchSourceWithListeningSocket:listeningSocket4 isIPv6:NO]; _source6 = [self _createDispatchSourceWithListeningSocket:listeningSocket6 isIPv6:YES]; _port = port; + _bindToLocalhost = bindToLocalhost; NSString* bonjourName = _GetOption(_options, GCDWebServerOption_BonjourName, nil); NSString* bonjourType = _GetOption(_options, GCDWebServerOption_BonjourType, @"_http._tcp"); @@ -619,6 +621,7 @@ static inline NSString* _EncodeBase64(NSString* string) { #endif _source4 = NULL; _port = 0; + _bindToLocalhost = NO; _serverName = nil; _authenticationRealm = nil; @@ -715,7 +718,7 @@ static inline NSString* _EncodeBase64(NSString* string) { - (NSURL*)serverURL { if (_source4) { - NSString* ipAddress = GCDWebServerGetPrimaryIPAddress(NO); // We can't really use IPv6 anyway as it doesn't work great with HTTP URLs in practice + NSString* ipAddress = _bindToLocalhost ? @"localhost" : GCDWebServerGetPrimaryIPAddress(NO); // We can't really use IPv6 anyway as it doesn't work great with HTTP URLs in practice if (ipAddress) { if (_port != 80) { return [NSURL URLWithString:[NSString stringWithFormat:@"http://%@:%i/", ipAddress, (int)_port]];