Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows setup headers and dismiss Server Trust #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SVHTTPRequest/SVHTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion SVHTTPRequest/SVHTTPClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<SVHTTPRequestPrivateMethods>)requestOperation setClient:self];

[self.HTTPHeaderFields enumerateKeysAndObjectsUsingBlock:^(NSString *field, NSString *value, BOOL *stop) {
Expand Down
2 changes: 2 additions & 0 deletions SVHTTPRequest/SVHTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 19 additions & 1 deletion SVHTTPRequest/SVHTTPRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ - (NSString*)encodedURLParameterString;

static NSInteger SVHTTPRequestTaskCount = 0;
static NSString *defaultUserAgent;
static NSTimeInterval SVHTTPRequestTimeoutInterval = 20;
static NSTimeInterval SVHTTPRequestTimeoutInterval = 100;

@interface SVHTTPRequest ()

Expand Down Expand Up @@ -367,6 +367,10 @@ - (void)start {
[self increaseSVHTTPRequestTaskCount];
});

if (self.headers) {
[self.operationRequest setAllHTTPHeaderFields:self.headers];
}

if(self.operationParameters)
[self addParametersToRequest:self.operationParameters];

Expand Down Expand Up @@ -491,6 +495,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) {
Expand Down