OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "pdf/url_loader_wrapper_impl.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "net/http/http_util.h" | |
12 #include "pdf/timer.h" | |
13 #include "ppapi/c/pp_errors.h" | |
14 #include "ppapi/cpp/logging.h" | |
15 #include "ppapi/cpp/url_request_info.h" | |
16 #include "ppapi/cpp/url_response_info.h" | |
17 | |
18 namespace chrome_pdf { | |
19 | |
20 namespace { | |
21 // We should read with delay to prevent block UI thread, and reduce CPU usage. | |
22 const int kReadDelayMs = 2; | |
23 | |
24 pp::URLRequestInfo MakeRangeRequest(pp::Instance* plugin_instance, | |
25 const std::string& url, | |
26 const std::string& referrer_url, | |
27 uint32_t position, | |
28 uint32_t size) { | |
29 pp::URLRequestInfo request(plugin_instance); | |
30 request.SetURL(url); | |
31 request.SetMethod("GET"); | |
32 request.SetFollowRedirects(false); | |
33 request.SetCustomReferrerURL(referrer_url); | |
34 | |
35 // According to rfc2616, byte range specifies position of the first and last | |
36 // bytes in the requested range inclusively. Therefore we should subtract 1 | |
37 // from the position + size, to get index of the last byte that needs to be | |
38 // downloaded. | |
39 std::string str_header = | |
40 base::StringPrintf("Range: bytes=%d-%d", position, position + size - 1); | |
41 pp::Var header(str_header.c_str()); | |
42 request.SetHeaders(header); | |
43 | |
44 return request; | |
45 } | |
46 | |
47 bool GetByteRangeFromStr(const std::string& content_range_str, | |
48 int* start, | |
49 int* end) { | |
50 std::string range = content_range_str; | |
51 if (!base::StartsWith(range, "bytes", base::CompareCase::INSENSITIVE_ASCII)) | |
52 return false; | |
53 | |
54 range = range.substr(strlen("bytes")); | |
55 std::string::size_type pos = range.find('-'); | |
56 std::string range_end; | |
57 if (pos != std::string::npos) | |
58 range_end = range.substr(pos + 1); | |
59 base::TrimWhitespaceASCII(range, base::TRIM_LEADING, &range); | |
60 base::TrimWhitespaceASCII(range_end, base::TRIM_LEADING, &range_end); | |
61 *start = atoi(range.c_str()); | |
62 *end = atoi(range_end.c_str()); | |
63 return true; | |
64 } | |
65 | |
66 // If the headers have a byte-range response, writes the start and end | |
67 // positions and returns true if at least the start position was parsed. | |
68 // The end position will be set to 0 if it was not found or parsed from the | |
69 // response. | |
70 // Returns false if not even a start position could be parsed. | |
71 bool GetByteRangeFromHeaders(const std::string& headers, int* start, int* end) { | |
72 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); | |
73 while (it.GetNext()) { | |
74 if (base::LowerCaseEqualsASCII(it.name(), "content-range")) { | |
75 if (GetByteRangeFromStr(it.values().c_str(), start, end)) | |
76 return true; | |
77 } | |
78 } | |
79 return false; | |
80 } | |
81 | |
82 bool IsDoubleEndLineAtEnd(const char* buffer, int size) { | |
83 if (size < 2) | |
84 return false; | |
85 | |
86 if (buffer[size - 1] == '\n' && buffer[size - 2] == '\n') | |
87 return true; | |
88 | |
89 if (size < 4) | |
90 return false; | |
91 | |
92 return buffer[size - 1] == '\n' && buffer[size - 2] == '\r' && | |
93 buffer[size - 3] == '\n' && buffer[size - 4] == '\r'; | |
94 } | |
95 | |
96 } // namespace | |
97 | |
98 class URLLoaderWrapperImpl::ReadStarter : public Timer { | |
99 public: | |
100 explicit ReadStarter(URLLoaderWrapperImpl* owner) | |
101 : Timer(kReadDelayMs), owner_(owner) {} | |
102 ~ReadStarter() override {} | |
103 | |
104 // Timer overrides: | |
105 void OnTimer() override { owner_->ReadResponseBodyImpl(); } | |
106 | |
107 private: | |
108 URLLoaderWrapperImpl* owner_; | |
109 }; | |
110 | |
111 URLLoaderWrapperImpl::URLLoaderWrapperImpl(pp::Instance* plugin_instance, | |
112 const pp::URLLoader& url_loader) | |
113 : plugin_instance_(plugin_instance), | |
114 url_loader_(url_loader), | |
115 callback_factory_(this) { | |
116 SetHeadersFromLoader(); | |
117 } | |
118 | |
119 URLLoaderWrapperImpl::~URLLoaderWrapperImpl() { | |
120 Close(); | |
121 // We should call callbacks to prevent memory leaks. | |
122 // The callbacks don't do anything, because the objects that created the | |
123 // callbacks have been destroyed. | |
124 if (!did_open_callback_.IsOptional()) | |
125 did_open_callback_.RunAndClear(-1); | |
126 if (!did_read_callback_.IsOptional()) | |
127 did_read_callback_.RunAndClear(-1); | |
128 } | |
129 | |
130 int URLLoaderWrapperImpl::GetContentLength() const { | |
131 return content_length_; | |
132 } | |
133 | |
134 bool URLLoaderWrapperImpl::IsAcceptRangesBytes() const { | |
135 return accept_ranges_bytes_; | |
136 } | |
137 | |
138 bool URLLoaderWrapperImpl::IsContentEncoded() const { | |
139 return content_encoded_; | |
140 } | |
141 | |
142 std::string URLLoaderWrapperImpl::GetContentType() const { | |
143 return content_type_; | |
144 } | |
145 std::string URLLoaderWrapperImpl::GetContentDisposition() const { | |
146 return content_disposition_; | |
147 } | |
148 | |
149 int URLLoaderWrapperImpl::GetStatusCode() const { | |
150 return url_loader_.GetResponseInfo().GetStatusCode(); | |
151 } | |
152 | |
153 bool URLLoaderWrapperImpl::IsMultipart() const { | |
154 return is_multipart_; | |
155 } | |
156 | |
157 bool URLLoaderWrapperImpl::GetByteRange(int* start, int* end) const { | |
158 DCHECK(start); | |
159 DCHECK(end); | |
160 *start = byte_range_.start(); | |
161 *end = byte_range_.end(); | |
162 return byte_range_.IsValid(); | |
163 } | |
164 | |
165 bool URLLoaderWrapperImpl::GetDownloadProgress( | |
166 int64_t* bytes_received, | |
167 int64_t* total_bytes_to_be_received) const { | |
168 return url_loader_.GetDownloadProgress(bytes_received, | |
169 total_bytes_to_be_received); | |
170 } | |
171 | |
172 void URLLoaderWrapperImpl::Close() { | |
173 url_loader_.Close(); | |
174 read_starter_.reset(); | |
175 } | |
176 | |
177 void URLLoaderWrapperImpl::OpenRange(const std::string& url, | |
178 const std::string& referrer_url, | |
179 uint32_t position, | |
180 uint32_t size, | |
181 const pp::CompletionCallback& cc) { | |
182 did_open_callback_ = cc; | |
183 pp::CompletionCallback callback = | |
184 callback_factory_.NewCallback(&URLLoaderWrapperImpl::DidOpen); | |
185 int rv = url_loader_.Open( | |
186 MakeRangeRequest(plugin_instance_, url, referrer_url, position, size), | |
187 callback); | |
188 if (rv != PP_OK_COMPLETIONPENDING) | |
189 callback.Run(rv); | |
190 } | |
191 | |
192 void URLLoaderWrapperImpl::ReadResponseBody(char* buffer, | |
193 int buffer_size, | |
194 const pp::CompletionCallback& cc) { | |
195 did_read_callback_ = cc; | |
196 buffer_ = buffer; | |
197 buffer_size_ = buffer_size; | |
198 read_starter_ = base::MakeUnique<ReadStarter>(this); | |
199 } | |
200 | |
201 void URLLoaderWrapperImpl::ReadResponseBodyImpl() { | |
202 read_starter_.reset(); | |
203 pp::CompletionCallback callback = | |
204 callback_factory_.NewCallback(&URLLoaderWrapperImpl::DidRead); | |
205 int rv = url_loader_.ReadResponseBody(buffer_, buffer_size_, callback); | |
206 if (rv != PP_OK_COMPLETIONPENDING) { | |
207 callback.Run(rv); | |
208 } | |
209 } | |
210 | |
211 void URLLoaderWrapperImpl::SetResponseHeaders( | |
212 const std::string& response_headers) { | |
213 response_headers_ = response_headers; | |
214 ParseHeaders(); | |
215 } | |
216 | |
217 void URLLoaderWrapperImpl::ParseHeaders() { | |
218 content_length_ = -1; | |
219 accept_ranges_bytes_ = false; | |
220 content_encoded_ = false; | |
221 content_type_.clear(); | |
222 content_disposition_.clear(); | |
223 multipart_boundary_.clear(); | |
224 byte_range_ = gfx::Range::InvalidRange(); | |
225 is_multipart_ = false; | |
226 | |
227 if (response_headers_.empty()) | |
228 return; | |
229 | |
230 net::HttpUtil::HeadersIterator it(response_headers_.begin(), | |
231 response_headers_.end(), "\n"); | |
232 while (it.GetNext()) { | |
233 if (base::LowerCaseEqualsASCII(it.name(), "content-length")) { | |
234 content_length_ = atoi(it.values().c_str()); | |
235 } else if (base::LowerCaseEqualsASCII(it.name(), "accept-ranges")) { | |
236 accept_ranges_bytes_ = base::LowerCaseEqualsASCII(it.values(), "bytes"); | |
237 } else if (base::LowerCaseEqualsASCII(it.name(), "content-encoding")) { | |
238 content_encoded_ = true; | |
239 } else if (base::LowerCaseEqualsASCII(it.name(), "content-type")) { | |
240 content_type_ = it.values(); | |
241 size_t semi_colon_pos = content_type_.find(';'); | |
242 if (semi_colon_pos != std::string::npos) { | |
243 content_type_ = content_type_.substr(0, semi_colon_pos); | |
244 } | |
245 base::TrimWhitespaceASCII(content_type_, base::TRIM_ALL, &content_type_); | |
246 // multipart boundary. | |
247 std::string type = base::ToLowerASCII(it.values()); | |
248 if (base::StartsWith(type, "multipart/", base::CompareCase::SENSITIVE)) { | |
249 const char* boundary = strstr(type.c_str(), "boundary="); | |
250 DCHECK(boundary); | |
251 if (boundary) { | |
252 multipart_boundary_ = std::string(boundary + 9); | |
253 is_multipart_ = !multipart_boundary_.empty(); | |
254 } | |
255 } | |
256 } else if (base::LowerCaseEqualsASCII(it.name(), "content-disposition")) { | |
257 content_disposition_ = it.values(); | |
258 } else if (base::LowerCaseEqualsASCII(it.name(), "content-range")) { | |
259 int start = 0; | |
260 int end = 0; | |
261 if (GetByteRangeFromStr(it.values().c_str(), &start, &end)) { | |
262 byte_range_ = gfx::Range(start, end); | |
263 } | |
264 } | |
265 } | |
266 } | |
267 | |
268 void URLLoaderWrapperImpl::DidOpen(int32_t result) { | |
269 SetHeadersFromLoader(); | |
270 did_open_callback_.RunAndClear(result); | |
271 } | |
272 | |
273 void URLLoaderWrapperImpl::DidRead(int32_t result) { | |
274 if (multi_part_processed_) { | |
275 // Reset this flag so we look inside the buffer in calls of DidRead for this | |
276 // response only once. Note that this code DOES NOT handle multi part | |
277 // responses with more than one part (we don't issue them at the moment, so | |
278 // they shouldn't arrive). | |
279 is_multipart_ = false; | |
280 } | |
281 if (result <= 0 || !is_multipart_) { | |
282 did_read_callback_.RunAndClear(result); | |
283 return; | |
284 } | |
285 if (result <= 2) { | |
286 // TODO(art-snake): Accumulate data for parse headers. | |
287 did_read_callback_.RunAndClear(result); | |
288 return; | |
289 } | |
290 | |
291 char* start = buffer_; | |
292 size_t length = result; | |
293 multi_part_processed_ = true; | |
294 for (int i = 2; i < result; ++i) { | |
295 if (IsDoubleEndLineAtEnd(buffer_, i)) { | |
296 int start_pos = 0; | |
297 int end_pos = 0; | |
298 if (GetByteRangeFromHeaders(std::string(buffer_, i), &start_pos, | |
299 &end_pos)) { | |
300 byte_range_ = gfx::Range(start_pos, end_pos); | |
301 start += i; | |
302 length -= i; | |
303 } | |
304 break; | |
305 } | |
306 } | |
307 result = length; | |
308 if (result == 0) { | |
309 // Continue receiving. | |
310 return ReadResponseBodyImpl(); | |
311 } | |
312 DCHECK(result > 0); | |
313 memmove(buffer_, start, result); | |
314 | |
315 did_read_callback_.RunAndClear(result); | |
316 } | |
317 | |
318 void URLLoaderWrapperImpl::SetHeadersFromLoader() { | |
319 pp::URLResponseInfo response = url_loader_.GetResponseInfo(); | |
320 pp::Var headers_var = response.GetHeaders(); | |
321 | |
322 SetResponseHeaders(headers_var.is_string() ? headers_var.AsString() : ""); | |
323 } | |
324 | |
325 } // namespace chrome_pdf | |
OLD | NEW |