Skip to content

Commit 4382672

Browse files
author
Olivier Poitrey
committed
Merge pull request SDWebImage#582 from jenshandersson/master
Added new option to allow invalid SSL Certificates. Useful for testing
2 parents b201e13 + 706c573 commit 4382672

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

SDWebImage/SDWebImageDownloader.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@ typedef enum
3333
* Handles cookies stored in NSHTTPCookieStore by setting
3434
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
3535
*/
36-
SDWebImageDownloaderHandleCookies = 1 << 5
36+
SDWebImageDownloaderHandleCookies = 1 << 5,
37+
/**
38+
* Enable to allow untrusted SSL ceriticates.
39+
* Useful for testing purposes. Use with caution in production.
40+
*/
41+
SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6
3742

3843
} SDWebImageDownloaderOptions;
3944

SDWebImage/SDWebImageDownloaderOperation.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,5 +371,21 @@ - (BOOL)shouldContinueWhenAppEntersBackground
371371
return self.options & SDWebImageDownloaderContinueInBackground;
372372
}
373373

374+
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
375+
{
376+
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
377+
}
378+
379+
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
380+
{
381+
BOOL trustAllCertificates = (self.options & SDWebImageDownloaderAllowInvalidSSLCertificates);
382+
if (trustAllCertificates && [challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
383+
{
384+
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
385+
forAuthenticationChallenge:challenge];
386+
}
387+
388+
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
389+
}
374390

375391
@end

SDWebImage/SDWebImageManager.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ typedef enum
5151
* Handles cookies stored in NSHTTPCookieStore by setting
5252
* NSMutableURLRequest.HTTPShouldHandleCookies = YES;
5353
*/
54-
SDWebImageHandleCookies = 1 << 6
54+
SDWebImageHandleCookies = 1 << 6,
55+
/**
56+
* Enable to allow untrusted SSL ceriticates.
57+
* Useful for testing purposes. Use with caution in production.
58+
*/
59+
SDWebImageAllowInvalidSSLCertificates = 1 << 7
5560
} SDWebImageOptions;
5661

5762
typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType);

SDWebImage/SDWebImageManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ - (BOOL)diskImageExistsForURL:(NSURL *)url
146146
if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderUseNSURLCache;
147147
if (options & SDWebImageContinueInBackground) downloaderOptions |= SDWebImageDownloaderContinueInBackground;
148148
if (options & SDWebImageHandleCookies) downloaderOptions |= SDWebImageDownloaderHandleCookies;
149+
if (options & SDWebImageAllowInvalidSSLCertificates) downloaderOptions |= SDWebImageDownloaderAllowInvalidSSLCertificates;
149150
if (image && options & SDWebImageRefreshCached)
150151
{
151152
// force progressive off if image already cached but forced refreshing

0 commit comments

Comments
 (0)