FRepoInfo.m 4.0 KB

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