From d32d4da1b105a8f1ab79abf9ce86e153ee3825d7 Mon Sep 17 00:00:00 2001 From: ccarnero Date: Fri, 14 Mar 2014 13:27:23 -0300 Subject: [PATCH 1/2] Can dismiss NSURLAuthenticationMethodServerTrust. Can set custom headers --- SVHTTPRequest/SVHTTPClient.h | 3 +++ SVHTTPRequest/SVHTTPClient.m | 4 +++- SVHTTPRequest/SVHTTPRequest.h | 2 ++ SVHTTPRequest/SVHTTPRequest.m | 14 ++++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/SVHTTPRequest/SVHTTPClient.h b/SVHTTPRequest/SVHTTPClient.h index 1f397f9..21437f5 100644 --- a/SVHTTPRequest/SVHTTPClient.h +++ b/SVHTTPRequest/SVHTTPClient.h @@ -46,4 +46,7 @@ typedef void (^SVHTTPRequestCompletionHandler)(id response, NSHTTPURLResponse *u @property (nonatomic, readwrite) NSURLRequestCachePolicy cachePolicy; @property (nonatomic, readwrite) NSUInteger timeoutInterval; +@property (nonatomic, readwrite) NSDictionary *headers; +@property (nonatomic, readwrite) BOOL dismissNSURLAuthenticationMethodServerTrust; + @end diff --git a/SVHTTPRequest/SVHTTPClient.m b/SVHTTPRequest/SVHTTPClient.m index c4b9e87..6663ed6 100644 --- a/SVHTTPRequest/SVHTTPClient.m +++ b/SVHTTPRequest/SVHTTPClient.m @@ -153,7 +153,9 @@ - (SVHTTPRequest*)queueRequest:(NSString*)path requestOperation.cachePolicy = self.cachePolicy; requestOperation.userAgent = self.userAgent; requestOperation.timeoutInterval = self.timeoutInterval; - + requestOperation.headers = self.headers; + requestOperation.dismissNSURLAuthenticationMethodServerTrust=_dismissNSURLAuthenticationMethodServerTrust; + [(id)requestOperation setClient:self]; [self.HTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *field, NSString *value, BOOL *stop) { diff --git a/SVHTTPRequest/SVHTTPRequest.h b/SVHTTPRequest/SVHTTPRequest.h index afe2a6b..07b8c9a 100644 --- a/SVHTTPRequest/SVHTTPRequest.h +++ b/SVHTTPRequest/SVHTTPRequest.h @@ -47,6 +47,8 @@ typedef NSUInteger SVHTTPRequestMethod; @property (nonatomic, readwrite) BOOL sendParametersAsJSON; @property (nonatomic, readwrite) NSURLRequestCachePolicy cachePolicy; @property (nonatomic, readwrite) NSUInteger timeoutInterval; +@property (nonatomic, readwrite) NSDictionary *headers; +@property (nonatomic, readwrite) BOOL dismissNSURLAuthenticationMethodServerTrust; @end diff --git a/SVHTTPRequest/SVHTTPRequest.m b/SVHTTPRequest/SVHTTPRequest.m index 54ca010..f718b88 100644 --- a/SVHTTPRequest/SVHTTPRequest.m +++ b/SVHTTPRequest/SVHTTPRequest.m @@ -491,6 +491,20 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon self.operationURLResponse = (NSHTTPURLResponse*)response; } +- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge +{ + if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { + if (_dismissNSURLAuthenticationMethodServerTrust) { + NSURLProtectionSpace * protectionSpace = [challenge protectionSpace]; + NSURLCredential* credentail = [NSURLCredential credentialForTrust:[protectionSpace serverTrust]]; + [[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge]; + } + else { + [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; + } + } +} + - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { dispatch_group_async(self.saveDataDispatchGroup, self.saveDataDispatchQueue, ^{ if(self.operationSavePath) { From f7a8e0897eaa55448a56ce7e63248fc206b50474 Mon Sep 17 00:00:00 2001 From: ccarnero Date: Fri, 14 Mar 2014 18:27:58 -0300 Subject: [PATCH 2/2] add headers to request --- SVHTTPRequest/SVHTTPRequest.m | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/SVHTTPRequest/SVHTTPRequest.m b/SVHTTPRequest/SVHTTPRequest.m index f718b88..ffe540b 100644 --- a/SVHTTPRequest/SVHTTPRequest.m +++ b/SVHTTPRequest/SVHTTPRequest.m @@ -31,7 +31,7 @@ - (NSString*)encodedURLParameterString; static NSInteger SVHTTPRequestTaskCount = 0; static NSString *defaultUserAgent; -static NSTimeInterval SVHTTPRequestTimeoutInterval = 20; +static NSTimeInterval SVHTTPRequestTimeoutInterval = 100; @interface SVHTTPRequest () @@ -367,6 +367,10 @@ - (void)start { [self increaseSVHTTPRequestTaskCount]; }); + if (self.headers) { + [self.operationRequest setAllHTTPHeaderFields:self.headers]; + } + if(self.operationParameters) [self addParametersToRequest:self.operationParameters];