Skip to content

Instantly share code, notes, and snippets.

@yatatsu
Last active August 29, 2015 14:05
Show Gist options
  • Save yatatsu/b807afcf63e08f4e5143 to your computer and use it in GitHub Desktop.
Save yatatsu/b807afcf63e08f4e5143 to your computer and use it in GitHub Desktop.
NSURLProtocolを使ってフィルタリングする
//
// MyURLProtocol.m
//
// Created by kitagawa on 2014/08/21.
// Copyright (c) 2014年 kitagawa. All rights reserved.
//
#import "MyURLProtocol.h"
static NSString *kCustomHeaderKey = @"API-Key";
static NSString *kCustomHeaderValue = @"OK, this is API-Key";
static NSString *kLoadURLString = @"http://localhost:8888/html/test.html";
static NSString *kSamplePostURLString = @"http://httpbin.org/post";
static NSString *kSampleGetURLString = @"http://httpbin.org/get";
static NSString *kURLSchemeHTTP = @"http";
static NSString *kURLSchemeHTTPS = @"https";
@interface MyURLProtocol () <NSURLConnectionDataDelegate, NSURLConnectionDelegate>
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, strong) NSURLConnection *connection;
@end
@implementation MyURLProtocol
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[NSURLProtocol registerClass:[self class]];
});
}
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
NSLog(@"url: %@", request.URL);
NSLog(@"request headers: %@", request.allHTTPHeaderFields);
NSString *scheme = request.URL.scheme;
if ([request valueForHTTPHeaderField:kCustomHeaderKey]) {
return NO;
}
if (([scheme compare:kURLSchemeHTTP] == NSOrderedSame
|| [scheme compare:kURLSchemeHTTPS] == NSOrderedSame)
&& [[self targetURL] containsObject:[request.URL absoluteString]]) {
return YES;
}
return NO;
}
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
return request;
}
- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
{
self = [super initWithRequest:request cachedResponse:cachedResponse client:client];
if (self) {
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest setValue:kCustomHeaderValue forHTTPHeaderField:kCustomHeaderKey];
self.request = (NSURLRequest *)mutableRequest;
}
return self;
}
- (void)startLoading
{
NSLog(@">>>startLoading");
self.connection = [[NSURLConnection alloc]
initWithRequest:[[self class] canonicalRequestForRequest:self.request]
delegate:self startImmediately:YES];
}
- (void)stopLoading
{
NSLog(@">>>stopLoading");
[self.connection cancel];
}
+ (NSArray *)targetURL
{
static dispatch_once_t onceToken;
static NSArray *__array = nil;
dispatch_once(&onceToken, ^{
__array = @[kLoadURLString,
kSampleGetURLString,
kSamplePostURLString];
});
return __array;
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
[[self client] URLProtocol:self didFailWithError:error];
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection {
return YES;
}
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[self client] URLProtocol:self didReceiveAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection
didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[self client] URLProtocol:self didCancelAuthenticationChallenge:challenge];
}
#pragma mark - NSURLConnectionDataDelegate
- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:[[self request] cachePolicy]];
}
- (void)connection:(NSURLConnection *)connection
didReceiveData:(NSData *)data
{
[[self client] URLProtocol:self didLoadData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
return cachedResponse;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[[self client] URLProtocolDidFinishLoading:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment