2014-12-31 10:02:14 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-08-13 15:14:10 +08:00
|
|
|
#include "platform/CCPlatformConfig.h"
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|
|
|
|
|
2016-03-21 20:12:58 +08:00
|
|
|
#import "network/HttpAsynConnection-apple.h"
|
2014-12-31 10:02:14 +08:00
|
|
|
|
Fix major memory leaks in iOS http request
... and other small fixes
Brief:
1. In iOS, whenever you have a new spawned thread with its own run loop (i.e. while true), you must wrap it with an autorelease pool, so it cleans up the objects at the end of each loop cycle.
2. There were a few "alloc" and "new" objects that weren't released.
3. The HttpAsync class itself was missing dealloc, also property assignment should use `self.property` so the previous value gets released as appropriate.
4. Resolved a small warning
**NOTE**: I have a cocos2d-x game that heavily relies on the HTTP requests, with cookies, but without SSL, so I couldn't test the SSL.
I haven't tested the cookies and SSL yet, but for starters, these improvements are meant to fix the memory issues only, not the logic.
2015-02-23 14:38:33 +08:00
|
|
|
@interface HttpAsynConnection ()
|
|
|
|
|
|
|
|
@property (readwrite) NSString *statusString;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
@implementation HttpAsynConnection
|
|
|
|
|
2015-11-16 18:25:26 +08:00
|
|
|
@synthesize srcURL = srcURL;
|
|
|
|
@synthesize sslFile = sslFile;
|
|
|
|
@synthesize responseHeader = responseHeader;
|
|
|
|
@synthesize responseData = responseData;
|
|
|
|
@synthesize getDataTime = getDataTime;
|
|
|
|
@synthesize responseCode = responseCode;
|
|
|
|
@synthesize statusString = statusString;
|
|
|
|
@synthesize responseError = responseError;
|
|
|
|
@synthesize connError = connError;
|
|
|
|
@synthesize conn = conn;
|
|
|
|
@synthesize finish = finish;
|
|
|
|
@synthesize runLoop = runLoop;
|
2014-12-31 10:02:14 +08:00
|
|
|
|
Fix major memory leaks in iOS http request
... and other small fixes
Brief:
1. In iOS, whenever you have a new spawned thread with its own run loop (i.e. while true), you must wrap it with an autorelease pool, so it cleans up the objects at the end of each loop cycle.
2. There were a few "alloc" and "new" objects that weren't released.
3. The HttpAsync class itself was missing dealloc, also property assignment should use `self.property` so the previous value gets released as appropriate.
4. Resolved a small warning
**NOTE**: I have a cocos2d-x game that heavily relies on the HTTP requests, with cookies, but without SSL, so I couldn't test the SSL.
I haven't tested the cookies and SSL yet, but for starters, these improvements are meant to fix the memory issues only, not the logic.
2015-02-23 14:38:33 +08:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[srcURL release];
|
|
|
|
[sslFile release];
|
|
|
|
[responseHeader release];
|
|
|
|
[responseData release];
|
|
|
|
[responseError release];
|
|
|
|
[conn release];
|
|
|
|
[runLoop release];
|
2015-05-29 17:13:49 +08:00
|
|
|
[connError release];
|
Fix major memory leaks in iOS http request
... and other small fixes
Brief:
1. In iOS, whenever you have a new spawned thread with its own run loop (i.e. while true), you must wrap it with an autorelease pool, so it cleans up the objects at the end of each loop cycle.
2. There were a few "alloc" and "new" objects that weren't released.
3. The HttpAsync class itself was missing dealloc, also property assignment should use `self.property` so the previous value gets released as appropriate.
4. Resolved a small warning
**NOTE**: I have a cocos2d-x game that heavily relies on the HTTP requests, with cookies, but without SSL, so I couldn't test the SSL.
I haven't tested the cookies and SSL yet, but for starters, these improvements are meant to fix the memory issues only, not the logic.
2015-02-23 14:38:33 +08:00
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
- (void) startRequest:(NSURLRequest *)request
|
|
|
|
{
|
2015-07-17 14:23:51 +08:00
|
|
|
#ifdef COCOS2D_DEBUG
|
2014-12-31 10:02:14 +08:00
|
|
|
NSLog(@"Starting to load %@", srcURL);
|
2015-07-17 14:23:51 +08:00
|
|
|
#endif
|
|
|
|
|
2014-12-31 10:02:14 +08:00
|
|
|
finish = false;
|
|
|
|
|
2015-02-25 14:56:25 +08:00
|
|
|
self.responseData = [NSMutableData data];
|
2014-12-31 10:02:14 +08:00
|
|
|
getDataTime = 0;
|
2015-03-12 09:06:53 +08:00
|
|
|
|
Fix major memory leaks in iOS http request
... and other small fixes
Brief:
1. In iOS, whenever you have a new spawned thread with its own run loop (i.e. while true), you must wrap it with an autorelease pool, so it cleans up the objects at the end of each loop cycle.
2. There were a few "alloc" and "new" objects that weren't released.
3. The HttpAsync class itself was missing dealloc, also property assignment should use `self.property` so the previous value gets released as appropriate.
4. Resolved a small warning
**NOTE**: I have a cocos2d-x game that heavily relies on the HTTP requests, with cookies, but without SSL, so I couldn't test the SSL.
I haven't tested the cookies and SSL yet, but for starters, these improvements are meant to fix the memory issues only, not the logic.
2015-02-23 14:38:33 +08:00
|
|
|
self.responseError = nil;
|
2015-03-12 09:06:53 +08:00
|
|
|
self.connError = nil;
|
2014-12-31 10:02:14 +08:00
|
|
|
|
|
|
|
// create the connection with the target request and this class as the delegate
|
2015-02-25 14:56:25 +08:00
|
|
|
self.conn = [[[NSURLConnection alloc] initWithRequest:request
|
|
|
|
delegate:self
|
|
|
|
startImmediately:NO] autorelease];
|
2014-12-31 10:02:14 +08:00
|
|
|
|
|
|
|
[self.conn scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
|
|
|
|
|
|
|
|
// start the connection
|
|
|
|
[self.conn start];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark NSURLConnectionDelegate methods
|
|
|
|
/**
|
|
|
|
* This delegate method is called when the NSURLConnection connects to the server. It contains the
|
|
|
|
* NSURLResponse object with the headers returned by the server. This method may be called multiple times.
|
|
|
|
* Therefore, it is important to reset the data on each call. Do not assume that it is the first call
|
|
|
|
* of this method.
|
|
|
|
**/
|
|
|
|
- (void) connection:(NSURLConnection *)connection
|
|
|
|
didReceiveResponse:(NSURLResponse *)response {
|
2015-07-17 14:23:51 +08:00
|
|
|
#ifdef COCOS2D_DEBUG
|
2014-12-31 10:02:14 +08:00
|
|
|
NSLog(@"Received response from request to url %@", srcURL);
|
2015-07-17 14:23:51 +08:00
|
|
|
#endif
|
2014-12-31 10:02:14 +08:00
|
|
|
|
|
|
|
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
|
|
|
|
//NSLog(@"All headers = %@", [httpResponse allHeaderFields]);
|
2015-02-23 15:52:49 +08:00
|
|
|
self.responseHeader = [httpResponse allHeaderFields];
|
2014-12-31 10:02:14 +08:00
|
|
|
|
|
|
|
responseCode = httpResponse.statusCode;
|
2015-02-25 14:52:15 +08:00
|
|
|
self.statusString = [NSHTTPURLResponse localizedStringForStatusCode:responseCode];
|
2015-01-08 00:23:27 +08:00
|
|
|
if(responseCode == 200)
|
Fix major memory leaks in iOS http request
... and other small fixes
Brief:
1. In iOS, whenever you have a new spawned thread with its own run loop (i.e. while true), you must wrap it with an autorelease pool, so it cleans up the objects at the end of each loop cycle.
2. There were a few "alloc" and "new" objects that weren't released.
3. The HttpAsync class itself was missing dealloc, also property assignment should use `self.property` so the previous value gets released as appropriate.
4. Resolved a small warning
**NOTE**: I have a cocos2d-x game that heavily relies on the HTTP requests, with cookies, but without SSL, so I couldn't test the SSL.
I haven't tested the cookies and SSL yet, but for starters, these improvements are meant to fix the memory issues only, not the logic.
2015-02-23 14:38:33 +08:00
|
|
|
self.statusString = @"OK";
|
2015-01-08 10:12:45 +08:00
|
|
|
|
|
|
|
/*The individual values of the numeric status codes defined for HTTP/1.1
|
2015-03-27 11:59:10 +08:00
|
|
|
| "200" ; OK
|
|
|
|
| "201" ; Created
|
|
|
|
| "202" ; Accepted
|
|
|
|
| "203" ; Non-Authoritative Information
|
|
|
|
| "204" ; No Content
|
|
|
|
| "205" ; Reset Content
|
|
|
|
| "206" ; Partial Content
|
2015-01-08 10:12:45 +08:00
|
|
|
*/
|
2015-02-27 20:31:33 +08:00
|
|
|
if (responseCode < 200 || responseCode >= 300)
|
|
|
|
{// something went wrong, abort the whole thing
|
|
|
|
self.responseError = [NSError errorWithDomain:@"CCBackendDomain"
|
|
|
|
code:responseCode
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: @"Bad HTTP Response Code"}];
|
|
|
|
}
|
2014-12-31 10:02:14 +08:00
|
|
|
|
|
|
|
[responseData setLength:0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This delegate method is called for each chunk of data received from the server. The chunk size
|
|
|
|
* is dependent on the network type and the server configuration.
|
|
|
|
*/
|
|
|
|
- (void)connection:(NSURLConnection *)connection
|
|
|
|
didReceiveData:(NSData *)data
|
|
|
|
{
|
|
|
|
//NSLog(@"get some data");
|
|
|
|
[responseData appendData:data];
|
|
|
|
getDataTime++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-11 18:15:34 +08:00
|
|
|
* This delegate method is called if the connection cannot be established to the server.
|
2014-12-31 10:02:14 +08:00
|
|
|
* The error object will have a description of the error
|
|
|
|
**/
|
|
|
|
- (void)connection:(NSURLConnection *)connection
|
|
|
|
didFailWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
//NSLog(@"Load failed with error %@", [error localizedDescription]);
|
2015-03-12 09:06:53 +08:00
|
|
|
self.connError = error;
|
2014-12-31 10:02:14 +08:00
|
|
|
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This delegate method is called when the data load is complete. The delegate will be released
|
|
|
|
* following this call
|
|
|
|
**/
|
|
|
|
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
|
|
|
|
{
|
|
|
|
finish = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Server evaluates client's certificate
|
|
|
|
- (BOOL) shouldTrustProtectionSpace:(NSURLProtectionSpace*)protectionSpace
|
|
|
|
{
|
|
|
|
if(sslFile == nil)
|
|
|
|
return YES;
|
|
|
|
//load the bundle client certificate
|
|
|
|
NSString *certPath = [[NSBundle mainBundle] pathForResource:sslFile ofType:@"der"];
|
|
|
|
NSData *certData = [[NSData alloc] initWithContentsOfFile:certPath];
|
|
|
|
CFDataRef certDataRef = (CFDataRef)certData;
|
|
|
|
SecCertificateRef cert = SecCertificateCreateWithData(NULL, certDataRef);
|
|
|
|
|
|
|
|
//Establish a chain of trust anchored on our bundled certificate
|
|
|
|
CFArrayRef certArrayRef = CFArrayCreate(NULL, (void*)&cert, 1, NULL);
|
|
|
|
SecTrustRef serverTrust = protectionSpace.serverTrust;
|
|
|
|
SecTrustSetAnchorCertificates(serverTrust, certArrayRef);
|
|
|
|
|
|
|
|
//Verify that trust
|
|
|
|
SecTrustResultType trustResult;
|
|
|
|
SecTrustEvaluate(serverTrust, &trustResult);
|
|
|
|
|
|
|
|
if(trustResult == kSecTrustResultRecoverableTrustFailure)
|
|
|
|
{
|
|
|
|
CFDataRef errDataRef = SecTrustCopyExceptions(serverTrust);
|
|
|
|
SecTrustSetExceptions(serverTrust, errDataRef);
|
|
|
|
SecTrustEvaluate(serverTrust, &trustResult);
|
2015-05-29 17:13:49 +08:00
|
|
|
[(id)errDataRef release];
|
2014-12-31 10:02:14 +08:00
|
|
|
}
|
2015-05-29 17:13:49 +08:00
|
|
|
[certData release];
|
|
|
|
[(id)certArrayRef release];
|
|
|
|
[(id)certArrayRef release];
|
2014-12-31 10:02:14 +08:00
|
|
|
//Did our custom trust chain evaluate successfully?
|
|
|
|
return trustResult = kSecTrustResultUnspecified || trustResult == kSecTrustResultProceed;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
|
|
|
|
{
|
|
|
|
id <NSURLAuthenticationChallengeSender> sender = challenge.sender;
|
|
|
|
NSURLProtectionSpace *protectionSpace = challenge.protectionSpace;
|
|
|
|
|
|
|
|
//Should server trust client?
|
|
|
|
if([self shouldTrustProtectionSpace:protectionSpace])
|
|
|
|
{
|
|
|
|
SecTrustRef trust = [protectionSpace serverTrust];
|
|
|
|
//
|
|
|
|
// SecCertificateRef certificate = SecTrustGetCertificateAtIndex(trust, 0);
|
|
|
|
//
|
|
|
|
// NSData *serverCertificateData = (NSData*)SecCertificateCopyData(certificate);
|
|
|
|
// NSString *serverCertificateDataHash = [[serverCertificateData base64EncodedString] ]
|
|
|
|
NSURLCredential *credential = [NSURLCredential credentialForTrust:trust];
|
|
|
|
[sender useCredential:credential forAuthenticationChallenge:challenge];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
[sender cancelAuthenticationChallenge:challenge];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2015-08-13 15:14:10 +08:00
|
|
|
|
|
|
|
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
|