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 <GoogleUtilities/GULUserDefaults.h>
  17. #import "FirebaseDatabase/Sources/Constants/FConstants.h"
  18. #import "FirebaseDatabase/Sources/Core/FRepoInfo.h"
  19. @interface FRepoInfo ()
  20. @property(nonatomic, strong) NSString *domain;
  21. @end
  22. @implementation FRepoInfo
  23. @synthesize internalHost;
  24. - (instancetype)init {
  25. [NSException
  26. raise:@"FIRDatabaseInvalidInitializer"
  27. format:@"Invalid initializer invoked. This is probably a bug in RTDB."];
  28. abort();
  29. }
  30. - (instancetype)initWithHost:(NSString *)aHost
  31. isSecure:(BOOL)isSecure
  32. withNamespace:(NSString *)aNamespace {
  33. self = [super init];
  34. if (self) {
  35. _host = [aHost copy];
  36. _domain =
  37. [_host containsString:@"."]
  38. ? [_host
  39. substringFromIndex:[_host rangeOfString:@"."].location +
  40. 1]
  41. : _host;
  42. _secure = isSecure;
  43. _namespace = aNamespace;
  44. // Get cached internal host if it exists
  45. NSString *internalHostKey =
  46. [NSString stringWithFormat:@"firebase:host:%@", _host];
  47. NSString *cachedInternalHost = [[GULUserDefaults standardUserDefaults]
  48. stringForKey:internalHostKey];
  49. if (cachedInternalHost != nil) {
  50. internalHost = cachedInternalHost;
  51. } else {
  52. internalHost = [_host copy];
  53. }
  54. }
  55. return self;
  56. }
  57. - (instancetype)initWithInfo:(FRepoInfo *)info emulatedHost:(NSString *)host {
  58. self = [self initWithHost:host isSecure:NO withNamespace:info.namespace];
  59. return self;
  60. }
  61. - (NSString *)description {
  62. // The namespace is encoded in the hostname, so we can just return this.
  63. return [NSString
  64. stringWithFormat:@"http%@://%@", (_secure ? @"s" : @""), _host];
  65. }
  66. - (void)setInternalHost:(NSString *)newHost {
  67. if (![internalHost isEqualToString:newHost]) {
  68. internalHost = newHost;
  69. // Cache the internal host so we don't need to redirect later on
  70. NSString *internalHostKey =
  71. [NSString stringWithFormat:@"firebase:host:%@", self.host];
  72. GULUserDefaults *cache = [GULUserDefaults standardUserDefaults];
  73. [cache setObject:internalHost forKey:internalHostKey];
  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. GULUserDefaults *cache = [GULUserDefaults standardUserDefaults];
  82. [cache removeObjectForKey:internalHostKey];
  83. }
  84. - (BOOL)isDemoHost {
  85. return [self.domain isEqualToString:@"firebaseio-demo.com"];
  86. }
  87. - (BOOL)isCustomHost {
  88. return ![self.domain isEqualToString:@"firebaseio-demo.com"] &&
  89. ![self.domain isEqualToString:@"firebaseio.com"];
  90. }
  91. - (NSString *)connectionURL {
  92. return [self connectionURLWithLastSessionID:nil];
  93. }
  94. - (NSString *)connectionURLWithLastSessionID:(NSString *)lastSessionID {
  95. NSString *scheme;
  96. if (self.secure) {
  97. scheme = @"wss";
  98. } else {
  99. scheme = @"ws";
  100. }
  101. NSString *url =
  102. [NSString stringWithFormat:@"%@://%@/.ws?%@=%@&ns=%@", scheme,
  103. self.internalHost, kWireProtocolVersionParam,
  104. kWebsocketProtocolVersion, self.namespace];
  105. if (lastSessionID != nil) {
  106. url = [NSString stringWithFormat:@"%@&ls=%@", url, lastSessionID];
  107. }
  108. return url;
  109. }
  110. - (id)copyWithZone:(NSZone *)zone {
  111. return self; // Immutable
  112. }
  113. - (NSUInteger)hash {
  114. NSUInteger result = _host.hash;
  115. result = 31 * result + (_secure ? 1 : 0);
  116. result = 31 * result + _namespace.hash;
  117. result = 31 * result + _host.hash;
  118. return result;
  119. }
  120. - (BOOL)isEqual:(id)anObject {
  121. if (![anObject isKindOfClass:[FRepoInfo class]]) {
  122. return NO;
  123. }
  124. FRepoInfo *other = (FRepoInfo *)anObject;
  125. return _secure == other.secure && [_host isEqualToString:other.host] &&
  126. [_namespace isEqualToString:other.namespace];
  127. }
  128. @end