QNConfiguration.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // QNConfiguration.m
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 15/5/21.
  6. // Copyright (c) 2015年 Qiniu. All rights reserved.
  7. //
  8. #import "QNConfiguration.h"
  9. #import "QNResponseInfo.h"
  10. #import "QNSessionManager.h"
  11. #import "QNSystem.h"
  12. #import "QNUpToken.h"
  13. const UInt32 kQNBlockSize = 4 * 1024 * 1024;
  14. @implementation QNConfiguration
  15. + (instancetype)build:(QNConfigurationBuilderBlock)block {
  16. QNConfigurationBuilder *builder = [[QNConfigurationBuilder alloc] init];
  17. block(builder);
  18. return [[QNConfiguration alloc] initWithBuilder:builder];
  19. }
  20. - (instancetype)initWithBuilder:(QNConfigurationBuilder *)builder {
  21. if (self = [super init]) {
  22. _chunkSize = builder.chunkSize;
  23. _putThreshold = builder.putThreshold;
  24. _retryMax = builder.retryMax;
  25. _timeoutInterval = builder.timeoutInterval;
  26. _recorder = builder.recorder;
  27. _recorderKeyGen = builder.recorderKeyGen;
  28. _proxy = builder.proxy;
  29. _converter = builder.converter;
  30. _disableATS = builder.disableATS;
  31. _zone = builder.zone;
  32. _useHttps = builder.useHttps;
  33. }
  34. return self;
  35. }
  36. @end
  37. @implementation QNConfigurationBuilder
  38. - (instancetype)init {
  39. if (self = [super init]) {
  40. _zone = [[QNAutoZone alloc] init];
  41. _chunkSize = 2 * 1024 * 1024;
  42. _putThreshold = 4 * 1024 * 1024;
  43. _retryMax = 3;
  44. _timeoutInterval = 60;
  45. _recorder = nil;
  46. _recorderKeyGen = nil;
  47. _proxy = nil;
  48. _converter = nil;
  49. if (hasAts() && !allowsArbitraryLoads()) {
  50. _disableATS = NO;
  51. } else {
  52. _disableATS = YES;
  53. }
  54. _useHttps = YES;
  55. }
  56. return self;
  57. }
  58. @end
  59. @implementation QNZoneInfo
  60. - (instancetype)init:(long)ttl
  61. upDomainsList:(NSMutableArray<NSString *> *)upDomainsList
  62. upDomainsDic:(NSMutableDictionary *)upDomainsDic {
  63. if (self = [super init]) {
  64. _ttl = ttl;
  65. _upDomainsList = upDomainsList;
  66. _upDomainsDic = upDomainsDic;
  67. }
  68. return self;
  69. }
  70. - (QNZoneInfo *)buildInfoFromJson:(NSDictionary *)resp {
  71. long ttl = [[resp objectForKey:@"ttl"] longValue];
  72. NSDictionary *up = [resp objectForKey:@"up"];
  73. NSDictionary *acc = [up objectForKey:@"acc"];
  74. NSDictionary *src = [up objectForKey:@"src"];
  75. NSDictionary *old_acc = [up objectForKey:@"old_acc"];
  76. NSDictionary *old_src = [up objectForKey:@"old_src"];
  77. NSArray *urlDicList = [[NSArray alloc] initWithObjects:acc, src, old_acc, old_src, nil];
  78. NSMutableArray *domainList = [[NSMutableArray alloc] init];
  79. NSMutableDictionary *domainDic = [[NSMutableDictionary alloc] init];
  80. //main
  81. for (int i = 0; i < urlDicList.count; i++) {
  82. if ([[urlDicList[i] allKeys] containsObject:@"main"]) {
  83. NSArray *mainDomainList = urlDicList[i][@"main"];
  84. for (int i = 0; i < mainDomainList.count; i++) {
  85. [domainList addObject:mainDomainList[i]];
  86. [domainDic setObject:[NSDate dateWithTimeIntervalSince1970:0] forKey:mainDomainList[i]];
  87. }
  88. }
  89. //backup
  90. if ([[urlDicList[i] allKeys] containsObject:@"backup"]) {
  91. NSArray *mainDomainList = urlDicList[i][@"backup"];
  92. for (int i = 0; i < mainDomainList.count; i++) {
  93. [domainList addObject:mainDomainList[i]];
  94. [domainDic setObject:[NSDate dateWithTimeIntervalSince1970:0] forKey:mainDomainList[i]];
  95. }
  96. }
  97. }
  98. return [[QNZoneInfo alloc] init:ttl upDomainsList:domainList upDomainsDic:domainDic];
  99. }
  100. - (void)frozenDomain:(NSString *)domain {
  101. NSTimeInterval secondsFor10min = 10 * 60;
  102. NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsFor10min];
  103. [self.upDomainsDic setObject:tomorrow forKey:domain];
  104. }
  105. @end
  106. @implementation QNZone
  107. - (instancetype)init {
  108. self = [super init];
  109. return self;
  110. }
  111. - (NSArray<NSString *> *)upDomainList:(NSString *)token {
  112. return self.upDomainList;
  113. }
  114. - (NSString *)upHost:(QNZoneInfo *)zoneInfo
  115. isHttps:(BOOL)isHttps
  116. lastUpHost:(NSString *)lastUpHost {
  117. NSString *upHost = nil;
  118. NSString *upDomain = nil;
  119. // frozen domain
  120. if (lastUpHost) {
  121. NSString *upLastDomain = nil;
  122. if (isHttps) {
  123. upLastDomain = [lastUpHost substringFromIndex:8];
  124. } else {
  125. upLastDomain = [lastUpHost substringFromIndex:7];
  126. }
  127. [zoneInfo frozenDomain:upLastDomain];
  128. }
  129. //get backup domain
  130. for (NSString *backupDomain in zoneInfo.upDomainsList) {
  131. NSDate *frozenTill = zoneInfo.upDomainsDic[backupDomain];
  132. NSDate *now = [NSDate date];
  133. if ([frozenTill compare:now] == NSOrderedAscending) {
  134. upDomain = backupDomain;
  135. break;
  136. }
  137. }
  138. if (upDomain) {
  139. [zoneInfo.upDomainsDic setObject:[NSDate dateWithTimeIntervalSince1970:0] forKey:upDomain];
  140. } else {
  141. //reset all the up host frozen time
  142. for (NSString *domain in zoneInfo.upDomainsList) {
  143. [zoneInfo.upDomainsDic setObject:[NSDate dateWithTimeIntervalSince1970:0] forKey:domain];
  144. }
  145. if (zoneInfo.upDomainsList.count > 0) {
  146. upDomain = zoneInfo.upDomainsList[0];
  147. }
  148. }
  149. if (upDomain) {
  150. if (isHttps) {
  151. upHost = [NSString stringWithFormat:@"https://%@", upDomain];
  152. } else {
  153. upHost = [NSString stringWithFormat:@"http://%@", upDomain];
  154. }
  155. }
  156. return upHost;
  157. }
  158. - (NSString *)up:(QNUpToken *)token
  159. isHttps:(BOOL)isHttps
  160. frozenDomain:(NSString *)frozenDomain {
  161. return nil;
  162. }
  163. - (void)preQuery:(QNUpToken *)token
  164. on:(QNPrequeryReturn)ret {
  165. ret(0);
  166. }
  167. @end
  168. @interface QNFixedZone () {
  169. NSString *server;
  170. NSMutableDictionary *cache;
  171. NSLock *lock;
  172. }
  173. @end
  174. @implementation QNFixedZone
  175. - (instancetype)initWithupDomainList:(NSArray<NSString *> *)upList {
  176. if (self = [super init]) {
  177. self.upDomainList = upList;
  178. self.zoneInfo = [self createZoneInfo:upList];
  179. }
  180. return self;
  181. }
  182. + (instancetype)createWithHost:(NSArray<NSString *> *)upList {
  183. return [[QNFixedZone alloc] initWithupDomainList:upList];
  184. }
  185. + (instancetype)zone0 {
  186. static QNFixedZone *z0 = nil;
  187. static dispatch_once_t onceToken;
  188. dispatch_once(&onceToken, ^{
  189. static const NSArray<NSString *> *uplist = nil;
  190. if (!uplist) {
  191. uplist = [[NSArray alloc] initWithObjects:@"upload.qiniup.com", @"upload-nb.qiniup.com",
  192. @"upload-xs.qiniup.com", @"up.qiniup.com",
  193. @"up-nb.qiniup.com", @"up-xs.qiniup.com",
  194. @"upload.qbox.me", @"up.qbox.me", nil];
  195. z0 = [QNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
  196. }
  197. });
  198. return z0;
  199. }
  200. + (instancetype)zone1 {
  201. static QNFixedZone *z1 = nil;
  202. static dispatch_once_t onceToken;
  203. dispatch_once(&onceToken, ^{
  204. static const NSArray<NSString *> *uplist = nil;
  205. if (!uplist) {
  206. uplist = [[NSArray alloc] initWithObjects:@"upload-z1.qiniup.com", @"up-z1.qiniup.com",
  207. @"upload-z1.qbox.me", @"up-z1.qbox.me", nil];
  208. z1 = [QNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
  209. }
  210. });
  211. return z1;
  212. }
  213. + (instancetype)zone2 {
  214. static QNFixedZone *z2 = nil;
  215. static dispatch_once_t onceToken;
  216. dispatch_once(&onceToken, ^{
  217. static const NSArray<NSString *> *uplist = nil;
  218. if (!uplist) {
  219. uplist = [[NSArray alloc] initWithObjects:@"upload-z2.qiniup.com", @"upload-gz.qiniup.com",
  220. @"upload-fs.qiniup.com", @"up-z2.qiniup.com",
  221. @"up-gz.qiniup.com", @"up-fs.qiniup.com",
  222. @"upload-z2.qbox.me", @"up-z2.qbox.me", nil];
  223. z2 = [QNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
  224. }
  225. });
  226. return z2;
  227. }
  228. + (instancetype)zoneNa0 {
  229. static QNFixedZone *zNa0 = nil;
  230. static dispatch_once_t onceToken;
  231. dispatch_once(&onceToken, ^{
  232. static const NSArray<NSString *> *uplist = nil;
  233. if (!uplist) {
  234. uplist = [[NSArray alloc] initWithObjects:@"upload-na0.qiniup.com", @"up-na0.qiniup.com",
  235. @"upload-na0.qbox.me", @"up-na0.qbox.me", nil];
  236. zNa0 = [QNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
  237. }
  238. });
  239. return zNa0;
  240. }
  241. + (instancetype)zoneAs0 {
  242. static QNFixedZone *zAs0 = nil;
  243. static dispatch_once_t onceToken;
  244. dispatch_once(&onceToken, ^{
  245. static const NSArray<NSString *> *uplist = nil;
  246. if (!uplist) {
  247. uplist = [[NSArray alloc] initWithObjects:@"upload-as0.qiniup.com", @"up-as0.qiniup.com",
  248. @"upload-as0.qbox.me", @"up-as0.qbox.me", nil];
  249. zAs0 = [QNFixedZone createWithHost:(NSArray<NSString *> *)uplist];
  250. }
  251. });
  252. return zAs0;
  253. }
  254. - (void)preQuery:(QNUpToken *)token
  255. on:(QNPrequeryReturn)ret {
  256. ret(0);
  257. }
  258. - (QNZoneInfo *)createZoneInfo:(NSArray<NSString *> *)upDomainList {
  259. NSMutableDictionary *upDomainDic = [[NSMutableDictionary alloc] init];
  260. for (NSString *upDomain in upDomainList) {
  261. [upDomainDic setValue:[NSDate dateWithTimeIntervalSince1970:0] forKey:upDomain];
  262. }
  263. QNZoneInfo *zoneInfo = [[QNZoneInfo alloc] init:86400 upDomainsList:(NSMutableArray<NSString *> *)upDomainList upDomainsDic:upDomainDic];
  264. return zoneInfo;
  265. }
  266. - (NSString *)up:(QNUpToken *)token
  267. isHttps:(BOOL)isHttps
  268. frozenDomain:(NSString *)frozenDomain {
  269. if (self.zoneInfo == nil) {
  270. return nil;
  271. }
  272. return [super upHost:self.zoneInfo isHttps:isHttps lastUpHost:frozenDomain];
  273. }
  274. @end
  275. @implementation QNAutoZone {
  276. NSString *server;
  277. NSMutableDictionary *cache;
  278. NSLock *lock;
  279. QNSessionManager *sesionManager;
  280. }
  281. - (instancetype)init{
  282. if (self = [super init]) {
  283. server = @"https://uc.qbox.me";
  284. cache = [NSMutableDictionary new];
  285. lock = [NSLock new];
  286. sesionManager = [[QNSessionManager alloc] initWithProxy:nil timeout:10 urlConverter:nil];
  287. }
  288. return self;
  289. }
  290. - (NSString *)up:(QNUpToken *)token
  291. isHttps:(BOOL)isHttps
  292. frozenDomain:(NSString *)frozenDomain {
  293. NSString *index = [token index];
  294. [lock lock];
  295. QNZoneInfo *info = [cache objectForKey:index];
  296. [lock unlock];
  297. if (info == nil) {
  298. return nil;
  299. }
  300. return [super upHost:info isHttps:isHttps lastUpHost:frozenDomain];
  301. }
  302. - (void)preQuery:(QNUpToken *)token
  303. on:(QNPrequeryReturn)ret {
  304. if (token == nil) {
  305. ret(-1);
  306. }
  307. [lock lock];
  308. QNZoneInfo *info = [cache objectForKey:[token index]];
  309. [lock unlock];
  310. if (info != nil) {
  311. ret(0);
  312. return;
  313. }
  314. //https://uc.qbox.me/v2/query?ak=T3sAzrwItclPGkbuV4pwmszxK7Ki46qRXXGBBQz3&bucket=if-pbl
  315. NSString *url = [NSString stringWithFormat:@"%@/v2/query?ak=%@&bucket=%@", server, token.access, token.bucket];
  316. [sesionManager get:url withHeaders:nil withCompleteBlock:^(QNResponseInfo *info, NSDictionary *resp) {
  317. if (!info.error) {
  318. QNZoneInfo *info = [[[QNZoneInfo alloc] init] buildInfoFromJson:resp];
  319. if (info == nil) {
  320. ret(kQNInvalidToken);
  321. } else {
  322. [lock lock];
  323. [cache setValue:info forKey:[token index]];
  324. [lock unlock];
  325. ret(0);
  326. }
  327. } else {
  328. ret(kQNNetworkError);
  329. }
  330. }];
  331. }
  332. @end