| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/services/network/url_loader_impl.h" | 5 #include "mojo/services/network/url_loader_impl.h" |
| 6 | 6 |
| 7 #include "mojo/common/common_type_converters.h" |
| 7 #include "mojo/services/network/network_context.h" | 8 #include "mojo/services/network/network_context.h" |
| 8 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 9 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| 10 #include "net/http/http_response_headers.h" | 11 #include "net/http/http_response_headers.h" |
| 11 | 12 |
| 12 namespace mojo { | 13 namespace mojo { |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 const uint32_t kMaxReadSize = 64 * 1024; | 16 const uint32_t kMaxReadSize = 64 * 1024; |
| 16 | 17 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 : net::WrappedIOBuffer(pending_write->buffer()), | 87 : net::WrappedIOBuffer(pending_write->buffer()), |
| 87 pending_write_(pending_write) { | 88 pending_write_(pending_write) { |
| 88 } | 89 } |
| 89 private: | 90 private: |
| 90 virtual ~DependentIOBuffer() {} | 91 virtual ~DependentIOBuffer() {} |
| 91 scoped_refptr<PendingWriteToDataPipe> pending_write_; | 92 scoped_refptr<PendingWriteToDataPipe> pending_write_; |
| 92 }; | 93 }; |
| 93 | 94 |
| 94 URLLoaderImpl::URLLoaderImpl(NetworkContext* context) | 95 URLLoaderImpl::URLLoaderImpl(NetworkContext* context) |
| 95 : context_(context), | 96 : context_(context), |
| 97 auto_follow_redirects_(true), |
| 96 weak_ptr_factory_(this) { | 98 weak_ptr_factory_(this) { |
| 97 } | 99 } |
| 98 | 100 |
| 99 URLLoaderImpl::~URLLoaderImpl() { | 101 URLLoaderImpl::~URLLoaderImpl() { |
| 100 } | 102 } |
| 101 | 103 |
| 102 void URLLoaderImpl::OnConnectionError() { | 104 void URLLoaderImpl::OnConnectionError() { |
| 103 delete this; | 105 delete this; |
| 104 } | 106 } |
| 105 | 107 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 119 } | 121 } |
| 120 | 122 |
| 121 response_body_stream_ = response_body_stream.Pass(); | 123 response_body_stream_ = response_body_stream.Pass(); |
| 122 | 124 |
| 123 GURL url(request->url); | 125 GURL url(request->url); |
| 124 url_request_.reset( | 126 url_request_.reset( |
| 125 new net::URLRequest(url, | 127 new net::URLRequest(url, |
| 126 net::DEFAULT_PRIORITY, | 128 net::DEFAULT_PRIORITY, |
| 127 this, | 129 this, |
| 128 context_->url_request_context())); | 130 context_->url_request_context())); |
| 131 url_request_->set_method(request->method); |
| 132 if (request->headers) { |
| 133 net::HttpRequestHeaders headers; |
| 134 for (size_t i = 0; i < request->headers.size(); ++i) |
| 135 headers.AddHeaderFromString(request->headers[i].To<base::StringPiece>()); |
| 136 url_request_->SetExtraRequestHeaders(headers); |
| 137 } |
| 129 if (request->bypass_cache) | 138 if (request->bypass_cache) |
| 130 url_request_->SetLoadFlags(net::LOAD_BYPASS_CACHE); | 139 url_request_->SetLoadFlags(net::LOAD_BYPASS_CACHE); |
| 140 // TODO(darin): Handle request body. |
| 141 |
| 142 auto_follow_redirects_ = request->auto_follow_redirects; |
| 143 |
| 131 url_request_->Start(); | 144 url_request_->Start(); |
| 132 } | 145 } |
| 133 | 146 |
| 134 void URLLoaderImpl::FollowRedirect() { | 147 void URLLoaderImpl::FollowRedirect() { |
| 135 NOTIMPLEMENTED(); | 148 if (auto_follow_redirects_) { |
| 149 DLOG(ERROR) << "Spurious call to FollowRedirect"; |
| 150 } else { |
| 151 if (url_request_) |
| 152 url_request_->FollowDeferredRedirect(); |
| 153 } |
| 136 } | 154 } |
| 137 | 155 |
| 138 void URLLoaderImpl::OnReceivedRedirect(net::URLRequest* url_request, | 156 void URLLoaderImpl::OnReceivedRedirect(net::URLRequest* url_request, |
| 139 const GURL& new_url, | 157 const GURL& new_url, |
| 140 bool* defer_redirect) { | 158 bool* defer_redirect) { |
| 141 DCHECK(url_request == url_request_.get()); | 159 DCHECK(url_request == url_request_.get()); |
| 142 DCHECK(url_request->status().is_success()); | 160 DCHECK(url_request->status().is_success()); |
| 143 | 161 |
| 144 URLResponsePtr response = MakeURLResponse(url_request); | 162 URLResponsePtr response = MakeURLResponse(url_request); |
| 145 std::string redirect_method = | 163 std::string redirect_method = |
| 146 net::URLRequest::ComputeMethodForRedirect(url_request->method(), | 164 net::URLRequest::ComputeMethodForRedirect(url_request->method(), |
| 147 response->status_code); | 165 response->status_code); |
| 148 client()->OnReceivedRedirect( | 166 client()->OnReceivedRedirect( |
| 149 response.Pass(), new_url.spec(), redirect_method); | 167 response.Pass(), new_url.spec(), redirect_method); |
| 150 | 168 |
| 151 *defer_redirect = false; | 169 *defer_redirect = !auto_follow_redirects_; |
| 152 } | 170 } |
| 153 | 171 |
| 154 void URLLoaderImpl::OnResponseStarted(net::URLRequest* url_request) { | 172 void URLLoaderImpl::OnResponseStarted(net::URLRequest* url_request) { |
| 155 DCHECK(url_request == url_request_.get()); | 173 DCHECK(url_request == url_request_.get()); |
| 156 | 174 |
| 157 if (!url_request->status().is_success()) { | 175 if (!url_request->status().is_success()) { |
| 158 SendError(url_request->status().error()); | 176 SendError(url_request->status().error()); |
| 159 return; | 177 return; |
| 160 } | 178 } |
| 161 | 179 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 if (completed_synchronously) { | 244 if (completed_synchronously) { |
| 227 base::MessageLoop::current()->PostTask( | 245 base::MessageLoop::current()->PostTask( |
| 228 FROM_HERE, | 246 FROM_HERE, |
| 229 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); | 247 base::Bind(&URLLoaderImpl::ReadMore, weak_ptr_factory_.GetWeakPtr())); |
| 230 } else { | 248 } else { |
| 231 ReadMore(); | 249 ReadMore(); |
| 232 } | 250 } |
| 233 } | 251 } |
| 234 | 252 |
| 235 } // namespace mojo | 253 } // namespace mojo |
| OLD | NEW |