PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Frameworks/Foundation/NSURLConnection.mm

https://gitlab.com/goolic/WinObjC
Objective C++ | 323 lines | 218 code | 77 blank | 28 comment | 34 complexity | ab2c86c434d3318454d6f373cd1947f0 MD5 | raw file
  1. /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
  2. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  3. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  4. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  5. #include "Starboard.h"
  6. #include "Foundation/NSURLProtocol.h"
  7. #include "Foundation/NSURLCache.h"
  8. #include "Foundation/NSMutableData.h"
  9. #include "Foundation/NSError.h"
  10. #include "Foundation/NSRunLoop.h"
  11. #include "NSURLConnectionState.h"
  12. #include "Foundation/NSURLConnection.h"
  13. @implementation NSURLConnection : NSObject
  14. +(BOOL) canHandleRequest:(id)request {
  15. return ([NSURLProtocol _URLProtocolClassForRequest:request] !=nil)?YES:NO;
  16. }
  17. /* annotate with type */ +(void) sendAsynchronousRequest:(id)request queue:(id)queue completionHandler:(void(^)(NSURLResponse*, NSData*, NSError **))completionHandler {
  18. EbrDebugLog("sendAsynchronousRequest not fully supported\n");
  19. id response, error;
  20. id data = [self sendSynchronousRequest:request returningResponse:&response error:&error];
  21. EbrCallBlock(completionHandler, "dddd", completionHandler, response, data, error);
  22. }
  23. /* annotate with type */ +(id) sendSynchronousRequest:(id)request returningResponse:(NSURLResponse**)responsep error:(NSError **)errorp {
  24. id state = [[[NSURLConnectionState alloc] init] autorelease];
  25. id connection = [[self alloc] initWithRequest:request delegate:state startImmediately:FALSE];
  26. if(connection==nil) {
  27. if(errorp!=NULL){
  28. *errorp = [NSError errorWithDomain:@"NSURLErrorDomain" code:50 userInfo:nil];
  29. }
  30. return nil;
  31. }
  32. id mode = @"NSURLConnectionRequestMode";
  33. [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
  34. [connection start];
  35. [state receiveAllDataInMode:mode];
  36. [connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
  37. id result= [[((NSURLConnection*) connection)->_mutableData retain] autorelease];
  38. [connection cancel];
  39. if(errorp!=NULL)
  40. *errorp = [state error];
  41. if(responsep!=NULL)
  42. *responsep = [[((NSURLConnection*) connection)->_response retain] autorelease];
  43. [connection release];
  44. return result;
  45. }
  46. /* annotate with type */ +(id) connectionWithRequest:(id)request delegate:(id)delegate {
  47. return [[[self alloc] initWithRequest:request delegate:delegate] autorelease];
  48. }
  49. /* annotate with type */ -(id) initWithRequest:(id)request delegate:(id)delegate startImmediately:(BOOL)startLoading {
  50. _request = [request copy];
  51. id cls = [NSURLProtocol _URLProtocolClassForRequest:request];
  52. if ( cls == nil ) {
  53. return nil;
  54. }
  55. if((_protocol=[[cls alloc] initWithRequest:_request cachedResponse:[[NSURLCache sharedURLCache] cachedResponseForRequest:_request] client:self])==nil) {
  56. [self dealloc];
  57. return nil;
  58. }
  59. _delegate = [delegate retain];
  60. if ( startLoading ) [self start];
  61. return self;
  62. }
  63. /* annotate with type */ -(id) initWithRequest:(id)request delegate:(id)delegate {
  64. return [self initWithRequest:request delegate:delegate startImmediately:YES];
  65. }
  66. -(void) dealloc {
  67. [_request release];
  68. [_protocol release];
  69. [_delegate release];
  70. [_response release];
  71. [_mutableData release];
  72. [super dealloc];
  73. }
  74. /* annotate with type */ -(void) start {
  75. if ( !_didRetain ) {
  76. [self retain];
  77. _didRetain = TRUE;
  78. }
  79. [_protocol startLoading];
  80. }
  81. /* annotate with type */ -(void) scheduleInRunLoop:(id)runLoop forMode:(id)mode {
  82. [_protocol scheduleInRunLoop:runLoop forMode:mode];
  83. }
  84. /* annotate with type */ -(void) unscheduleFromRunLoop:(id)runLoop forMode:(id)mode {
  85. [_protocol unscheduleFromRunLoop:runLoop forMode:mode];
  86. }
  87. /* annotate with type */ -(void) URLProtocol:(id)urlProtocol didFailWithError:(id)error {
  88. EbrDebugLog("URL protocol did fail\n");
  89. //if ( [_delegate respondsToSelector:@selector(connection:willSendRequest:redirectResponse:)] ) [_delegate connection:self willSendRequest:_request redirectResponse:nil];
  90. if ( [_delegate respondsToSelector:@selector(connection:didFailWithError:)] ) [_delegate connection:self didFailWithError:error];
  91. if ( _didRetain && !_didRelease ) {
  92. _didRelease = TRUE;
  93. [self autorelease];
  94. }
  95. [_delegate autorelease];
  96. _delegate = nil;
  97. }
  98. /* annotate with type */ -(id) URLProtocol:(id)urlProtocol willSendRequest:(id)request redirectResponse:(id)response {
  99. id ret = request;
  100. if ( [_delegate respondsToSelector:@selector(connection:willSendRequest:redirectResponse:)] ) {
  101. ret = [_delegate connection:self willSendRequest:request redirectResponse:response];
  102. }
  103. return ret;
  104. }
  105. /* annotate with type */ -(void) URLProtocol:(id)urlProtocol didReceiveResponse:(id)response cacheStoragePolicy:(NSURLCacheStoragePolicy)policy {
  106. EbrDebugLog("URL protocol did receive response\n");
  107. if(_mutableData==nil)
  108. _mutableData = [[NSMutableData alloc] init];
  109. /*
  110. if ( [response respondsToSelector:@selector(statusCode)] && [response statusCode] != 200 ) {
  111. [_delegate setError:[NSError errorWithDomain:@"Bad response code" code:[response statusCode] userInfo:nil]];
  112. }
  113. */
  114. _response = [response retain];
  115. _storagePolicy=policy;
  116. if ( [_delegate respondsToSelector:@selector(connection:willCacheResponse:)])
  117. [_delegate connection:self willCacheResponse:response];
  118. if ( [_delegate respondsToSelector:@selector(connection:didReceiveResponse:)]) {
  119. [_delegate connection:self didReceiveResponse:response];
  120. }
  121. }
  122. /* annotate with type */ -(void) URLProtocol:(id)urlProtocol didLoadData:(id)data {
  123. EbrDebugLog("URL protocol did load data\n");
  124. if(_mutableData==nil)
  125. _mutableData = [[NSMutableData alloc] init];
  126. if ( ![_request _shouldDiscardData] ) {
  127. [_mutableData appendData:data];
  128. }
  129. if ( [_delegate respondsToSelector:@selector(connection:didReceiveData:)] ) {
  130. [_delegate connection:self didReceiveData:data];
  131. }
  132. }
  133. /* annotate with type */ -(void) URLProtocolDidFinishLoading:(id)urlProtocol {
  134. EbrDebugLog("URL protocol did finish loading\n");
  135. /*
  136. if(_storagePolicy==NSURLCacheStorageNotAllowed) {
  137. //[[NSURLCache sharedURLCache] removeCachedResponseForRequest:_request];
  138. } else {
  139. //NSCachedURLResponse *cachedResponse=[[NSCachedURLResponse alloc] initWithResponse:_response data:_mutableData userInfo:nil storagePolicy:_storagePolicy];
  140. //if([_delegate respondsToSelector:@selector(connection:willCacheResponse:)])
  141. //cachedResponse=[_delegate connection:self willCacheResponse:cachedResponse];
  142. //if(cachedResponse!=nil){
  143. //[[NSURLCache sharedURLCache] storeCachedResponse:cachedResponse forRequest:_request];
  144. //}
  145. }
  146. */
  147. if ([_delegate respondsToSelector:@selector(connectionDidFinishLoading:)])
  148. [_delegate performSelector:@selector(connectionDidFinishLoading:) withObject:self];
  149. if ( _didRetain && !_didRelease ) {
  150. _didRelease = TRUE;
  151. [self autorelease];
  152. }
  153. [_delegate autorelease];
  154. _delegate = nil;
  155. }
  156. /* annotate with type */ -(void) cancel {
  157. [_protocol stopLoading];
  158. if ( _didRetain && !_didRelease ) {
  159. _didRelease = TRUE;
  160. [self autorelease];
  161. }
  162. }
  163. /* annotate with type */ -(void) URLProtocol:(id)urlProtocol didReceiveAuthenticationChallenge:(id)challenge {
  164. if ( [_delegate respondsToSelector:@selector(connection:willSendRequestForAuthenticationChallenge:)] ) {
  165. [_delegate connection:self willSendRequestForAuthenticationChallenge:challenge];
  166. } else {
  167. [_delegate connection:self didReceiveAuthenticationChallenge:challenge];
  168. }
  169. }
  170. #if 0
  171. -(void)URLProtocol:(NSURLProtocol *)urlProtocol wasRedirectedToRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirect {
  172. [_delegate connection:self willSendRequest:request redirectResponse:redirect];
  173. }
  174. -(void)URLProtocol:(NSURLProtocol *)urlProtocol didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  175. // [_delegate connection:self didCancelAuthenticationChallenge];
  176. }
  177. -(void)URLProtocol:(NSURLProtocol *)urlProtocol cachedResponseIsValid:(NSCachedURLResponse *)cachedResponse {
  178. }
  179. #endif
  180. #if 0
  181. /* annotate with type */ -(id) initWithRequest:(id)url delegate:(id)delegate startImmediately:(DWORD)immediately {
  182. _delegate = delegate;
  183. if ( immediately ) [self start];
  184. return self;
  185. }
  186. /* annotate with type */ -(id) unscheduleFromRunLoop:(id)runLoop forMode:(id)mode {
  187. if ( _timer != nil ) {
  188. [_timer invalidate];
  189. _timer = nil;
  190. }
  191. return self;
  192. }
  193. /* annotate with type */ -(id) scheduleInRunLoop:(id)runLoop forMode:(id)mode {
  194. if ( _timer != nil ) return self;
  195. _timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:0.0 target:self selector:@selector(_notifyFailure) userInfo:0 repeats:FALSE];
  196. [runLoop addTimer:_timer forMode:mode];
  197. [runLoop _wakeUp];
  198. return self;
  199. }
  200. /* annotate with type */ +(id) sendSynchronousRequest:(id)request returningResponse:(id *)response error:(id *)error {
  201. if ( response ) *response = nil;
  202. if ( error ) *error = [NSError errorWithDomain:@"socket" code:100 userInfo:nil];
  203. return nil;
  204. }
  205. /* annotate with type */ +(id) connectionWithRequest:(id)request delegate:(id)delegate {
  206. return [[self alloc] initWithRequest:request delegate:delegate];
  207. }
  208. /* annotate with type */ +(id) canHandleRequest:(id)request {
  209. return FALSE;
  210. }
  211. -(id) /* use typed version */ _notifyFailure {
  212. if ( [_delegate respondsToSelector:@selector(connection:didFailWithError:)] ) {
  213. [_delegate connection:self didFailWithError:nil];
  214. }
  215. return nil;
  216. }
  217. -(id) /* use typed version */ start {
  218. [self scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:@"kCFRunLoopDefaultMode"];
  219. return nil;
  220. }
  221. -(id) /* use typed version */ cancel {
  222. return nil;
  223. }
  224. -(id) /* use typed version */ dealloc {
  225. _delegate = nil;
  226. return [super dealloc];
  227. }
  228. #endif
  229. /* annotate with type */ -(id) retain {
  230. return [super retain];
  231. }
  232. -(void) release {
  233. [super release];
  234. }
  235. /* annotate with type */ -(id) autorelease {
  236. return [super autorelease];
  237. }
  238. /* annotate with type */ -(id) _protocol {
  239. return _protocol;
  240. }
  241. @end