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 } | |
122 | |
123 int URLLoaderWrapperImpl::GetContentLength() const { | |
124 return content_length_; | |
125 } | |
126 | |
127 bool URLLoaderWrapperImpl::IsAcceptRangesBytes() const { | |
128 return accept_ranges_bytes_; | |
129 } | |
130 | |
131 bool URLLoaderWrapperImpl::IsContentEncoded() const { | |
132 return content_encoded_; | |
133 } | |
134 | |
135 std::string URLLoaderWrapperImpl::GetContentType() const { | |
136 return content_type_; | |
137 } | |
138 std::string URLLoaderWrapperImpl::GetContentDisposition() const { | |
139 return content_disposition_; | |
140 } | |
141 | |
142 int URLLoaderWrapperImpl::GetStatusCode() const { | |
143 return url_loader_.GetResponseInfo().GetStatusCode(); | |
144 } | |
145 | |
146 bool URLLoaderWrapperImpl::IsMultipart() const { | |
147 return is_multipart_; | |
148 } | |
149 | |
150 bool URLLoaderWrapperImpl::GetByteRange(int* start, int* end) const { | |
151 DCHECK(start); | |
152 DCHECK(end); | |
153 *start = byte_range_.start(); | |
154 *end = byte_range_.end(); | |
155 return byte_range_.IsValid(); | |
156 } | |
157 | |
158 bool URLLoaderWrapperImpl::GetDownloadProgress( | |
159 int64_t* bytes_received, | |
160 int64_t* total_bytes_to_be_received) const { | |
161 return url_loader_.GetDownloadProgress(bytes_received, | |
162 total_bytes_to_be_received); | |
163 } | |
164 | |
165 void URLLoaderWrapperImpl::Close() { | |
166 url_loader_.Close(); | |
167 read_starter_.reset(); | |
168 } | |
169 | |
170 void URLLoaderWrapperImpl::OpenRange(const std::string& url, | |
171 const std::string& referrer_url, | |
172 uint32_t position, | |
173 uint32_t size, | |
174 const pp::CompletionCallback& cc) { | |
175 did_open_callback_ = cc; | |
176 pp::CompletionCallback callback = | |
177 callback_factory_.NewCallback(&URLLoaderWrapperImpl::DidOpen); | |
178 int rv = url_loader_.Open( | |
179 MakeRangeRequest(plugin_instance_, url, referrer_url, position, size), | |
180 callback); | |
181 if (rv != PP_OK_COMPLETIONPENDING) | |
182 callback.Run(rv); | |
183 } | |
184 | |
185 void URLLoaderWrapperImpl::ReadResponseBody(char* buffer, | |
186 int buffer_size, | |
187 const pp::CompletionCallback& cc) { | |
188 did_read_callback_ = cc; | |
189 buffer_ = buffer; | |
190 buffer_size_ = buffer_size; | |
191 read_starter_ = base::MakeUnique<ReadStarter>(this); | |
192 } | |
193 | |
194 void URLLoaderWrapperImpl::ReadResponseBodyImpl() { | |
195 read_starter_.reset(); | |
196 pp::CompletionCallback callback = | |
197 callback_factory_.NewCallback(&URLLoaderWrapperImpl::DidRead); | |
198 int rv = url_loader_.ReadResponseBody(buffer_, buffer_size_, callback); | |
199 if (rv != PP_OK_COMPLETIONPENDING) { | |
200 callback.Run(rv); | |
201 } | |
202 } | |
203 | |
204 void URLLoaderWrapperImpl::SetResponseHeaders( | |
205 const std::string& response_headers) { | |
206 response_headers_ = response_headers; | |
207 ParseHeaders(); | |
208 } | |
209 | |
210 void URLLoaderWrapperImpl::ParseHeaders() { | |
211 content_length_ = -1; | |
212 accept_ranges_bytes_ = false; | |
213 content_encoded_ = false; | |
214 content_type_.clear(); | |
215 content_disposition_.clear(); | |
216 multipart_boundary_.clear(); | |
217 byte_range_ = gfx::Range::InvalidRange(); | |
218 is_multipart_ = false; | |
219 | |
220 if (response_headers_.empty()) | |
221 return; | |
222 | |
223 net::HttpUtil::HeadersIterator it(response_headers_.begin(), | |
224 response_headers_.end(), "\n"); | |
225 while (it.GetNext()) { | |
226 if (base::LowerCaseEqualsASCII(it.name(), "content-length")) { | |
227 content_length_ = atoi(it.values().c_str()); | |
228 } else if (base::LowerCaseEqualsASCII(it.name(), "accept-ranges")) { | |
229 accept_ranges_bytes_ = base::LowerCaseEqualsASCII(it.values(), "bytes"); | |
230 } else if (base::LowerCaseEqualsASCII(it.name(), "content-encoding")) { | |
231 content_encoded_ = true; | |
232 } else if (base::LowerCaseEqualsASCII(it.name(), "content-type")) { | |
233 content_type_ = it.values(); | |
234 size_t semi_colon_pos = content_type_.find(';'); | |
235 if (semi_colon_pos != std::string::npos) { | |
236 content_type_ = content_type_.substr(0, semi_colon_pos); | |
237 } | |
238 base::TrimWhitespaceASCII(content_type_, base::TRIM_ALL, &content_type_); | |
239 // multipart boundary. | |
240 std::string type = base::ToLowerASCII(it.values()); | |
241 if (base::StartsWith(type, "multipart/", base::CompareCase::SENSITIVE)) { | |
242 const char* boundary = strstr(type.c_str(), "boundary="); | |
243 DCHECK(boundary); | |
244 if (boundary) { | |
245 multipart_boundary_ = std::string(boundary + 9); | |
246 is_multipart_ = !multipart_boundary_.empty(); | |
247 } | |
248 } | |
249 } else if (base::LowerCaseEqualsASCII(it.name(), "content-disposition")) { | |
250 content_disposition_ = it.values(); | |
251 } else if (base::LowerCaseEqualsASCII(it.name(), "content-range")) { | |
252 int start = 0; | |
253 int end = 0; | |
254 if (GetByteRangeFromStr(it.values().c_str(), &start, &end)) { | |
255 byte_range_ = gfx::Range(start, end); | |
256 } | |
257 } | |
258 } | |
259 } | |
260 | |
261 void URLLoaderWrapperImpl::DidOpen(int32_t result) { | |
262 SetHeadersFromLoader(); | |
263 did_open_callback_.Run(result); | |
264 } | |
265 | |
266 void URLLoaderWrapperImpl::DidRead(int32_t result) { | |
267 if (multi_part_processed_) { | |
268 // Reset this flag so we look inside the buffer in calls of DidRead for this | |
269 // response only once. Note that this code DOES NOT handle multi part | |
270 // responses with more than one part (we don't issue them at the moment, so | |
271 // they shouldn't arrive). | |
272 is_multipart_ = false; | |
273 } | |
274 if (result <= 0 || !is_multipart_) { | |
275 did_read_callback_.Run(result); | |
276 return; | |
277 } | |
278 if (result <= 2) { | |
279 // TODO(art-snake): Accumulate data for parse headers. | |
280 did_read_callback_.Run(result); | |
281 return; | |
282 } | |
283 | |
284 char* start = buffer_; | |
285 size_t length = result; | |
286 multi_part_processed_ = true; | |
287 for (int i = 2; i < result; ++i) { | |
288 if (IsDoubleEndLineAtEnd(buffer_, i)) { | |
289 int start_pos = 0; | |
290 int end_pos = 0; | |
291 if (GetByteRangeFromHeaders(std::string(buffer_, i), &start_pos, | |
292 &end_pos)) { | |
293 byte_range_ = gfx::Range(start_pos, end_pos); | |
294 start += i; | |
295 length -= i; | |
296 } | |
297 break; | |
298 } | |
299 } | |
300 result = length; | |
301 if (result == 0) { | |
302 // Continue receiving. | |
303 return ReadResponseBodyImpl(); | |
304 } | |
305 DCHECK(result > 0); | |
306 memmove(buffer_, start, result); | |
307 | |
308 did_read_callback_.Run(result); | |
309 } | |
310 | |
311 void URLLoaderWrapperImpl::SetHeadersFromLoader() { | |
312 pp::URLResponseInfo response = url_loader_.GetResponseInfo(); | |
313 pp::Var headers_var = response.GetHeaders(); | |
314 | |
315 SetResponseHeaders(headers_var.is_string() ? headers_var.AsString() : ""); | |
316 } | |
317 | |
318 } // namespace chrome_pdf | |
OLD | NEW |