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