FRepoInfo.m 4.1 KB

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