Browse Source

Fixed -serverURL not taking into account GCDWebServerOption_BindToLocalhost

Pierre-Olivier Latour 10 years ago
parent
commit
0a9d3105fc
1 changed files with 4 additions and 1 deletions
  1. 4 1
      GCDWebServer/Core/GCDWebServer.m

+ 4 - 1
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]];