| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Foundation/Foundation.h> |
| 6 |
| 7 #import "CrNet.h" |
| 8 |
| 9 #import "crnet_consumer_app_delegate.h" |
| 10 |
| 11 @interface TestDelegate : NSObject<NSURLSessionDelegate, |
| 12 NSURLSessionDataDelegate, |
| 13 NSURLSessionTaskDelegate> |
| 14 |
| 15 - (id)initWithSemaphore:(dispatch_semaphore_t)sem; |
| 16 |
| 17 @end |
| 18 |
| 19 @implementation TestDelegate { |
| 20 dispatch_semaphore_t _sem; |
| 21 } |
| 22 |
| 23 - (id)initWithSemaphore:(dispatch_semaphore_t)sem { |
| 24 _sem = sem; |
| 25 return self; |
| 26 } |
| 27 |
| 28 - (void)URLSession:(NSURLSession*)session |
| 29 didBecomeInvalidWithError:(NSError*)error { |
| 30 NSLog(@"URLSession didBecomeInvalidWithError %@", error); |
| 31 } |
| 32 |
| 33 - (void)URLSession:(NSURLSession*)session |
| 34 task:(NSURLSessionTask*)task |
| 35 didCompleteWithError:(NSError*)error { |
| 36 NSLog(@"URLSessionTask didCompleteWithError %@", error); |
| 37 dispatch_semaphore_signal(_sem); |
| 38 } |
| 39 |
| 40 - (void)URLSession:(NSURLSession*)session |
| 41 task:(NSURLSessionTask*)task |
| 42 didReceiveChallenge:(NSURLAuthenticationChallenge*)challenge |
| 43 completionHandler: |
| 44 (void (^)(NSURLSessionAuthChallengeDisposition disp, |
| 45 NSURLCredential* credential))completionHandler { |
| 46 NSLog(@"URLSessionTask didReceiveChallenge %@", challenge); |
| 47 completionHandler(NSURLSessionAuthChallengeUseCredential, nil); |
| 48 } |
| 49 |
| 50 - (void)URLSession:(NSURLSession*)session |
| 51 task:(NSURLSessionTask*)task |
| 52 willPerformHTTPRedirection:(NSHTTPURLResponse*)response |
| 53 newRequest:(NSURLRequest*)request |
| 54 completionHandler:(void (^)(NSURLRequest*))completionHandler { |
| 55 NSLog(@"URLSessionTask willPerformHttpRedirection %@", request); |
| 56 completionHandler(request); |
| 57 } |
| 58 |
| 59 - (void)URLSession:(NSURLSession*)session |
| 60 dataTask:(NSURLSessionDataTask*)dataTask |
| 61 didReceiveResponse:(NSURLResponse*)response |
| 62 completionHandler:(void (^)(NSURLSessionResponseDisposition disposition)) |
| 63 completionHandler { |
| 64 NSHTTPURLResponse* resp = (NSHTTPURLResponse*)response; |
| 65 NSLog(@"URLSessionDataTask didReceiveResponse status %ld", |
| 66 (long)resp.statusCode); |
| 67 completionHandler(NSURLSessionResponseAllow); |
| 68 } |
| 69 |
| 70 - (void)URLSession:(NSURLSession*)session |
| 71 dataTask:(NSURLSessionDataTask*)dataTask |
| 72 didReceiveData:(NSData*)data { |
| 73 NSLog(@"URLSessionDataTask didReceiveData %lu bytes", |
| 74 (unsigned long)data.length); |
| 75 } |
| 76 |
| 77 - (void)URLSession:(NSURLSession*)session |
| 78 dataTask:(NSURLSessionDataTask*)dataTask |
| 79 willCacheResponse:(NSCachedURLResponse*)proposedResponse |
| 80 completionHandler: |
| 81 (void (^)(NSCachedURLResponse* cachedResponse))completionHandler { |
| 82 NSLog(@"URLSessionDataTask willCacheResponse %@", proposedResponse); |
| 83 completionHandler(proposedResponse); |
| 84 } |
| 85 |
| 86 @end |
| 87 |
| 88 void use_crnet(NSURLSessionConfiguration* config) { |
| 89 [CrNet setPartialUserAgent:@"Foo/1.0"]; |
| 90 [CrNet install]; |
| 91 [CrNet installIntoSessionConfiguration:config]; |
| 92 } |
| 93 |
| 94 void fetch(NSURLSession* session, NSString* url) { |
| 95 NSURL* testURL = [NSURL URLWithString:url]; |
| 96 NSURLSessionDataTask* task = [session dataTaskWithURL:testURL]; |
| 97 NSLog(@"fetch: starting %@", url); |
| 98 [task resume]; |
| 99 } |
| 100 |
| 101 int main(int argc, char *argv[]) { |
| 102 dispatch_semaphore_t sem = dispatch_semaphore_create(0); |
| 103 TestDelegate* delegate = [[TestDelegate alloc] initWithSemaphore:sem]; |
| 104 NSURLSessionConfiguration* config = |
| 105 [NSURLSessionConfiguration ephemeralSessionConfiguration]; |
| 106 NSURLSession* session = [NSURLSession sessionWithConfiguration:config |
| 107 delegate:delegate |
| 108 delegateQueue:nil]; |
| 109 |
| 110 NSLog(@"main: installing crnet"); |
| 111 use_crnet(config); |
| 112 |
| 113 fetch(session, @"https://www.google.com"); |
| 114 fetch(session, @"https://twitter.com"); |
| 115 fetch(session, @"https://m.facebook.com"); |
| 116 fetch(session, @"https://www.yahoo.com"); |
| 117 |
| 118 int64_t secs = 1000000000LL; |
| 119 secs *= 5LL; |
| 120 NSLog(@"main: waiting"); |
| 121 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 122 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 123 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 124 dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, secs)); |
| 125 NSLog(@"main: timeout, exiting"); |
| 126 } |
| OLD | NEW |