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 #include "url_request_peer.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "net/base/load_flags.h" |
| 9 #include "net/cronet/android/android_log.h" |
| 10 #include "net/http/http_status_code.h" |
| 11 |
| 12 #define LOG_TAG "ChromiumNetwork" |
| 13 |
| 14 static const size_t kBufferSizeIncrement = 8192; |
| 15 |
| 16 // Fragment automatically inserted in the User-Agent header to indicate |
| 17 // that the request is coming from this network stack. |
| 18 static const char kUserAgentFragment[] = "; ChromiumJNI/"; |
| 19 |
| 20 URLRequestPeer::URLRequestPeer(URLRequestContextPeer *context, |
| 21 URLRequestPeerDelegate *delegate, GURL url, |
| 22 net::RequestPriority priority) |
| 23 : method_("GET"), |
| 24 url_request_(NULL), |
| 25 read_buffer_(new net::GrowableIOBuffer()), |
| 26 bytes_read_(0), |
| 27 total_bytes_read_(0), |
| 28 error_code_(0), |
| 29 http_status_code_(0), |
| 30 canceled_(false), |
| 31 expected_size_(0) { |
| 32 context_ = context; |
| 33 delegate_ = delegate; |
| 34 url_ = url; |
| 35 priority_ = priority; |
| 36 } |
| 37 |
| 38 URLRequestPeer::~URLRequestPeer() { |
| 39 CHECK(url_request_ == NULL); |
| 40 } |
| 41 |
| 42 void URLRequestPeer::SetMethod(const std::string &method) { |
| 43 method_ = method; |
| 44 } |
| 45 |
| 46 void URLRequestPeer::AddHeader(const std::string &name, |
| 47 const std::string &value) { |
| 48 headers_.SetHeader(name, value); |
| 49 } |
| 50 |
| 51 void URLRequestPeer::SetPostContent(const char *bytes, int bytes_len) { |
| 52 if (!upload_data_stream_) { |
| 53 upload_data_stream_.reset( |
| 54 new net::UploadDataStream(net::UploadDataStream::CHUNKED, 0)); |
| 55 } |
| 56 upload_data_stream_->AppendChunk(bytes, bytes_len, true /* is_last_chunk */); |
| 57 } |
| 58 |
| 59 GURL URLRequestPeer::url() const { |
| 60 return url_; |
| 61 } |
| 62 |
| 63 int URLRequestPeer::error_code() const { |
| 64 return error_code_; |
| 65 } |
| 66 |
| 67 int URLRequestPeer::http_status_code() const { |
| 68 return http_status_code_; |
| 69 } |
| 70 |
| 71 int64 URLRequestPeer::content_length() const { |
| 72 return expected_size_; |
| 73 } |
| 74 |
| 75 std::string URLRequestPeer::content_type() const { |
| 76 return content_type_; |
| 77 } |
| 78 |
| 79 void URLRequestPeer::Start() { |
| 80 context_->GetNetworkTaskRunner()->PostTask( |
| 81 FROM_HERE, |
| 82 base::Bind(&URLRequestPeer::OnInitiateConnectionWrapper, this)); |
| 83 } |
| 84 |
| 85 /* static */ |
| 86 void URLRequestPeer::OnInitiateConnectionWrapper(URLRequestPeer *self) { |
| 87 self->OnInitiateConnection(); |
| 88 } |
| 89 |
| 90 void URLRequestPeer::OnInitiateConnection() { |
| 91 if (canceled_) { |
| 92 return; |
| 93 } |
| 94 |
| 95 LOGV(LOG_TAG, context_->logging_level(), |
| 96 "Starting chromium request: %s, priority: %s", url_.spec().c_str(), |
| 97 RequestPriorityToString(priority_)); |
| 98 |
| 99 url_request_ = |
| 100 new net::URLRequest(url_, |
| 101 net::DEFAULT_PRIORITY, |
| 102 this, |
| 103 context_->GetURLRequestContext()); |
| 104 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
| 105 net::LOAD_DO_NOT_SAVE_COOKIES | |
| 106 net::LOAD_DO_NOT_SEND_COOKIES); |
| 107 url_request_->set_method(method_); |
| 108 url_request_->SetExtraRequestHeaders(headers_); |
| 109 std::string user_agent; |
| 110 if (headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { |
| 111 headers_.GetHeader(net::HttpRequestHeaders::kUserAgent, &user_agent); |
| 112 } else { |
| 113 user_agent = context_->GetUserAgent(url_); |
| 114 } |
| 115 size_t pos = user_agent.find(')'); |
| 116 if (pos != std::string::npos) { |
| 117 user_agent.insert(pos, context_->version()); |
| 118 user_agent.insert(pos, kUserAgentFragment); |
| 119 } |
| 120 url_request_->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kUserAgent, |
| 121 user_agent, true /* override */); |
| 122 LOGV(LOG_TAG, context_->logging_level(), "User agent: %s", |
| 123 user_agent.c_str()); |
| 124 |
| 125 if (upload_data_stream_) { |
| 126 url_request_->set_upload(make_scoped_ptr(upload_data_stream_.release())); |
| 127 } |
| 128 url_request_->SetPriority(priority_); |
| 129 |
| 130 url_request_->Start(); |
| 131 } |
| 132 |
| 133 void URLRequestPeer::Cancel() { |
| 134 if (canceled_) { |
| 135 return; |
| 136 } |
| 137 |
| 138 canceled_ = true; |
| 139 |
| 140 context_->GetNetworkTaskRunner()->PostTask( |
| 141 FROM_HERE, base::Bind(&URLRequestPeer::OnCancelRequestWrapper, this)); |
| 142 } |
| 143 |
| 144 /* static */ |
| 145 void URLRequestPeer::OnCancelRequestWrapper(URLRequestPeer *self) { |
| 146 self->OnCancelRequest(); |
| 147 } |
| 148 |
| 149 void URLRequestPeer::OnCancelRequest() { |
| 150 LOGV(LOG_TAG, context_->logging_level(), "Canceling chromium request: %s", |
| 151 url_.spec().c_str()); |
| 152 |
| 153 if (url_request_ != NULL) { |
| 154 url_request_->Cancel(); |
| 155 } |
| 156 |
| 157 OnRequestCanceled(); |
| 158 } |
| 159 |
| 160 void URLRequestPeer::Destroy() { |
| 161 context_->GetNetworkTaskRunner()->PostTask( |
| 162 FROM_HERE, base::Bind(&URLRequestPeer::OnDestroyRequest, this)); |
| 163 } |
| 164 |
| 165 /* static */ |
| 166 void URLRequestPeer::OnDestroyRequest(URLRequestPeer *self) { |
| 167 LOGV(LOG_TAG, self->context_->logging_level(), |
| 168 "Destroying chromium request: %s", self->url_.spec().c_str()); |
| 169 delete self; |
| 170 } |
| 171 |
| 172 void URLRequestPeer::OnResponseStarted(net::URLRequest *request) { |
| 173 if (request->status().status() != net::URLRequestStatus::SUCCESS) { |
| 174 OnRequestFailed(); |
| 175 return; |
| 176 } |
| 177 |
| 178 http_status_code_ = request->GetResponseCode(); |
| 179 LOGV(LOG_TAG, context_->logging_level(), "Response started with status %d", |
| 180 http_status_code_); |
| 181 |
| 182 request->GetResponseHeaderByName("Content-Type", &content_type_); |
| 183 expected_size_ = request->GetExpectedContentSize(); |
| 184 delegate_->OnResponseStarted(this); |
| 185 |
| 186 Read(); |
| 187 } |
| 188 |
| 189 /* |
| 190 * Reads all available data or starts an asynchronous read. |
| 191 */ |
| 192 void URLRequestPeer::Read() { |
| 193 while (1) { |
| 194 if (read_buffer_->RemainingCapacity() == 0) { |
| 195 int new_capacity = read_buffer_->capacity() + kBufferSizeIncrement; |
| 196 read_buffer_->SetCapacity(new_capacity); |
| 197 } |
| 198 |
| 199 int bytes_read; |
| 200 if (url_request_->Read(read_buffer_, read_buffer_->RemainingCapacity(), |
| 201 &bytes_read)) { |
| 202 if (bytes_read == 0) { |
| 203 OnRequestSucceeded(); |
| 204 break; |
| 205 } |
| 206 |
| 207 LOGV(LOG_TAG, context_->logging_level(), "Synchronously read %d bytes", |
| 208 bytes_read); |
| 209 OnBytesRead(bytes_read); |
| 210 } else if (url_request_->status().status() == |
| 211 net::URLRequestStatus::IO_PENDING) { |
| 212 if (bytes_read_ != 0) { |
| 213 LOGV(LOG_TAG, context_->logging_level(), "Flushing buffer: %d bytes", |
| 214 bytes_read_); |
| 215 delegate_->OnBytesRead(this); |
| 216 read_buffer_->set_offset(0); |
| 217 bytes_read_ = 0; |
| 218 } |
| 219 LOGV(LOG_TAG, context_->logging_level(), "Started async read"); |
| 220 break; |
| 221 } else { |
| 222 OnRequestFailed(); |
| 223 break; |
| 224 } |
| 225 } |
| 226 } |
| 227 |
| 228 void URLRequestPeer::OnReadCompleted(net::URLRequest *request, int bytes_read) { |
| 229 LOGV(LOG_TAG, context_->logging_level(), "Asynchronously read %d bytes", |
| 230 bytes_read); |
| 231 if (bytes_read < 0) { |
| 232 OnRequestFailed(); |
| 233 return; |
| 234 } else if (bytes_read == 0) { |
| 235 OnRequestSucceeded(); |
| 236 return; |
| 237 } |
| 238 |
| 239 OnBytesRead(bytes_read); |
| 240 Read(); |
| 241 } |
| 242 |
| 243 void URLRequestPeer::OnBytesRead(int bytes_read) { |
| 244 read_buffer_->set_offset(read_buffer_->offset() + bytes_read); |
| 245 bytes_read_ += bytes_read; |
| 246 total_bytes_read_ += bytes_read; |
| 247 } |
| 248 |
| 249 void URLRequestPeer::OnRequestSucceeded() { |
| 250 if (canceled_) { |
| 251 return; |
| 252 } |
| 253 |
| 254 LOGV(LOG_TAG, context_->logging_level(), |
| 255 "Request completed with HTTP status %d. Total bytes read: %d", |
| 256 http_status_code_, total_bytes_read_); |
| 257 OnRequestCompleted(); |
| 258 } |
| 259 |
| 260 void URLRequestPeer::OnRequestFailed() { |
| 261 if (canceled_) { |
| 262 return; |
| 263 } |
| 264 |
| 265 error_code_ = url_request_->status().error(); |
| 266 LOGV(LOG_TAG, context_->logging_level(), |
| 267 "Request failed with status %d and error %s", |
| 268 url_request_->status().status(), net::ErrorToString(error_code_)); |
| 269 OnRequestCompleted(); |
| 270 } |
| 271 |
| 272 void URLRequestPeer::OnRequestCanceled() { OnRequestCompleted(); } |
| 273 |
| 274 void URLRequestPeer::OnRequestCompleted() { |
| 275 LOGV(LOG_TAG, context_->logging_level(), "Completed: %s", |
| 276 url_.spec().c_str()); |
| 277 if (url_request_ != NULL) { |
| 278 delete url_request_; |
| 279 url_request_ = NULL; |
| 280 } |
| 281 |
| 282 delegate_->OnBytesRead(this); |
| 283 delegate_->OnRequestFinished(this); |
| 284 } |
| 285 |
| 286 size_t URLRequestPeer::BytesRead() const { return bytes_read_; } |
| 287 |
| 288 unsigned char *URLRequestPeer::Data() const { |
| 289 return reinterpret_cast<unsigned char *>(read_buffer_->StartOfBuffer()); |
| 290 } |
OLD | NEW |