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_adapter.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_adapter.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 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, | 18 URLRequestAdapter::URLRequestAdapter(URLRequestContextAdapter* context, |
19 URLRequestAdapterDelegate* 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 chunked_upload_(false) { |
31 context_ = context; | 32 context_ = context; |
32 delegate_ = delegate; | 33 delegate_ = delegate; |
33 url_ = url; | 34 url_ = url; |
34 priority_ = priority; | 35 priority_ = priority; |
35 } | 36 } |
36 | 37 |
37 URLRequestAdapter::~URLRequestAdapter() { | 38 URLRequestAdapter::~URLRequestAdapter() { |
38 CHECK(url_request_ == NULL); | 39 CHECK(url_request_ == NULL); |
39 } | 40 } |
40 | 41 |
(...skipping 14 matching lines...) Expand all Loading... |
55 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); | 56 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
56 } | 57 } |
57 | 58 |
58 void URLRequestAdapter::SetUploadChannel(JNIEnv* env, int64 content_length) { | 59 void URLRequestAdapter::SetUploadChannel(JNIEnv* env, int64 content_length) { |
59 scoped_ptr<net::UploadElementReader> reader( | 60 scoped_ptr<net::UploadElementReader> reader( |
60 new WrappedChannelElementReader(delegate_, content_length)); | 61 new WrappedChannelElementReader(delegate_, content_length)); |
61 upload_data_stream_.reset( | 62 upload_data_stream_.reset( |
62 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); | 63 net::UploadDataStream::CreateWithReader(reader.Pass(), 0)); |
63 } | 64 } |
64 | 65 |
| 66 void URLRequestAdapter::EnableChunkedUpload() { |
| 67 chunked_upload_ = true; |
| 68 } |
| 69 |
| 70 void URLRequestAdapter::AppendChunkToUpload(const char* bytes, |
| 71 int bytes_len, |
| 72 bool is_last_chunk) { |
| 73 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; |
| 74 context_->GetNetworkTaskRunner()->PostTask( |
| 75 FROM_HERE, |
| 76 base::Bind(&URLRequestAdapter::OnAppendChunk, |
| 77 base::Unretained(this), |
| 78 bytes, |
| 79 bytes_len, |
| 80 is_last_chunk)); |
| 81 } |
| 82 |
65 std::string URLRequestAdapter::GetHeader(const std::string& name) const { | 83 std::string URLRequestAdapter::GetHeader(const std::string& name) const { |
66 std::string value; | 84 std::string value; |
67 if (url_request_ != NULL) { | 85 if (url_request_ != NULL) { |
68 url_request_->GetResponseHeaderByName(name, &value); | 86 url_request_->GetResponseHeaderByName(name, &value); |
69 } | 87 } |
70 return value; | 88 return value; |
71 } | 89 } |
72 | 90 |
73 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { | 91 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { |
74 if (url_request_ == NULL) { | 92 if (url_request_ == NULL) { |
75 return NULL; | 93 return NULL; |
76 } | 94 } |
77 return url_request_->response_headers(); | 95 return url_request_->response_headers(); |
78 } | 96 } |
79 | 97 |
80 void URLRequestAdapter::Start() { | 98 void URLRequestAdapter::Start() { |
81 context_->GetNetworkTaskRunner()->PostTask( | 99 context_->GetNetworkTaskRunner()->PostTask( |
82 FROM_HERE, | 100 FROM_HERE, |
83 base::Bind(&URLRequestAdapter::OnInitiateConnection, | 101 base::Bind(&URLRequestAdapter::OnInitiateConnection, |
84 base::Unretained(this))); | 102 base::Unretained(this))); |
85 } | 103 } |
86 | 104 |
| 105 void URLRequestAdapter::OnAppendChunk(const char* bytes, |
| 106 int bytes_len, |
| 107 bool is_last_chunk) { |
| 108 if (url_request_ != NULL) { |
| 109 url_request_->AppendChunkToUpload(bytes, bytes_len, is_last_chunk); |
| 110 delegate_->OnAppendChunkCompleted(this); |
| 111 } |
| 112 } |
| 113 |
87 void URLRequestAdapter::OnInitiateConnection() { | 114 void URLRequestAdapter::OnInitiateConnection() { |
88 if (canceled_) { | 115 if (canceled_) { |
89 return; | 116 return; |
90 } | 117 } |
91 | 118 |
92 VLOG(1) << "Starting chromium request: " | 119 VLOG(1) << "Starting chromium request: " |
93 << url_.possibly_invalid_spec().c_str() | 120 << url_.possibly_invalid_spec().c_str() |
94 << " priority: " << RequestPriorityToString(priority_); | 121 << " priority: " << RequestPriorityToString(priority_); |
95 url_request_ = new net::URLRequest( | 122 url_request_ = new net::URLRequest( |
96 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); | 123 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); |
97 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 124 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
98 net::LOAD_DO_NOT_SAVE_COOKIES | | 125 net::LOAD_DO_NOT_SAVE_COOKIES | |
99 net::LOAD_DO_NOT_SEND_COOKIES); | 126 net::LOAD_DO_NOT_SEND_COOKIES); |
100 url_request_->set_method(method_); | 127 url_request_->set_method(method_); |
101 url_request_->SetExtraRequestHeaders(headers_); | 128 url_request_->SetExtraRequestHeaders(headers_); |
102 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { | 129 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { |
103 std::string user_agent; | 130 std::string user_agent; |
104 user_agent = context_->GetUserAgent(url_); | 131 user_agent = context_->GetUserAgent(url_); |
105 url_request_->SetExtraRequestHeaderByName( | 132 url_request_->SetExtraRequestHeaderByName( |
106 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); | 133 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); |
107 } | 134 } |
108 | 135 |
109 if (upload_data_stream_) | 136 if (upload_data_stream_) { |
110 url_request_->set_upload(upload_data_stream_.Pass()); | 137 url_request_->set_upload(upload_data_stream_.Pass()); |
| 138 } else if (chunked_upload_) { |
| 139 url_request_->EnableChunkedUpload(); |
| 140 } |
111 | 141 |
112 url_request_->SetPriority(priority_); | 142 url_request_->SetPriority(priority_); |
113 | 143 |
114 url_request_->Start(); | 144 url_request_->Start(); |
115 } | 145 } |
116 | 146 |
117 void URLRequestAdapter::Cancel() { | 147 void URLRequestAdapter::Cancel() { |
118 if (canceled_) { | 148 if (canceled_) { |
119 return; | 149 return; |
120 } | 150 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 286 |
257 delegate_->OnBytesRead(this); | 287 delegate_->OnBytesRead(this); |
258 delegate_->OnRequestFinished(this); | 288 delegate_->OnRequestFinished(this); |
259 } | 289 } |
260 | 290 |
261 unsigned char* URLRequestAdapter::Data() const { | 291 unsigned char* URLRequestAdapter::Data() const { |
262 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 292 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); |
263 } | 293 } |
264 | 294 |
265 } // namespace cronet | 295 } // namespace cronet |
OLD | NEW |