説明なし

RNFetchBlobNetwork.m 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // RNFetchBlobNetwork.m
  3. // RNFetchBlob
  4. //
  5. // Created by wkh237 on 2016/6/6.
  6. // Copyright © 2016 wkh237. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "RNFetchBlobNetwork.h"
  10. #import "RNFetchBlob.h"
  11. #import "RNFetchBlobConst.h"
  12. #import "RNFetchBlobProgress.h"
  13. #if __has_include(<React/RCTAssert.h>)
  14. #import <React/RCTRootView.h>
  15. #import <React/RCTLog.h>
  16. #import <React/RCTEventDispatcher.h>
  17. #import <React/RCTBridge.h>
  18. #else
  19. #import "RCTRootView.h"
  20. #import "RCTLog.h"
  21. #import "RCTEventDispatcher.h"
  22. #import "RCTBridge.h"
  23. #endif
  24. ////////////////////////////////////////
  25. //
  26. // HTTP request handler
  27. //
  28. ////////////////////////////////////////
  29. NSMapTable * expirationTable;
  30. __attribute__((constructor))
  31. static void initialize_tables() {
  32. if(expirationTable == nil)
  33. {
  34. expirationTable = [[NSMapTable alloc] init];
  35. }
  36. }
  37. @implementation RNFetchBlobNetwork
  38. - (id)init {
  39. self = [super init];
  40. if (self) {
  41. self.requestsTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
  42. self.taskQueue = [[NSOperationQueue alloc] init];
  43. self.taskQueue.qualityOfService = NSQualityOfServiceUtility;
  44. self.taskQueue.maxConcurrentOperationCount = 10;
  45. }
  46. return self;
  47. }
  48. + (RNFetchBlobNetwork* _Nullable)sharedInstance {
  49. static id _sharedInstance = nil;
  50. static dispatch_once_t onceToken;
  51. dispatch_once(&onceToken, ^{
  52. _sharedInstance = [[self alloc] init];
  53. });
  54. return _sharedInstance;
  55. }
  56. + (void) sendRequest:(__weak NSDictionary * _Nullable )options
  57. contentLength:(long) contentLength
  58. bridge:(RCTBridge * _Nullable)bridgeRef
  59. taskId:(NSString * _Nullable)taskId
  60. withRequest:(__weak NSURLRequest * _Nullable)req
  61. callback:(_Nullable RCTResponseSenderBlock) callback
  62. {
  63. RNFetchBlobRequest *request = [[RNFetchBlobRequest alloc] init];
  64. [request sendRequest:options
  65. contentLength:contentLength
  66. bridge:bridgeRef
  67. taskId:taskId
  68. withRequest:req
  69. taskOperationQueue:[self sharedInstance].taskQueue
  70. callback:callback];
  71. @synchronized([RNFetchBlobNetwork class]) {
  72. [[self sharedInstance].requestsTable setObject:request forKey:taskId];
  73. }
  74. }
  75. + (void) enableProgressReport:(NSString *) taskId config:(RNFetchBlobProgress *)config
  76. {
  77. if (config) {
  78. @synchronized ([RNFetchBlobNetwork class]) {
  79. [[self sharedInstance].requestsTable objectForKey:taskId].progressConfig = config;
  80. }
  81. }
  82. }
  83. + (void) enableUploadProgress:(NSString *) taskId config:(RNFetchBlobProgress *)config
  84. {
  85. if (config) {
  86. @synchronized ([RNFetchBlobNetwork class]) {
  87. [[self sharedInstance].requestsTable objectForKey:taskId].uploadProgressConfig = config;
  88. }
  89. }
  90. }
  91. // removing case from headers
  92. + (NSMutableDictionary *) normalizeHeaders:(NSDictionary *)headers
  93. {
  94. NSMutableDictionary * mheaders = [[NSMutableDictionary alloc]init];
  95. for(NSString * key in headers) {
  96. [mheaders setValue:[headers valueForKey:key] forKey:[key lowercaseString]];
  97. }
  98. return mheaders;
  99. }
  100. // #115 Invoke fetch.expire event on those expired requests so that the expired event can be handled
  101. + (void) emitExpiredTasks
  102. {
  103. @synchronized ([RNFetchBlobNetwork class]){
  104. NSEnumerator * emu = [expirationTable keyEnumerator];
  105. NSString * key;
  106. while((key = [emu nextObject]))
  107. {
  108. RCTBridge * bridge = [RNFetchBlob getRCTBridge];
  109. id args = @{ @"taskId": key };
  110. [bridge.eventDispatcher sendDeviceEventWithName:EVENT_EXPIRE body:args];
  111. }
  112. // clear expired task entries
  113. [expirationTable removeAllObjects];
  114. expirationTable = [[NSMapTable alloc] init];
  115. }
  116. }
  117. + (void) cancelRequest:(NSString *)taskId
  118. {
  119. NSURLSessionDataTask * task;
  120. @synchronized ([RNFetchBlobNetwork class]) {
  121. task = [[self sharedInstance].requestsTable objectForKey:taskId].task;
  122. }
  123. if(task && task.state == NSURLSessionTaskStateRunning) {
  124. [task cancel];
  125. }
  126. }
  127. @end