Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / ios / net / crn_http_protocol_handler.mm
blobe5479ceff2fb122bcbac0473c046f07620d7fb9c
1 // Copyright 2012 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.
5 #import "ios/net/crn_http_protocol_handler.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/mac/bind_objc_block.h"
10 #include "base/mac/scoped_nsobject.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #import "ios/net/clients/crn_network_client_protocol.h"
17 #import "ios/net/crn_http_protocol_handler_proxy_with_client_thread.h"
18 #import "ios/net/http_protocol_logging.h"
19 #include "ios/net/nsurlrequest_util.h"
20 #import "ios/net/protocol_handler_util.h"
21 #include "ios/net/request_tracker.h"
22 #include "net/base/auth.h"
23 #include "net/base/elements_upload_data_stream.h"
24 #include "net/base/io_buffer.h"
25 #include "net/base/load_flags.h"
26 #import "net/base/mac/url_conversions.h"
27 #include "net/base/net_errors.h"
28 #include "net/base/net_util.h"
29 #include "net/base/upload_bytes_element_reader.h"
30 #include "net/http/http_request_headers.h"
31 #include "net/url_request/redirect_info.h"
32 #include "net/url_request/url_request.h"
33 #include "net/url_request/url_request_context.h"
34 #include "net/url_request/url_request_context_getter.h"
36 namespace net {
37 class HttpProtocolHandlerCore;
40 namespace {
42 // Size of the buffer used to read the net::URLRequest.
43 const int kIOBufferSize = 4096;
45 // Global instance of the HTTPProtocolHandlerDelegate.
46 net::HTTPProtocolHandlerDelegate* g_protocol_handler_delegate = nullptr;
48 // Empty callback.
49 void DoNothing(bool flag) {}
51 }  // namespace
53 // Bridge class to forward NSStream events to the HttpProtocolHandlerCore.
54 // Lives on the IO thread.
55 @interface CRWHTTPStreamDelegate : NSObject<NSStreamDelegate> {
56  @private
57   // The object is owned by |_core| and has a weak reference to it.
58   __weak net::HttpProtocolHandlerCore* _core;
60 - (instancetype)initWithHttpProtocolHandlerCore:
61     (net::HttpProtocolHandlerCore*)core;
62 // NSStreamDelegate method.
63 - (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent;
64 @end
66 #pragma mark -
67 #pragma mark HttpProtocolHandlerCore
69 namespace net {
71 // static
72 void HTTPProtocolHandlerDelegate::SetInstance(
73     HTTPProtocolHandlerDelegate* delegate) {
74   g_protocol_handler_delegate = delegate;
77 // The HttpProtocolHandlerCore class is the bridge between the URLRequest
78 // and the NSURLProtocolClient.
79 // Threading and ownership details:
80 // - The HttpProtocolHandlerCore is owned by the HttpProtocolHandler
81 // - The HttpProtocolHandler is owned by the system and can be deleted anytime
82 // - All the methods of HttpProtocolHandlerCore must be called on the IO thread,
83 //   except its constructor that can be called from any thread.
85 // Implementation notes from Apple's "Read Me About CustomHttpProtocolHandler":
87 // An NSURLProtocol subclass is expected to call the various methods of the
88 // NSURLProtocolClient from the loading thread, including all of the following:
89 //  -URLProtocol:wasRedirectedToRequest:redirectResponse:
90 //  -URLProtocol:didReceiveResponse:cacheStoragePolicy:
91 //  -URLProtocol:didLoadData:
92 //  -URLProtocol:didFinishLoading:
93 //  -URLProtocol:didFailWithError:
94 //  -URLProtocol:didReceiveAuthenticationChallenge:
95 //  -URLProtocol:didCancelAuthenticationChallenge:
97 // The NSURLProtocol subclass must call the client callbacks in the expected
98 // order. This breaks down into three phases:
99 //  o pre-response -- In the initial phase the NSURLProtocol can make any number
100 //    of -URLProtocol:wasRedirectedToRequest:redirectResponse: and
101 //    -URLProtocol:didReceiveAuthenticationChallenge: callbacks.
102 //  o response -- It must then call
103 //    -URLProtocol:didReceiveResponse:cacheStoragePolicy: to indicate the
104 //    arrival of a definitive response.
105 //  o post-response -- After receive a response it may then make any number of
106 //    -URLProtocol:didLoadData: callbacks, followed by a
107 //    -URLProtocolDidFinishLoading: callback.
109 // The -URLProtocol:didFailWithError: callback can be made at any time
110 // (although keep in mind the following point).
112 // The NSProtocol subclass must only send one authentication challenge to the
113 // client at a time. After calling
114 // -URLProtocol:didReceiveAuthenticationChallenge:, it must wait for the client
115 // to resolve the challenge before calling any callbacks other than
116 // -URLProtocol:didCancelAuthenticationChallenge:. This means that, if the
117 // connection fails while there is an outstanding authentication challenge, the
118 // NSURLProtocol subclass must call
119 // -URLProtocol:didCancelAuthenticationChallenge: before calling
120 // -URLProtocol:didFailWithError:.
121 class HttpProtocolHandlerCore
122     : public base::RefCountedThreadSafe<HttpProtocolHandlerCore,
123                                         HttpProtocolHandlerCore>,
124       public URLRequest::Delegate {
125  public:
126   HttpProtocolHandlerCore(NSURLRequest* request);
127   // Starts the network request, and forwards the data downloaded from the
128   // network to |base_client|.
129   void Start(id<CRNNetworkClientProtocol> base_client);
130   // Cancels the request.
131   void Cancel();
132   // Called by NSStreamDelegate. Used for POST requests having a HTTPBodyStream.
133   void HandleStreamEvent(NSStream* stream, NSStreamEvent event);
135   // URLRequest::Delegate methods:
136   void OnReceivedRedirect(URLRequest* request,
137                           const RedirectInfo& new_url,
138                           bool* defer_redirect) override;
139   void OnAuthRequired(URLRequest* request,
140                       AuthChallengeInfo* auth_info) override;
141   void OnCertificateRequested(URLRequest* request,
142                               SSLCertRequestInfo* cert_request_info) override;
143   void OnSSLCertificateError(URLRequest* request,
144                              const SSLInfo& ssl_info,
145                              bool fatal) override;
146   void OnResponseStarted(URLRequest* request) override;
147   void OnReadCompleted(URLRequest* request, int bytes_read) override;
149  private:
150   friend class base::RefCountedThreadSafe<HttpProtocolHandlerCore,
151                                           HttpProtocolHandlerCore>;
152   friend class base::DeleteHelper<HttpProtocolHandlerCore>;
153   ~HttpProtocolHandlerCore() override;
155   // RefCountedThreadSafe traits implementation:
156   static void Destruct(const HttpProtocolHandlerCore* x);
158   void SetLoadFlags();
159   void StopNetRequest();
160   // Stop listening the delegate on the IO run loop.
161   void StopListeningStream(NSStream* stream);
162   NSInteger IOSErrorCode(int os_error);
163   void StopRequestWithError(NSInteger ns_error_code, int net_error_code);
164   // Pass an authentication result provided by a client down to the network
165   // request. |auth_ok| is true if the authentication was successful, false
166   // otherwise. |username| and |password| should be populated with the correct
167   // credentials if |auth_ok| is true.
168   void CompleteAuthentication(bool auth_ok,
169                               const base::string16& username,
170                               const base::string16& password);
171   void StripPostSpecificHeaders(NSMutableURLRequest* request);
172   void CancelAfterSSLError();
173   void ContinueAfterSSLError();
174   void SSLErrorCallback(bool carryOn);
175   void HostStateCallback(bool carryOn);
176   void StartReading();
177   // Pushes |client| at the end of the |clients_| array and sets it as the top
178   // level client.
179   void PushClient(id<CRNNetworkClientProtocol> client);
180   // Pushes all of the clients in |clients|, calling PushClient() on each one.
181   void PushClients(NSArray* clients);
183   base::ThreadChecker thread_checker_;
185   // Contains CRNNetworkClientProtocol objects. The first client is the original
186   // NSURLProtocol client, and the following clients are ordered such as the
187   // ith client is responsible for managing the (i-1)th client.
188   base::scoped_nsobject<NSMutableArray> clients_;
189   // Weak. This is the last client in |clients_|.
190   id<CRNNetworkClientProtocol> top_level_client_;
191   scoped_refptr<IOBuffer> buffer_;
192   base::scoped_nsobject<NSMutableURLRequest> request_;
193   // Stream delegate to read the HTTPBodyStream.
194   base::scoped_nsobject<CRWHTTPStreamDelegate> stream_delegate_;
195   // Vector of readers used to accumulate a POST data stream.
196   ScopedVector<UploadElementReader> post_data_readers_;
198   // This cannot be a scoped pointer because it must be deleted on the IO
199   // thread.
200   URLRequest* net_request_;
202   base::WeakPtr<RequestTracker> tracker_;
204   DISALLOW_COPY_AND_ASSIGN(HttpProtocolHandlerCore);
207 HttpProtocolHandlerCore::HttpProtocolHandlerCore(NSURLRequest* request)
208     : clients_([[NSMutableArray alloc] init]),
209       top_level_client_(nil),
210       buffer_(new IOBuffer(kIOBufferSize)),
211       net_request_(nullptr) {
212   // The request will be accessed from another thread. It is safer to make a
213   // copy to avoid conflicts.
214   // The copy is mutable, because that request will be given to the client in
215   // case of a redirect, but with a different URL. The URL must be created
216   // from the absoluteString of the original URL, because mutableCopy only
217   // shallowly copies the request, and just retains the non-threadsafe NSURL.
218   thread_checker_.DetachFromThread();
219   request_.reset([request mutableCopy]);
220   [request_ setURL:[NSURL URLWithString:[[request URL] absoluteString]]];
223 void HttpProtocolHandlerCore::HandleStreamEvent(NSStream* stream,
224                                                 NSStreamEvent event) {
225   DCHECK(thread_checker_.CalledOnValidThread());
226   DCHECK(stream_delegate_);
227   switch (event) {
228     case NSStreamEventErrorOccurred:
229       DLOG(ERROR)
230           << "Failed to read POST data: "
231           << base::SysNSStringToUTF8([[stream streamError] description]);
232       StopListeningStream(stream);
233       StopRequestWithError(NSURLErrorUnknown, ERR_UNEXPECTED);
234       break;
235     case NSStreamEventEndEncountered:
236       StopListeningStream(stream);
237       if (!post_data_readers_.empty()) {
238         // NOTE: This call will result in |post_data_readers_| being cleared,
239         // which is the desired behavior.
240         net_request_->set_upload(make_scoped_ptr(
241             new ElementsUploadDataStream(post_data_readers_.Pass(), 0)));
242         DCHECK(post_data_readers_.empty());
243       }
244       net_request_->Start();
245       if (tracker_)
246         tracker_->StartRequest(net_request_);
247       break;
248     case NSStreamEventHasBytesAvailable: {
249       NSUInteger length;
250       DCHECK([stream isKindOfClass:[NSInputStream class]]);
251       length = [(NSInputStream*)stream read:(unsigned char*)buffer_->data()
252                                   maxLength:kIOBufferSize];
253       if (length) {
254         std::vector<char> owned_data(buffer_->data(), buffer_->data() + length);
255         post_data_readers_.push_back(
256             new UploadOwnedBytesElementReader(&owned_data));
257       }
258       break;
259     }
260     case NSStreamEventNone:
261     case NSStreamEventOpenCompleted:
262     case NSStreamEventHasSpaceAvailable:
263       break;
264     default:
265       NOTREACHED() << "Unexpected stream event: " << event;
266       break;
267   }
270 #pragma mark URLRequest::Delegate methods
272 void HttpProtocolHandlerCore::OnReceivedRedirect(
273     URLRequest* request,
274     const RedirectInfo& redirect_info,
275     bool* /* defer_redirect */) {
276   DCHECK(thread_checker_.CalledOnValidThread());
278   // Cancel the request and notify UIWebView.
279   // If we did nothing, the network stack would follow the redirect
280   // automatically, however we do not want this because we need the UIWebView to
281   // be notified. The UIWebView will then issue a new request following the
282   // redirect.
283   DCHECK(request_);
284   GURL new_url = redirect_info.new_url;
286   if (!new_url.is_valid()) {
287     StopRequestWithError(NSURLErrorBadURL, ERR_INVALID_URL);
288     return;
289   }
291   DCHECK(new_url.is_valid());
292   NSURL* new_nsurl = NSURLWithGURL(new_url);
293   // Stash the original URL in case we need to report it in an error.
294   [request_ setURL:new_nsurl];
296   if (stream_delegate_.get())
297     StopListeningStream([request_ HTTPBodyStream]);
299   // TODO(droger): See if we can share some code with URLRequest::Redirect() in
300   // net/net_url_request/url_request.cc.
302   // For 303 redirects, all request methods except HEAD are converted to GET,
303   // as per the latest httpbis draft.  The draft also allows POST requests to
304   // be converted to GETs when following 301/302 redirects, for historical
305   // reasons. Most major browsers do this and so shall we.
306   // See:
307   // https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-17#section-7.3
308   const int http_status_code = request->GetResponseCode();
309   NSString* method = [request_ HTTPMethod];
310   const bool was_post = [method isEqualToString:@"POST"];
311   if ((http_status_code == 303 && ![method isEqualToString:@"HEAD"]) ||
312       ((http_status_code == 301 || http_status_code == 302) && was_post)) {
313     [request_ setHTTPMethod:@"GET"];
314     [request_ setHTTPBody:nil];
315     [request_ setHTTPBodyStream:nil];
316     if (was_post) {
317       // If being switched from POST to GET, must remove headers that were
318       // specific to the POST and don't have meaning in GET. For example
319       // the inclusion of a multipart Content-Type header in GET can cause
320       // problems with some servers:
321       // http://code.google.com/p/chromium/issues/detail?id=843
322       StripPostSpecificHeaders(request_.get());
323     }
324   }
326   NSURLResponse* response = GetNSURLResponseForRequest(request);
327 #if !defined(NDEBUG)
328   DVLOG(2) << "Redirect, to client:";
329   LogNSURLResponse(response);
330   DVLOG(2) << "Redirect, to client:";
331   LogNSURLRequest(request_);
332 #endif  // !defined(NDEBUG)
333   if (tracker_) {
334     tracker_->StopRedirectedRequest(request);
335     // Add clients from tracker that depend on redirect data.
336     PushClients(tracker_->ClientsHandlingRedirect(*request, new_url, response));
337   }
339   [top_level_client_ wasRedirectedToRequest:request_
340                               nativeRequest:request
341                            redirectResponse:response];
342   // Don't use |request_| or |response| anymore, as the client may be using them
343   // on another thread and they are not re-entrant. As |request_| is mutable, it
344   // is also important that it is not modified.
345   request_.reset(nil);
346   request->Cancel();
347   DCHECK_EQ(net_request_, request);
348   StopNetRequest();
351 void HttpProtocolHandlerCore::OnAuthRequired(URLRequest* request,
352                                              AuthChallengeInfo* auth_info) {
353   DCHECK(thread_checker_.CalledOnValidThread());
354   // A request with no tab ID should not hit HTTP authentication.
355   if (tracker_) {
356     // UIWebView does not handle authentication, so there is no point in calling
357     // the protocol method didReceiveAuthenticationChallenge.
358     // Instead, clients may handle proxy auth or display a UI to handle the
359     // challenge.
360     // Pass a weak reference, to avoid retain cycles.
361     network_client::AuthCallback callback =
362         base::Bind(&HttpProtocolHandlerCore::CompleteAuthentication,
363                    base::Unretained(this));
364     [top_level_client_ didRecieveAuthChallenge:auth_info
365                                  nativeRequest:*request
366                                       callback:callback];
367   } else if (net_request_ != nullptr) {
368     net_request_->CancelAuth();
369   }
372 void HttpProtocolHandlerCore::OnCertificateRequested(
373     URLRequest* request,
374     SSLCertRequestInfo* cert_request_info) {
375   DCHECK(thread_checker_.CalledOnValidThread());
377   // TODO(ios): The network stack does not support SSL client authentication
378   // on iOS yet. The request has to be canceled for now.
379   request->Cancel();
380   StopRequestWithError(NSURLErrorClientCertificateRequired,
381                        ERR_SSL_PROTOCOL_ERROR);
384 void HttpProtocolHandlerCore::ContinueAfterSSLError(void) {
385   DCHECK(thread_checker_.CalledOnValidThread());
386   if (net_request_ != nullptr) {
387     // Continue the request and load the data.
388     net_request_->ContinueDespiteLastError();
389   }
392 void HttpProtocolHandlerCore::CancelAfterSSLError(void) {
393   DCHECK(thread_checker_.CalledOnValidThread());
394   if (net_request_ != nullptr) {
395     // Cancel the request.
396     net_request_->Cancel();
398     // The request is signalled simply cancelled to the consumer, the
399     // presentation of the SSL error will be done via the tracker.
400     StopRequestWithError(NSURLErrorCancelled, ERR_BLOCKED_BY_CLIENT);
401   }
404 void HttpProtocolHandlerCore::SSLErrorCallback(bool carryOn) {
405   DCHECK(thread_checker_.CalledOnValidThread());
406   if (carryOn)
407     ContinueAfterSSLError();
408   else
409     CancelAfterSSLError();
412 void HttpProtocolHandlerCore::HostStateCallback(bool carryOn) {
413   DCHECK(thread_checker_.CalledOnValidThread());
414   if (carryOn)
415     StartReading();
416   else
417     CancelAfterSSLError();
420 void HttpProtocolHandlerCore::OnSSLCertificateError(URLRequest* request,
421                                                     const SSLInfo& ssl_info,
422                                                     bool fatal) {
423   DCHECK(thread_checker_.CalledOnValidThread());
425   if (fatal) {
426     if (tracker_) {
427       tracker_->OnSSLCertificateError(request, ssl_info, false,
428                                       base::Bind(&DoNothing));
429     }
430     CancelAfterSSLError();  // High security host do not tolerate any issue.
431   } else if (!tracker_) {
432     // No tracker, this is a request outside the context of a tab. There is no
433     // way to present anything to the user so only allow trivial errors.
434     // See ssl_cert_error_handler upstream.
435     if (IsCertStatusMinorError(ssl_info.cert_status))
436       ContinueAfterSSLError();
437     else
438       CancelAfterSSLError();
439   } else {
440     // The tracker will decide, eventually asking the user, and will invoke the
441     // callback.
442     RequestTracker::SSLCallback callback =
443         base::Bind(&HttpProtocolHandlerCore::SSLErrorCallback, this);
444     DCHECK(tracker_);
445     tracker_->OnSSLCertificateError(request, ssl_info, !fatal, callback);
446   }
449 void HttpProtocolHandlerCore::OnResponseStarted(URLRequest* request) {
450   DCHECK(thread_checker_.CalledOnValidThread());
452   if (net_request_ == nullptr)
453     return;
455   const URLRequestStatus& status = request->status();
456   if (!status.is_success()) {
457     int error = status.error();
458     StopRequestWithError(IOSErrorCode(error), error);
459     return;
460   }
462   if (tracker_ && IsCertStatusError(request->ssl_info().cert_status) &&
463       !request->context()->GetNetworkSessionParams()->
464           ignore_certificate_errors) {
465     // The certificate policy cache is captured here because SSL errors do not
466     // always trigger OnSSLCertificateError (this is the case when a page comes
467     // from the HTTP cache).
468     RequestTracker::SSLCallback callback =
469         base::Bind(&HttpProtocolHandlerCore::HostStateCallback, this);
470     tracker_->CaptureCertificatePolicyCache(request, callback);
471     return;
472   }
474   StartReading();
477 void HttpProtocolHandlerCore::StartReading() {
478   DCHECK(thread_checker_.CalledOnValidThread());
479   if (net_request_ == nullptr)
480     return;
482   NSURLResponse* response = GetNSURLResponseForRequest(net_request_);
483 #if !defined(NDEBUG)
484   DVLOG(2) << "To client:";
485   LogNSURLResponse(response);
486 #endif  // !defined(NDEBUG)
488   if (tracker_) {
489     tracker_->CaptureHeaders(net_request_);
490     long long expectedContentLength = [response expectedContentLength];
491     if (expectedContentLength > 0)
492       tracker_->CaptureExpectedLength(net_request_, expectedContentLength);
494     // Add clients from tracker.
495     PushClients(
496         tracker_->ClientsHandlingRequestAndResponse(*net_request_, response));
497   }
499   // Don't call any function on the response from now on, as the client may be
500   // using it and the object is not re-entrant.
501   [top_level_client_ didReceiveResponse:response];
503   int bytes_read = 0;
505   if (net_request_->Read(buffer_.get(), kIOBufferSize, &bytes_read)) {
506     OnReadCompleted(net_request_, bytes_read);
507   } else if (!net_request_->status().is_success()) {
508     int error = net_request_->status().error();
509     StopRequestWithError(IOSErrorCode(error), error);
510   }
513 void HttpProtocolHandlerCore::OnReadCompleted(URLRequest* request,
514                                               int bytes_read) {
515   DCHECK(thread_checker_.CalledOnValidThread());
517   if (net_request_ == nullptr)
518     return;
520   base::scoped_nsobject<NSMutableData> data([[NSMutableData alloc] init]);
522   // Read all we can from the socket and put it into data.
523   // TODO(droger): It may be possible to avoid some of the copies (using
524   // WrappedIOBuffer for example).
525   NSUInteger data_length;
526   bool loop = (bytes_read > 0);
527   bool io_pending = false;
528   uint64_t total_byte_read = loop ? bytes_read : 0;
529   while (loop) {
530     data_length = [data length];  // Assumes that getting the length is fast.
531     [data increaseLengthBy:bytes_read];
532     memcpy(reinterpret_cast<char*>([data mutableBytes]) + data_length,
533            buffer_->data(), bytes_read);
534     io_pending = !request->Read(buffer_.get(), kIOBufferSize, &bytes_read);
535     loop = !io_pending && (bytes_read > 0);
536     total_byte_read += bytes_read;
537   }
539   if (tracker_)
540     tracker_->CaptureReceivedBytes(request, total_byte_read);
542   // Notify the client.
543   const URLRequestStatus& status = request->status();
544   if (status.is_success()) {
545     if ([data length] > 0) {
546       // If the data is not encoded in UTF8, the NSString is nil.
547       DVLOG(3) << "To client:" << std::endl
548                << base::SysNSStringToUTF8([[[NSString alloc]
549                       initWithData:data
550                           encoding:NSUTF8StringEncoding] autorelease]);
551       [top_level_client_ didLoadData:data];
552     }
553     if (bytes_read == 0 && !io_pending) {
554       DCHECK_EQ(net_request_, request);
555       // There is nothing more to read.
556       StopNetRequest();
557       [top_level_client_ didFinishLoading];
558     }
559   } else {
560     // Request failed (not canceled).
561     int error = status.error();
562     StopRequestWithError(IOSErrorCode(error), error);
563   }
566 HttpProtocolHandlerCore::~HttpProtocolHandlerCore() {
567   DCHECK(thread_checker_.CalledOnValidThread());
568   [top_level_client_ cancelAuthRequest];
569   DCHECK(!net_request_);
570   DCHECK(!stream_delegate_);
573 // static
574 void HttpProtocolHandlerCore::Destruct(const HttpProtocolHandlerCore* x) {
575   scoped_refptr<base::SingleThreadTaskRunner> task_runner =
576       g_protocol_handler_delegate->GetDefaultURLRequestContext()
577           ->GetNetworkTaskRunner();
578   if (task_runner->BelongsToCurrentThread())
579     delete x;
580   else
581     task_runner->DeleteSoon(FROM_HERE, x);
584 void HttpProtocolHandlerCore::SetLoadFlags() {
585   DCHECK(thread_checker_.CalledOnValidThread());
586   int load_flags = LOAD_NORMAL;
588   if (![request_ HTTPShouldHandleCookies])
589     load_flags |= LOAD_DO_NOT_SEND_COOKIES | LOAD_DO_NOT_SAVE_COOKIES;
591   // Cache flags.
592   if (tracker_) {
593     RequestTracker::CacheMode cache_mode = tracker_->GetCacheMode();
594     switch (cache_mode) {
595       case RequestTracker::CACHE_RELOAD:
596         load_flags |= LOAD_VALIDATE_CACHE;
597         break;
598       case RequestTracker::CACHE_HISTORY:
599         load_flags |= LOAD_PREFERRING_CACHE;
600         break;
601       case RequestTracker::CACHE_BYPASS:
602         load_flags |= LOAD_DISABLE_CACHE | LOAD_BYPASS_CACHE;
603         break;
604       case RequestTracker::CACHE_ONLY:
605         load_flags |= LOAD_ONLY_FROM_CACHE;
606         break;
607       case RequestTracker::CACHE_NORMAL:
608         // Do nothing, normal load.
609         break;
610     }
611   } else {
612     switch ([request_ cachePolicy]) {
613       case NSURLRequestReloadIgnoringLocalAndRemoteCacheData:
614         load_flags |= LOAD_BYPASS_CACHE;
615       case NSURLRequestReloadIgnoringLocalCacheData:
616         load_flags |= LOAD_DISABLE_CACHE;
617         break;
618       case NSURLRequestReturnCacheDataElseLoad:
619         load_flags |= LOAD_PREFERRING_CACHE;
620         break;
621       case NSURLRequestReturnCacheDataDontLoad:
622         load_flags |= LOAD_ONLY_FROM_CACHE;
623         break;
624       case NSURLRequestReloadRevalidatingCacheData:
625         load_flags |= LOAD_VALIDATE_CACHE;
626         break;
627       case NSURLRequestUseProtocolCachePolicy:
628         // Do nothing, normal load.
629         break;
630     }
631   }
632   net_request_->SetLoadFlags(load_flags);
635 void HttpProtocolHandlerCore::Start(id<CRNNetworkClientProtocol> base_client) {
636   DCHECK(thread_checker_.CalledOnValidThread());
637   DCHECK(!top_level_client_);
638   DCHECK_EQ(0u, [clients_ count]);
639   DCHECK(base_client);
640   top_level_client_ = base_client;
641   [clients_ addObject:base_client];
642   GURL url = GURLWithNSURL([request_ URL]);
644   bool valid_tracker = RequestTracker::GetRequestTracker(request_, &tracker_);
645   if (!valid_tracker) {
646     // The request is associated with a tracker that does not exist, cancel it.
647     // NSURLErrorCancelled avoids triggering any error page.
648     [top_level_client_ didFailWithNSErrorCode:NSURLErrorCancelled
649                                  netErrorCode:ERR_ABORTED];
650     return;
651   }
653   if (tracker_) {
654     // Set up any clients that can operate regardless of the request
655     PushClients(tracker_->ClientsHandlingAnyRequest());
656   } else {
657     // There was no request_group_id, so the request was from something like a
658     // data: or file: URL.
659     // Attach any global clients to the request.
660     PushClients(RequestTracker::GlobalClientsHandlingAnyRequest());
661   }
663   // Now that all of the network clients are set up, if there was an error with
664   // the URL, it can be raised and all of the clients will have a chance to
665   // handle it.
666   if (!url.is_valid()) {
667     DLOG(ERROR) << "Trying to load an invalid URL: "
668                 << base::SysNSStringToUTF8([[request_ URL] absoluteString]);
669     [top_level_client_ didFailWithNSErrorCode:NSURLErrorBadURL
670                                  netErrorCode:ERR_INVALID_URL];
671     return;
672   }
674   const URLRequestContext* context =
675       tracker_ ? tracker_->GetRequestContext()
676                : g_protocol_handler_delegate->GetDefaultURLRequestContext()
677                      ->GetURLRequestContext();
678   DCHECK(context);
680   net_request_ =
681       context->CreateRequest(url, DEFAULT_PRIORITY, this).release();
682   net_request_->set_method(base::SysNSStringToUTF8([request_ HTTPMethod]));
684   net_request_->set_first_party_for_cookies(
685       GURLWithNSURL([request_ mainDocumentURL]));
687 #if !defined(NDEBUG)
688   DVLOG(2) << "From client:";
689   LogNSURLRequest(request_);
690 #endif  // !defined(NDEBUG)
692   CopyHttpHeaders(request_, net_request_);
694   // Add network clients.
695   if (tracker_)
696     PushClients(tracker_->ClientsHandlingRequest(*net_request_));
698   [top_level_client_ didCreateNativeRequest:net_request_];
699   SetLoadFlags();
701   if ([request_ HTTPBodyStream]) {
702     NSInputStream* input_stream = [request_ HTTPBodyStream];
703     stream_delegate_.reset(
704         [[CRWHTTPStreamDelegate alloc] initWithHttpProtocolHandlerCore:this]);
705     [input_stream setDelegate:stream_delegate_];
706     [input_stream scheduleInRunLoop:[NSRunLoop currentRunLoop]
707                             forMode:NSDefaultRunLoopMode];
708     [input_stream open];
709     // The request will be started when the stream is fully read.
710     return;
711   }
713   NSData* body = [request_ HTTPBody];
714   const NSUInteger body_length = [body length];
715   if (body_length > 0) {
716     const char* source_bytes = reinterpret_cast<const char*>([body bytes]);
717     std::vector<char> owned_data(source_bytes, source_bytes + body_length);
718     scoped_ptr<UploadElementReader> reader(
719         new UploadOwnedBytesElementReader(&owned_data));
720     net_request_->set_upload(
721         ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0));
722   }
724   net_request_->Start();
725   if (tracker_)
726     tracker_->StartRequest(net_request_);
729 void HttpProtocolHandlerCore::Cancel() {
730   DCHECK(thread_checker_.CalledOnValidThread());
731   if (net_request_ == nullptr)
732     return;
734   DVLOG(2) << "Client canceling request: " << net_request_->url().spec();
735   net_request_->Cancel();
736   StopNetRequest();
739 void HttpProtocolHandlerCore::StopNetRequest() {
740   DCHECK(thread_checker_.CalledOnValidThread());
741   if (tracker_)
742     tracker_->StopRequest(net_request_);
743   delete net_request_;
744   net_request_ = nullptr;
745   if (stream_delegate_.get())
746     StopListeningStream([request_ HTTPBodyStream]);
749 void HttpProtocolHandlerCore::StopListeningStream(NSStream* stream) {
750   DCHECK(thread_checker_.CalledOnValidThread());
751   DCHECK(stream);
752   DCHECK(stream_delegate_);
753   DCHECK([stream delegate] == stream_delegate_.get());
754   [stream setDelegate:nil];
755   [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
756                     forMode:NSDefaultRunLoopMode];
757   stream_delegate_.reset(nil);
758   // Close the stream if needed.
759   switch ([stream streamStatus]) {
760     case NSStreamStatusOpening:
761     case NSStreamStatusOpen:
762     case NSStreamStatusReading:
763     case NSStreamStatusWriting:
764     case NSStreamStatusAtEnd:
765       [stream close];
766       break;
767     case NSStreamStatusNotOpen:
768     case NSStreamStatusClosed:
769     case NSStreamStatusError:
770       break;
771     default:
772       NOTREACHED() << "Unexpected stream status: " << [stream streamStatus];
773       break;
774   }
777 NSInteger HttpProtocolHandlerCore::IOSErrorCode(int os_error) {
778   DCHECK(thread_checker_.CalledOnValidThread());
779   switch (os_error) {
780     case ERR_SSL_PROTOCOL_ERROR:
781       return NSURLErrorClientCertificateRequired;
782     case ERR_CONNECTION_RESET:
783     case ERR_NETWORK_CHANGED:
784       return NSURLErrorNetworkConnectionLost;
785     case ERR_UNEXPECTED:
786       return NSURLErrorUnknown;
787     default:
788       return NSURLErrorCannotConnectToHost;
789   }
792 void HttpProtocolHandlerCore::StopRequestWithError(NSInteger ns_error_code,
793                                                    int net_error_code) {
794   DCHECK(net_request_ != nullptr);
795   DCHECK(thread_checker_.CalledOnValidThread());
797   // Don't show an error message on ERR_ABORTED because this is error is often
798   // fired when switching profiles (see RequestTracker::CancelRequests()).
799   DLOG_IF(ERROR, net_error_code != ERR_ABORTED)
800       << "HttpProtocolHandlerCore - Network error: "
801       << ErrorToString(net_error_code) << " (" << net_error_code << ")";
803   [top_level_client_ didFailWithNSErrorCode:ns_error_code
804                                netErrorCode:net_error_code];
805   StopNetRequest();
808 void HttpProtocolHandlerCore::CompleteAuthentication(
809     bool auth_ok,
810     const base::string16& username,
811     const base::string16& password) {
812   DCHECK(thread_checker_.CalledOnValidThread());
813   if (net_request_ == nullptr)
814     return;
815   if (auth_ok) {
816     net_request_->SetAuth(AuthCredentials(username, password));
817   } else {
818     net_request_->CancelAuth();
819   }
822 void HttpProtocolHandlerCore::StripPostSpecificHeaders(
823     NSMutableURLRequest* request) {
824   DCHECK(thread_checker_.CalledOnValidThread());
825   DCHECK(request);
826   [request setValue:nil forHTTPHeaderField:base::SysUTF8ToNSString(
827       HttpRequestHeaders::kContentLength)];
828   [request setValue:nil forHTTPHeaderField:base::SysUTF8ToNSString(
829       HttpRequestHeaders::kContentType)];
830   [request setValue:nil forHTTPHeaderField:base::SysUTF8ToNSString(
831       HttpRequestHeaders::kOrigin)];
834 void HttpProtocolHandlerCore::PushClient(id<CRNNetworkClientProtocol> client) {
835   DCHECK(thread_checker_.CalledOnValidThread());
836   [client setUnderlyingClient:top_level_client_];
837   [clients_ addObject:client];
838   top_level_client_ = client;
841 void HttpProtocolHandlerCore::PushClients(NSArray* clients) {
842   DCHECK(thread_checker_.CalledOnValidThread());
843   for (id<CRNNetworkClientProtocol> client in clients)
844     PushClient(client);
847 }  // namespace net
849 #pragma mark -
850 #pragma mark CRWHTTPStreamDelegate
852 @implementation CRWHTTPStreamDelegate
853 - (instancetype)initWithHttpProtocolHandlerCore:
854     (net::HttpProtocolHandlerCore*)core {
855   DCHECK(core);
856   self = [super init];
857   if (self)
858     _core = core;
859   return self;
862 - (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent {
863   _core->HandleStreamEvent(theStream, streamEvent);
865 @end
867 #pragma mark -
868 #pragma mark DeferredCancellation
870 // An object of class |DeferredCancellation| represents a deferred cancellation
871 // of a request. In principle this is a block posted to a thread's runloop, but
872 // since there is no performBlock:onThread:, this class wraps the desired
873 // behavior in an object.
874 @interface DeferredCancellation : NSObject
876 - (instancetype)initWithCore:(scoped_refptr<net::HttpProtocolHandlerCore>)core;
877 - (void)cancel;
879 @end
881 @implementation DeferredCancellation {
882   scoped_refptr<net::HttpProtocolHandlerCore> _core;
885 - (instancetype)initWithCore:(scoped_refptr<net::HttpProtocolHandlerCore>)core {
886   if ((self = [super init])) {
887     _core = core;
888   }
889   return self;
892 - (void)cancel {
893   g_protocol_handler_delegate->GetDefaultURLRequestContext()
894       ->GetNetworkTaskRunner()
895       ->PostTask(FROM_HERE,
896                  base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
899 @end
901 #pragma mark -
902 #pragma mark HttpProtocolHandler
904 @interface CRNHTTPProtocolHandler (Private)
906 - (id<CRNHTTPProtocolHandlerProxy>)getProtocolHandlerProxy;
907 - (scoped_refptr<net::HttpProtocolHandlerCore>)getCore;
908 - (NSThread*)getClientThread;
909 - (void)cancelRequest;
911 @end
913 // The HttpProtocolHandler is called by the iOS system to handle the
914 // NSURLRequest.
915 @implementation CRNHTTPProtocolHandler {
916   scoped_refptr<net::HttpProtocolHandlerCore> _core;
917   base::scoped_nsprotocol<id<CRNHTTPProtocolHandlerProxy>> _protocolProxy;
918   NSThread* _clientThread;
919   NSString* _clientRunLoopMode;
920   BOOL _supportedURL;
923 #pragma mark NSURLProtocol methods
925 + (BOOL)canInitWithRequest:(NSURLRequest*)request {
926   DVLOG(5) << "canInitWithRequest " << net::FormatUrlRequestForLogging(request);
927   return g_protocol_handler_delegate->CanHandleRequest(request);
930 + (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request {
931   // TODO(droger): Is this used if we disable the cache of UIWebView? If it is,
932   // then we need a real implementation, even though Chrome network stack does
933   // not need it (GURLs are automatically canonized).
934   return request;
937 - (instancetype)initWithRequest:(NSURLRequest*)request
938                  cachedResponse:(NSCachedURLResponse*)cachedResponse
939                          client:(id<NSURLProtocolClient>)client {
940   DCHECK(!cachedResponse);
941   self = [super initWithRequest:request
942                  cachedResponse:cachedResponse
943                          client:client];
944   if (self) {
945     _supportedURL = g_protocol_handler_delegate->IsRequestSupported(request);
946     _core = new net::HttpProtocolHandlerCore(request);
947   }
948   return self;
951 #pragma mark NSURLProtocol overrides.
953 - (NSCachedURLResponse*)cachedResponse {
954   // We do not use the UIWebView cache.
955   // TODO(droger): Disable the UIWebView cache.
956   return nil;
959 - (void)startLoading {
960   // If the scheme is not valid, just return an error right away.
961   if (!_supportedURL) {
962     NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
964     // It is possible for URL to be nil, so check for that
965     // before creating the error object. See http://crbug/349051
966     NSURL* url = [[self request] URL];
967     if (url)
968       [dictionary setObject:url forKey:NSURLErrorKey];
970     NSError* error = [NSError errorWithDomain:NSURLErrorDomain
971                                          code:NSURLErrorUnsupportedURL
972                                      userInfo:dictionary];
973     [[self client] URLProtocol:self didFailWithError:error];
974     return;
975   }
977   _clientThread = [NSThread currentThread];
979   // The closure passed to PostTask must to retain the _protocolProxy
980   // scoped_nsobject. A call to getProtocolHandlerProxy before passing
981   // _protocolProxy ensure that _protocolProxy is instanciated before passing
982   // it.
983   [self getProtocolHandlerProxy];
984   DCHECK(_protocolProxy);
985   g_protocol_handler_delegate->GetDefaultURLRequestContext()
986       ->GetNetworkTaskRunner()
987       ->PostTask(FROM_HERE, base::Bind(&net::HttpProtocolHandlerCore::Start,
988                                        _core, _protocolProxy));
991 - (id<CRNHTTPProtocolHandlerProxy>)getProtocolHandlerProxy {
992   DCHECK_EQ([NSThread currentThread], _clientThread);
993   if (!_protocolProxy.get()) {
994     _protocolProxy.reset([[CRNHTTPProtocolHandlerProxyWithClientThread alloc]
995         initWithProtocol:self
996             clientThread:_clientThread
997              runLoopMode:[[NSRunLoop currentRunLoop] currentMode]]);
998   }
999   return _protocolProxy.get();
1002 - (scoped_refptr<net::HttpProtocolHandlerCore>)getCore {
1003   return _core;
1006 - (NSThread*)getClientThread {
1007   return _clientThread;
1010 - (void)cancelRequest {
1011   g_protocol_handler_delegate->GetDefaultURLRequestContext()
1012       ->GetNetworkTaskRunner()
1013       ->PostTask(FROM_HERE,
1014                  base::Bind(&net::HttpProtocolHandlerCore::Cancel, _core));
1015   [_protocolProxy invalidate];
1018 - (void)stopLoading {
1019   [self cancelRequest];
1020   _protocolProxy.reset();
1023 @end
1025 #pragma mark -
1026 #pragma mark PauseableHttpProtocolHandler
1028 // The HttpProtocolHandler is called by the iOS system to handle the
1029 // NSURLRequest. This HttpProtocolHandler conforms to the observed semantics of
1030 // NSURLProtocol when used with NSURLSession on iOS 8 - i.e., |-startLoading|
1031 // means "start or resume request" and |-stopLoading| means "pause request".
1032 // Since there is no way to actually pause a request in the network stack, this
1033 // is implemented using a subclass of CRNHTTPProtocolHandlerProxy that knows how
1034 // to defer callbacks.
1036 // Note that this class conforms to somewhat complex threading rules:
1037 // 1) |initWithRequest:cachedResponse:client:| and |dealloc| can be called on
1038 //    any thread.
1039 // 2) |startLoading| and |stopLoading| are always called on the client thread.
1040 // 3) |stopLoading| is called before |dealloc| is called.
1042 // The main wrinkle is that |dealloc|, which may be called on any thread, needs
1043 // to clean up a running network request. To do this, |dealloc| needs to run
1044 // |cancelRequest|, which needs to be run on the client thread. Since it is
1045 // guaranteed that |startLoading| is called before |dealloc| is called, the
1046 // |startLoading| method stores a pointer to the client thread, then |dealloc|
1047 // asks that client thread to perform the |cancelRequest| selector via
1048 // |scheduleCancelRequest|.
1050 // Some of the above logic is implemented in the parent class
1051 // (CRNHTTPProtocolHandler) because it is convenient.
1052 @implementation CRNPauseableHTTPProtocolHandler {
1053   BOOL _started;
1054   dispatch_queue_t _queue;
1057 #pragma mark NSURLProtocol methods
1059 - (void)dealloc {
1060   [self scheduleCancelRequest];
1061   [super dealloc];
1064 #pragma mark NSURLProtocol overrides.
1066 - (void)startLoading {
1067   if (_started) {
1068     [[self getProtocolHandlerProxy] resume];
1069     return;
1070   }
1072   _started = YES;
1073   [super startLoading];
1076 - (void)stopLoading {
1077   [[self getProtocolHandlerProxy] pause];
1080 // This method has unusual concurrency properties. It can be called on any
1081 // thread, but it must be called from |-dealloc|, which guarantees that no other
1082 // method of this object is running concurrently (since |-dealloc| is only
1083 // called when the last reference to the object drops).
1085 // This method takes a reference to _core to ensure that _core lives long enough
1086 // to have the request cleanly cancelled.
1087 - (void)scheduleCancelRequest {
1088   DeferredCancellation* cancellation =
1089       [[DeferredCancellation alloc] initWithCore:[self getCore]];
1090   NSArray* modes = @[ [[NSRunLoop currentRunLoop] currentMode] ];
1091   [cancellation performSelector:@selector(cancel)
1092                        onThread:[self getClientThread]
1093                      withObject:nil
1094                   waitUntilDone:NO
1095                           modes:modes];
1098 @end