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::AppendChunk(const char* bytes, int bytes_len, | |
71 bool is_last_chunk) { | |
72 VLOG(1) << "AppendChunk, len: " << bytes_len << ", last: " << is_last_chunk; | |
mmenke
2014/08/15 18:50:39
include base/logging.h
mdumitrescu
2014/08/18 11:28:35
Done.
| |
73 scoped_ptr<char[]> buf(new char[bytes_len]); | |
74 memcpy(buf.get(), bytes, bytes_len); | |
mmenke
2014/08/15 18:50:39
include string.h for memcpy. Should go below "url
mdumitrescu
2014/08/18 11:28:34
Done.
| |
75 context_->GetNetworkTaskRunner()->PostTask( | |
76 FROM_HERE, | |
mmenke
2014/08/15 18:50:39
include base/location.h
mdumitrescu
2014/08/18 11:28:34
Done.
| |
77 base::Bind(&URLRequestAdapter::OnAppendChunk, | |
mmenke
2014/08/15 18:50:39
include base/bind.h
mdumitrescu
2014/08/18 11:28:35
Done.
| |
78 base::Unretained(this), | |
79 Passed(buf.Pass()), | |
80 bytes_len, | |
81 is_last_chunk)); | |
82 } | |
83 | |
65 std::string URLRequestAdapter::GetHeader(const std::string& name) const { | 84 std::string URLRequestAdapter::GetHeader(const std::string& name) const { |
66 std::string value; | 85 std::string value; |
67 if (url_request_ != NULL) { | 86 if (url_request_ != NULL) { |
68 url_request_->GetResponseHeaderByName(name, &value); | 87 url_request_->GetResponseHeaderByName(name, &value); |
69 } | 88 } |
70 return value; | 89 return value; |
71 } | 90 } |
72 | 91 |
73 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { | 92 net::HttpResponseHeaders* URLRequestAdapter::GetResponseHeaders() const { |
74 if (url_request_ == NULL) { | 93 if (url_request_ == NULL) { |
75 return NULL; | 94 return NULL; |
76 } | 95 } |
77 return url_request_->response_headers(); | 96 return url_request_->response_headers(); |
78 } | 97 } |
79 | 98 |
80 void URLRequestAdapter::Start() { | 99 void URLRequestAdapter::Start() { |
81 context_->GetNetworkTaskRunner()->PostTask( | 100 context_->GetNetworkTaskRunner()->PostTask( |
82 FROM_HERE, | 101 FROM_HERE, |
83 base::Bind(&URLRequestAdapter::OnInitiateConnection, | 102 base::Bind(&URLRequestAdapter::OnInitiateConnection, |
84 base::Unretained(this))); | 103 base::Unretained(this))); |
85 } | 104 } |
86 | 105 |
106 void URLRequestAdapter::OnAppendChunk(const scoped_ptr<char[]> bytes, | |
107 int bytes_len, bool is_last_chunk) { | |
108 url_request_->AppendChunkToUpload(bytes.get(), bytes_len, is_last_chunk); | |
109 } | |
110 | |
87 void URLRequestAdapter::OnInitiateConnection() { | 111 void URLRequestAdapter::OnInitiateConnection() { |
88 if (canceled_) { | 112 if (canceled_) { |
89 return; | 113 return; |
90 } | 114 } |
91 | 115 |
92 VLOG(1) << "Starting chromium request: " | 116 VLOG(1) << "Starting chromium request: " |
93 << url_.possibly_invalid_spec().c_str() | 117 << url_.possibly_invalid_spec().c_str() |
94 << " priority: " << RequestPriorityToString(priority_); | 118 << " priority: " << RequestPriorityToString(priority_); |
95 url_request_ = new net::URLRequest( | 119 url_request_ = new net::URLRequest( |
96 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); | 120 url_, net::DEFAULT_PRIORITY, this, context_->GetURLRequestContext()); |
97 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | 121 url_request_->SetLoadFlags(net::LOAD_DISABLE_CACHE | |
98 net::LOAD_DO_NOT_SAVE_COOKIES | | 122 net::LOAD_DO_NOT_SAVE_COOKIES | |
99 net::LOAD_DO_NOT_SEND_COOKIES); | 123 net::LOAD_DO_NOT_SEND_COOKIES); |
100 url_request_->set_method(method_); | 124 url_request_->set_method(method_); |
101 url_request_->SetExtraRequestHeaders(headers_); | 125 url_request_->SetExtraRequestHeaders(headers_); |
102 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { | 126 if (!headers_.HasHeader(net::HttpRequestHeaders::kUserAgent)) { |
103 std::string user_agent; | 127 std::string user_agent; |
104 user_agent = context_->GetUserAgent(url_); | 128 user_agent = context_->GetUserAgent(url_); |
105 url_request_->SetExtraRequestHeaderByName( | 129 url_request_->SetExtraRequestHeaderByName( |
106 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); | 130 net::HttpRequestHeaders::kUserAgent, user_agent, true /* override */); |
107 } | 131 } |
108 | 132 |
109 if (upload_data_stream_) | 133 if (upload_data_stream_) { |
110 url_request_->set_upload(upload_data_stream_.Pass()); | 134 url_request_->set_upload(upload_data_stream_.Pass()); |
135 } else if (chunked_upload_) { | |
136 url_request_->EnableChunkedUpload(); | |
137 } | |
111 | 138 |
112 url_request_->SetPriority(priority_); | 139 url_request_->SetPriority(priority_); |
113 | 140 |
114 url_request_->Start(); | 141 url_request_->Start(); |
115 } | 142 } |
116 | 143 |
117 void URLRequestAdapter::Cancel() { | 144 void URLRequestAdapter::Cancel() { |
118 if (canceled_) { | 145 if (canceled_) { |
119 return; | 146 return; |
120 } | 147 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 | 283 |
257 delegate_->OnBytesRead(this); | 284 delegate_->OnBytesRead(this); |
258 delegate_->OnRequestFinished(this); | 285 delegate_->OnRequestFinished(this); |
259 } | 286 } |
260 | 287 |
261 unsigned char* URLRequestAdapter::Data() const { | 288 unsigned char* URLRequestAdapter::Data() const { |
262 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); | 289 return reinterpret_cast<unsigned char*>(read_buffer_->StartOfBuffer()); |
263 } | 290 } |
264 | 291 |
265 } // namespace cronet | 292 } // namespace cronet |
OLD | NEW |