FRepoInfo.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FirebaseDatabase/Sources/Core/FRepoInfo.h"
  17. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  18. @interface FRepoInfo ()
  19. @property(nonatomic, strong) NSString *domain;
  20. @end
  21. @implementation FRepoInfo
  22. @synthesize internalHost;
  23. - (instancetype)init {
  24. [NSException
  25. raise:@"FIRDatabaseInvalidInitializer"
  26. format:@"Invalid initializer invoked. This is probably a bug in RTDB."];
  27. abort();
  28. }
  29. - (instancetype)initWithHost:(NSString *)aHost
  30. isSecure:(BOOL)isSecure
  31. withNamespace:(NSString *)aNamespace {
  32. self = [super init];
  33. if (self) {
  34. _host = [aHost copy];
  35. _domain =
  36. [_host containsString:@"."]
  37. ? [_host
  38. substringFromIndex:[_host rangeOfString:@"."].location +
  39. 1]
  40. : _host;
  41. _secure = isSecure;
  42. _namespace = aNamespace;
  43. // Get cached internal host if it exists
  44. NSString *internalHostKey =
  45. [NSString stringWithFormat:@"firebase:host:%@", _host];
  46. NSString *cachedInternalHost = [[NSUserDefaults standardUserDefaults]
  47. stringForKey:internalHostKey];
  48. if (cachedInternalHost != nil) {
  49. internalHost = cachedInternalHost;
  50. } else {
  51. internalHost = [_host copy];
  52. }
  53. }
  54. return self;
  55. }
  56. - (instancetype)initWithInfo:(FRepoInfo *)info emulatedHost:(NSString *)host {
  57. self = [self initWithHost:host isSecure:NO withNamespace:info.namespace];
  58. return self;
  59. }
  60. - (NSString *)description {
  61. // The namespace is encoded in the hostname, so we can just return this.
  62. return [NSString
  63. stringWithFormat:@"http%@://%@", (_secure ? @"s" : @""), _host];
  64. }
  65. - (void)setInternalHost:(NSString *)newHost {
  66. if (![internalHost isEqualToString:newHost]) {
  67. internalHost = newHost;
  68. // Cache the internal host so we don't need to redirect later on
  69. NSString *internalHostKey =
  70. [NSString stringWithFormat:@"firebase:host:%@", self.host];
  71. NSUserDefaults *cache = [NSUserDefaults standardUserDefaults];
  72. [cache setObject:internalHost forKey:internalHostKey];
  73. [cache synchronize];
  74. }
  75. }
  76. - (void)clearInternalHostCache {
  77. self.internalHost = self.host;
  78. // Remove the cached entry
  79. NSString *internalHostKey =
  80. [NSString stringWithFormat:@"firebase:host:%@", self.host];
  81. NSUserDefaults *cache = [NSUserDefaults standardUserDefaults];
  82. [cache removeObjectForKey:internalHostKey];
  83. [cache synchronize];
  84. }
  85. - (BOOL)isDemoHost {
  86. return [self.domain isEqualToString:@"firebaseio-demo.com"];
  87. }
  88. - (BOOL)isCustomHost {
  89. return ![self.domain isEqualToString:@"firebaseio-demo.com"] &&
  90. ![self.domain isEqualToString:@"firebaseio.com"];
  91. }
  92. - (NSString *)connectionURL {
  93. return [self connectionURLWithLastSessionID:nil];
  94. }
  95. - (NSString *)connectionURLWithLastSessionID:(NSString *)lastSessionID {
  96. NSString *scheme;
  97. if (self.secure) {
  98. scheme = @"wss";
  99. } else {
  100. scheme = @"ws";
  101. }
  102. NSString *url =
  103. [NSString stringWithFormat:@"%@://%@/.ws?%@=%@&ns=%@", scheme,
  104. self.internalHost, kWireProtocolVersionParam,
  105. kWebsocketProtocolVersion, self.namespace];
  106. if (lastSessionID != nil) {
  107. url = [NSString stringWithFormat:@"%@&ls=%@", url, lastSessionID];
  108. }
  109. return url;
  110. }
  111. - (id)copyWithZone:(NSZone *)zone {
  112. return self; // Immutable
  113. }
  114. - (NSUInteger)hash {
  115. NSUInteger result = _host.hash;
  116. result = 31 * result + (_secure ? 1 : 0);
  117. result = 31 * result + _namespace.hash;
  118. result = 31 * result + _host.hash;
  119. return result;
  120. }
  121. - (BOOL)isEqual:(id)anObject {
  122. if (![anObject isKindOfClass:[FRepoInfo class]]) {
  123. return NO;
  124. }
  125. FRepoInfo *other = (FRepoInfo *)anObject;
  126. return _secure == other.secure && [_host isEqualToString:other.host] &&
  127. [_namespace isEqualToString:other.namespace];
  128. }
  129. @end