ICRURLProtocol.m 4.3 KB
Newer Older
mei's avatar
mei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
//
//  ICRURLProtocol.m
//  XFFruit
//
//  Created by Xummer on 15/4/13.
//  Copyright (c) 2015年 Xummer. All rights reserved.
//

#import "ICRURLProtocol.h"
#import "ICRHTTPController.h"

@interface ICRURLProtocol ()
@property (nonatomic, readwrite, strong) NSURLConnection *connection;
@end

@implementation ICRURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    // IPatrol/rest/attachment/downloadDirect/id
    // -[NSURL pathComponents] = @["/", "IPatrol", "rest", "attachment", "downloadDirect", "file.html"] (note that the initial / is part of it)
    
    NSArray *pathComponents = [request.URL pathComponents];
    if ([pathComponents count] < 5) {
        return NO;
    }
    
    if ([pathComponents[ 1 ] isEqualToString:@"IPatrol"] &&
        [pathComponents[ 2 ] isEqualToString:@"rest"] &&
        [pathComponents[ 3 ] isEqualToString:@"attachment"] &&
        [pathComponents[ 4 ] isEqualToString:@"downloadDirect"] )
    {
        NSSet *trackMethods = [NSSet setWithObjects:@"GET", @"POST", nil];
        
        NSURL *httpUrl = [NSURL URLWithString:HTTP_REST_API_BASE_URL];
        if ([[request.URL host] isEqualToString:[httpUrl host]] &&
            [trackMethods containsObject:[[request HTTPMethod] uppercaseString]] &&
            ![request valueForHTTPHeaderField:@"token"] &&
            ([[request.URL scheme] caseInsensitiveCompare:@"http"] == NSOrderedSame ||
             [[request.URL scheme] caseInsensitiveCompare:@"https"] == NSOrderedSame)) {
                return YES;
            }
    }
    
    return NO;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    return request;
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a
                       toRequest:(NSURLRequest *)b
{
    return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)startLoading {
    // Set Auth
    NSURLRequest *connectionRequest = [self updateAuthForHTTPRequest:self.request];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:connectionRequest
                                                                delegate:self];
    self.connection = connection;
}

- (void)stopLoading {
    [self.connection cancel];
    self.connection = nil;
}

#pragma mark - Private Method
- (NSURLRequest *)updateAuthForHTTPRequest:(NSURLRequest *)request {
    NSString *nsAuthData = [[ICRUserUtil sharedInstance] token];
    
    if (nsAuthData) {
        
        NSMutableURLRequest *mutableReq = [request mutableCopy];
        
        [mutableReq setValue:nsAuthData forHTTPHeaderField:@"token"];
        [mutableReq setValue:nil forHTTPHeaderField:@"Accept"];
        
        return mutableReq;
    }
    
    return request;
}

#pragma mark -

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    [self.client URLProtocol:self
            didFailWithError:error];
}

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
{
    if (response != nil)
    {
        [[self client] URLProtocol:self wasRedirectedToRequest:request redirectResponse:response];
    }
    return request;
}

- (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];
}


- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response
{
    [self.client URLProtocol:self
          didReceiveResponse:response
          cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (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