Skip to content

Commit fbe3b95

Browse files
committed
Added new option to allow invalid SSL Certificates. Useful for testing
1 parent b201e13 commit fbe3b95

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-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+
SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6
38+
/**
39+
* Enable this to allow untrusted SSL ceriticates.
40+
* Useful for testing purposes.
41+
*/
3742

3843
} SDWebImageDownloaderOptions;
3944

SDWebImage/SDWebImageDownloaderOperation.m

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

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

375388
@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+
SDWebImageAllowInvalidSSLCertificates = 1 << 7
56+
/**
57+
* Enable this to allow untrusted SSL ceriticates.
58+
* Useful for testing purposes.
59+
*/
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)