├── HTTPSURLProtocol.h ├── HTTPSURLProtocol.m └── README.md /HTTPSURLProtocol.h: -------------------------------------------------------------------------------- 1 | // 2 | // XHTTPSURLProtocol.h 3 | // XHTTPSURLProtocol 4 | // 5 | // Created by hhfa008 on 13-6-27. 6 | // Copyright (c) 2013年 hhfa008 Inc. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | /** 12 | 该类用于处理https类型的ajax请求 13 | */ 14 | @interface HTTPSURLProtocol : NSURLProtocol 15 | { 16 | NSMutableData* _data; 17 | } 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /HTTPSURLProtocol.m: -------------------------------------------------------------------------------- 1 | // 2 | // HTTPSURLProtocol.m 3 | // HTTPSURLProtocol 4 | // 5 | // Created by hhfa008 on 13-6-27. 6 | // Copyright (c) 2013年 hhfa008 Inc. All rights reserved. 7 | // 8 | 9 | #import "HTTPSURLProtocol.h" 10 | 11 | static BOOL gExchangeCredentialFinished = YES; /**< 用于标记是否完成证书交换 */ 12 | 13 | @implementation HTTPSURLProtocol 14 | 15 | + (BOOL)canInitWithRequest:(NSURLRequest *)theRequest 16 | { 17 | //FIXME: 后来的https类型的reqeust应该也要被处理 18 | return ([[[theRequest URL] scheme] isEqualToString:@"https"] && gExchangeCredentialFinished); 19 | } 20 | 21 | + (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request 22 | { 23 | return request; 24 | } 25 | 26 | - (void)startLoading 27 | { 28 | gExchangeCredentialFinished = NO; 29 | NSURLConnection *theConncetion = [[NSURLConnection alloc] initWithRequest:self.request delegate:self]; 30 | if (theConncetion) { 31 | _data = [NSMutableData data]; 32 | } 33 | } 34 | 35 | - (void)stopLoading 36 | { 37 | // NOTE:如有清理工作,可以在此处添加 38 | } 39 | 40 | + (BOOL)requestIsCacheEquivalent:(NSURLRequest*)requestA toRequest:(NSURLRequest*)requestB 41 | { 42 | return NO; 43 | } 44 | 45 | #pragma mark - NSURLConnectionDelegate 46 | 47 | - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace 48 | { 49 | //响应服务器证书认证请求和客户端证书认证请求 50 | return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust] || 51 | [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]; 52 | } 53 | 54 | - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 55 | { 56 | NSURLCredential* credential; 57 | if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) 58 | { 59 | //服务器证书认证 60 | credential= [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 61 | } 62 | else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) 63 | { 64 | //客户端证书认证 65 | //TODO:设置客户端证书认证 66 | credential = nil; 67 | } 68 | 69 | if (credential != nil) 70 | { 71 | [challenge.sender useCredential:credential forAuthenticationChallenge:challenge]; 72 | } 73 | else 74 | { 75 | [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; 76 | } 77 | } 78 | 79 | - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 80 | { 81 | [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed]; 82 | } 83 | 84 | - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 85 | { 86 | [_data appendData:data]; 87 | } 88 | 89 | - (void)connectionDidFinishLoading:(NSURLConnection *)connection 90 | { 91 | [[self client] URLProtocol:self didLoadData:_data]; 92 | [[self client] URLProtocolDidFinishLoading:self]; 93 | gExchangeCredentialFinished = YES; 94 | } 95 | 96 | -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 97 | { 98 | [[self client] URLProtocol:self didFailWithError:error]; 99 | gExchangeCredentialFinished = YES; 100 | } 101 | 102 | @end 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HTTPSURLProtocol 2 | ================ 3 | 4 | HTTPSURLProtocol, a elegant way to handle https in iOS 5 | --------------------------------------------------------------------------------