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