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 "url_request_peer.h" | 5 #include "url_request_adapter.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "components/cronet/android/url_request_context_peer.h" | 8 #include "components/cronet/android/url_request_context_adapter.h" |
9 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" | 9 #include "components/cronet/android/wrapped_channel_upload_element_reader.h" |
10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
11 #include "net/base/upload_bytes_element_reader.h" | 11 #include "net/base/upload_bytes_element_reader.h" |
12 #include "net/http/http_status_code.h" | 12 #include "net/http/http_status_code.h" |
13 | 13 |
14 namespace cronet { | 14 namespace cronet { |
15 | 15 |
16 static const size_t kBufferSizeIncrement = 8192; | 16 static const size_t kBufferSizeIncrement = 8192; |
17 | 17 |
18 URLRequestPeer::URLRequestPeer(URLRequestContextPeer* context, | 18 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, |
19 URLRequestPeerDelegate* delegate, | 19 URLRequestAdapterDelegate* delegate, |
20 GURL url, | 20 GURL url, |
21 net::RequestPriority priority) | 21 net::RequestPriority priority) |
22 : method_("GET"), | 22 : method_("GET"), |
23 url_request_(NULL), | 23 url_request_(NULL), |
24 read_buffer_(new net::GrowableIOBuffer()), | 24 read_buffer_(new net::GrowableIOBuffer()), |
25 bytes_read_(0), | 25 bytes_read_(0), |
26 total_bytes_read_(0), | 26 total_bytes_read_(0), |
27 error_code_(0), | 27 error_code_(0), |
28 http_status_code_(0), | 28 http_status_code_(0), |
29 canceled_(false), | 29 canceled_(false), |
30 expected_size_(0) { | 30 expected_size_(0) { |
31 context_ = context; | 31 context_ = context; |
32 delegate_ = delegate; | 32 delegate_ = delegate; |
33 url_ = url; | 33 url_ = url; |
34 priority_ = priority; | 34 priority_ = priority; |
35 } | 35 } |
36 | 36 |
37 URLRequestPeer::~URLRequestPeer() { CHECK(url_request_ == NULL); } | 37 URLRequestAdapter::~URLRequestAdapter() { |
| 38 CHECK(url_request_ == NULL); |
| 39 } |
38 | 40 |
39 void URLRequestPeer::SetMethod(const std::string& method) { method_ = method; } | 41 void URLRequestAdapter::SetMethod(const std::string& method) { |
| 42 method_ = method; |
| 43 } |
40 | 44 |
41 void URLRequestPeer::AddHeader(const std::string& name, | 45 void URLRequestAdapter::AddHeader(const std::string& name, |
42 const std::string& value) { | 46 const std::string& value) { |
43 headers_.SetHeader(name, value); | 47 headers_.SetHeader(name, value); |
44 } | 48 } |
45 | 49 |
46 void URLRequestPeer::SetUploadContent(const char* bytes, int bytes_len) { | 50 void URLRequestAdapter::SetUploadContent(const char* bytes, int bytes_len) { |
47 std::vector<char> data(bytes, bytes + bytes_len); | 51 std::vector<char> data(bytes, bytes + bytes_len); |
48 scoped_ptr<net::UploadElementReader> reader( | 52 scoped_ptr<net::UploadElementReader> reader( |
49 new net::UploadOwnedBytesElementReader(&data)); | 53 new net::UploadOwnedBytesElementReader(&data)); |
50 upload_data_stream_.reset(net::UploadDataStream::CreateWithReader( | 54 upload_data_stream_.reset( |
51 reader.Pass(), 0)); | 55 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
52 } | 56 } |
53 | 57 |
54 void URLRequestPeer::SetUploadChannel(JNIEnv* env, int64 content_length) { | 58 void URLRequestAdapter::SetUploadChannel(JNIEnv* env, int64 content_length) { |
55 scoped_ptr<net::UploadElementReader> reader( | 59 scoped_ptr<net::UploadElementReader> reader( |
56 new WrappedChannelElementReader(delegate_, content_length)); | 60 new WrappedChannelElementReader(delegate_, content_length)); |
57 upload_data_stream_.reset(net::UploadDataStream::CreateWithReader( | 61 upload_data_stream_.reset( |
58 reader.Pass(), 0)); | 62 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
59 } | 63 } |
60 | 64 |
61 std::string URLRequestPeer::GetHeader(const std::string &name) const { | 65 std::string URLRequestAdapter::GetHeader(const std::string& name) const { |
62 std::string value; | 66 std::string value; |
63 if (url_request_ != NULL) { | 67 if (url_request_ != NULL) { |
64 url_request_->GetResponseHeaderByName(name, &value); | 68 url_request_->GetResponseHeaderByName(name, &value); |
65 } | 69 } |
66 return value; | 70 return value; |
67 } | 71 } |
68 | 72 |
69 net::HttpResponseHeaders* URLRequestPeer::GetResponseHeaders() const { | 73 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { |
70 if (url_request_ == NULL) { | 74 if (url_request_ == NULL) { |
71 return NULL; | 75 return NULL; |
72 } | 76 } |
73 return url_request_->response_headers(); | 77 return url_request_->response_headers(); |
74 } | 78 } |
75 | 79 |
76 void URLRequestPeer::Start() { | 80 void URLRequestAdapter::Start() { |
77 context_->GetNetworkTaskRunner()->PostTask( | 81 context_->GetNetworkTaskRunner()->PostTask( |
78 FROM_HERE, | 82 FROM_HERE, |
79 base::Bind(&URLRequestPeer::OnInitiateConnection, | 83 base::Bind(&URLRequestAdapter::OnInitiateConnection, |
80 base::Unretained(this))); | 84 base::Unretained(this))); |
81 } | 85 } |
82 | 86 |
83 void URLRequestPeer::OnInitiateConnection() { | 87 void URLRequestAdapter::OnInitiateConnection() { |
84 if (canceled_) { | 88 if (canceled_) { |
85 return; | 89 return; |
86 } | 90 } |
87 | 91 |
88 VLOG(1) << "Starting chromium request: " | 92 VLOG(1) << "Starting chromium request: " |
89 << url_.possibly_invalid_spec().c_str() | 93 << url_.possibly_invalid_spec().c_str() |
90 << " priority: " << RequestPriorityToString(priority_); | 94 << " priority: " << RequestPriorityToString(priority_); |
91 url_request_ = new net::URLRequest( | 95 url_request_ = new net::URLRequest( |
92 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); | 96 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); |
93 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 97 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
94 net::LOAD_DO_NOT_SAVE_COOKIES | | 98 net::LOAD_DO_NOT_SAVE_COOKIES | |
95 net::LOAD_DO_NOT_SEND_COOKIES); | 99 net::LOAD_DO_NOT_SEND_COOKIES); |
96 url_request_->set_method(method_); | 100 url_request_->set_method(method_); |
97 url_request_->SetExtraRequestHeaders(headers_); | 101 url_request_->SetExtraRequestHeaders(headers_); |
98 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { | 102 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { |
99 std::string user_agent; | 103 std::string user_agent; |
100 user_agent = context_->GetUserAgent(url_); | 104 user_agent = context_->GetUserAgent(url_); |
101 url_request_->SetExtraRequestHeaderByName( | 105 url_request_->SetExtraRequestHeaderByName( |
102 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); | 106 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); |
103 } | 107 } |
104 | 108 |
105 if (upload_data_stream_) | 109 if (upload_data_stream_) |
106 url_request_->set_upload(upload_data_stream_.Pass()); | 110 url_request_->set_upload(upload_data_stream_.Pass()); |
107 | 111 |
108 url_request_->SetPriority(priority_); | 112 url_request_->SetPriority(priority_); |
109 | 113 |
110 url_request_->Start(); | 114 url_request_->Start(); |
111 } | 115 } |
112 | 116 |
113 void URLRequestPeer::Cancel() { | 117 void URLRequestAdapter::Cancel() { |
114 if (canceled_) { | 118 if (canceled_) { |
115 return; | 119 return; |
116 } | 120 } |
117 | 121 |
118 canceled_ = true; | 122 canceled_ = true; |
119 | 123 |
120 context_->GetNetworkTaskRunner()->PostTask( | 124 context_->GetNetworkTaskRunner()->PostTask( |
121 FROM_HERE, | 125 FROM_HERE, |
122 base::Bind(&URLRequestPeer::OnCancelRequest, base::Unretained(this))); | 126 base::Bind(&URLRequestAdapter::OnCancelRequest, base::Unretained(this))); |
123 } | 127 } |
124 | 128 |
125 void URLRequestPeer::OnCancelRequest() { | 129 void URLRequestAdapter::OnCancelRequest() { |
126 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); | 130 VLOG(1) << "Canceling chromium request: " << url_.possibly_invalid_spec(); |
127 | 131 |
128 if (url_request_ != NULL) { | 132 if (url_request_ != NULL) { |
129 url_request_->Cancel(); | 133 url_request_->Cancel(); |
130 } | 134 } |
131 | 135 |
132 OnRequestCanceled(); | 136 OnRequestCanceled(); |
133 } | 137 } |
134 | 138 |
135 void URLRequestPeer::Destroy() { | 139 void URLRequestAdapter::Destroy() { |
136 context_->GetNetworkTaskRunner()->PostTask( | 140 context_->GetNetworkTaskRunner()->PostTask( |
137 FROM_HERE, base::Bind(&URLRequestPeer::OnDestroyRequest, this)); | 141 FROM_HERE, base::Bind(&URLRequestAdapter::OnDestroyRequest, this)); |
138 } | 142 } |
139 | 143 |
140 // static | 144 // static |
141 void URLRequestPeer::OnDestroyRequest(URLRequestPeer* self) { | 145 void URLRequestAdapter::OnDestroyRequest(URLRequestAdapter* self) { |
142 VLOG(1) << "Destroying chromium request: " | 146 VLOG(1) << "Destroying chromium request: " |
143 << self->url_.possibly_invalid_spec(); | 147 << self->url_.possibly_invalid_spec(); |
144 delete self; | 148 delete self; |
145 } | 149 } |
146 | 150 |
147 void URLRequestPeer::OnResponseStarted(net::URLRequest* request) { | 151 void URLRequestAdapter::OnResponseStarted(net::URLRequest* request) { |
148 if (request->status().status() != net::URLRequestStatus::SUCCESS) { | 152 if (request->status().status() != net::URLRequestStatus::SUCCESS) { |
149 OnRequestFailed(); | 153 OnRequestFailed(); |
150 return; | 154 return; |
151 } | 155 } |
152 | 156 |
153 http_status_code_ = request->GetResponseCode(); | 157 http_status_code_ = request->GetResponseCode(); |
154 VLOG(1) << "Response started with status: " << http_status_code_; | 158 VLOG(1) << "Response started with status: " << http_status_code_; |
155 | 159 |
156 request->GetResponseHeaderByName("Content-Type", &content_type_); | 160 request->GetResponseHeaderByName("Content-Type", &content_type_); |
157 expected_size_ = request->GetExpectedContentSize(); | 161 expected_size_ = request->GetExpectedContentSize(); |
158 delegate_->OnResponseStarted(this); | 162 delegate_->OnResponseStarted(this); |
159 | 163 |
160 Read(); | 164 Read(); |
161 } | 165 } |
162 | 166 |
163 // Reads all available data or starts an asynchronous read. | 167 // Reads all available data or starts an asynchronous read. |
164 void URLRequestPeer::Read() { | 168 void URLRequestAdapter::Read() { |
165 while (true) { | 169 while (true) { |
166 if (read_buffer_->RemainingCapacity() == 0) { | 170 if (read_buffer_->RemainingCapacity() == 0) { |
167 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; | 171 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; |
168 read_buffer_->SetCapacity(new_capacity); | 172 read_buffer_->SetCapacity(new_capacity); |
169 } | 173 } |
170 | 174 |
171 int bytes_read; | 175 int bytes_read; |
172 if (url_request_->Read( | 176 if (url_request_->Read( |
173 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { | 177 read_buffer_, read_buffer_->RemainingCapacity(), &bytes_read)) { |
174 if (bytes_read == 0) { | 178 if (bytes_read == 0) { |
(...skipping 14 matching lines...) Expand all Loading... |
189 } | 193 } |
190 VLOG(1) << "Started async read"; | 194 VLOG(1) << "Started async read"; |
191 break; | 195 break; |
192 } else { | 196 } else { |
193 OnRequestFailed(); | 197 OnRequestFailed(); |
194 break; | 198 break; |
195 } | 199 } |
196 } | 200 } |
197 } | 201 } |
198 | 202 |
199 void URLRequestPeer::OnReadCompleted(net::URLRequest* request, int bytes_read) { | 203 void URLRequestAdapter::OnReadCompleted(net::URLRequest* request, |
| 204 int bytes_read) { |
200 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; | 205 VLOG(1) << "Asynchronously read: " << bytes_read << " bytes"; |
201 if (bytes_read < 0) { | 206 if (bytes_read < 0) { |
202 OnRequestFailed(); | 207 OnRequestFailed(); |
203 return; | 208 return; |
204 } else if (bytes_read == 0) { | 209 } else if (bytes_read == 0) { |
205 OnRequestSucceeded(); | 210 OnRequestSucceeded(); |
206 return; | 211 return; |
207 } | 212 } |
208 | 213 |
209 OnBytesRead(bytes_read); | 214 OnBytesRead(bytes_read); |
210 Read(); | 215 Read(); |
211 } | 216 } |
212 | 217 |
213 void URLRequestPeer::OnBytesRead(int bytes_read) { | 218 void URLRequestAdapter::OnBytesRead(int bytes_read) { |
214 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); | 219 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); |
215 bytes_read_ += bytes_read; | 220 bytes_read_ += bytes_read; |
216 total_bytes_read_ += bytes_read; | 221 total_bytes_read_ += bytes_read; |
217 } | 222 } |
218 | 223 |
219 void URLRequestPeer::OnRequestSucceeded() { | 224 void URLRequestAdapter::OnRequestSucceeded() { |
220 if (canceled_) { | 225 if (canceled_) { |
221 return; | 226 return; |
222 } | 227 } |
223 | 228 |
224 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ | 229 VLOG(1) << "Request completed with HTTP status: " << http_status_code_ |
225 << ". Total bytes read: " << total_bytes_read_; | 230 << ". Total bytes read: " << total_bytes_read_; |
226 | 231 |
227 OnRequestCompleted(); | 232 OnRequestCompleted(); |
228 } | 233 } |
229 | 234 |
230 void URLRequestPeer::OnRequestFailed() { | 235 void URLRequestAdapter::OnRequestFailed() { |
231 if (canceled_) { | 236 if (canceled_) { |
232 return; | 237 return; |
233 } | 238 } |
234 | 239 |
235 error_code_ = url_request_->status().error(); | 240 error_code_ = url_request_->status().error(); |
236 VLOG(1) << "Request failed with status: " << url_request_->status().status() | 241 VLOG(1) << "Request failed with status: " << url_request_->status().status() |
237 << " and error: " << net::ErrorToString(error_code_); | 242 << " and error: " << net::ErrorToString(error_code_); |
238 OnRequestCompleted(); | 243 OnRequestCompleted(); |
239 } | 244 } |
240 | 245 |
241 void URLRequestPeer::OnRequestCanceled() { OnRequestCompleted(); } | 246 void URLRequestAdapter::OnRequestCanceled() { |
| 247 OnRequestCompleted(); |
| 248 } |
242 | 249 |
243 void URLRequestPeer::OnRequestCompleted() { | 250 void URLRequestAdapter::OnRequestCompleted() { |
244 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); | 251 VLOG(1) << "Completed: " << url_.possibly_invalid_spec(); |
245 if (url_request_ != NULL) { | 252 if (url_request_ != NULL) { |
246 delete url_request_; | 253 delete url_request_; |
247 url_request_ = NULL; | 254 url_request_ = NULL; |
248 } | 255 } |
249 | 256 |
250 delegate_->OnBytesRead(this); | 257 delegate_->OnBytesRead(this); |
251 delegate_->OnRequestFinished(this); | 258 delegate_->OnRequestFinished(this); |
252 } | 259 } |
253 | 260 |
254 unsigned char* URLRequestPeer::Data() const { | 261 unsigned char* URLRequestAdapter::Data() const { |
255 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 262 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); |
256 } | 263 } |
257 | 264 |
258 } // namespace cronet | 265 } // namespace cronet |
OLD | NEW |