Nenhuma descrição

RNFetchBlobNetwork.m 4.1KB

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