OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "pdf/document_loader.h" | 5 #include "pdf/document_loader.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "net/http/http_util.h" | 12 #include "net/http/http_util.h" |
13 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
14 #include "ppapi/cpp/url_loader.h" | 14 #include "ppapi/cpp/url_loader.h" |
15 #include "ppapi/cpp/url_request_info.h" | 15 #include "ppapi/cpp/url_request_info.h" |
16 #include "ppapi/cpp/url_response_info.h" | 16 #include "ppapi/cpp/url_response_info.h" |
17 | 17 |
18 namespace chrome_pdf { | 18 namespace chrome_pdf { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Return true if the HTTP response of |loader| is a succesful one and loading | |
Lei Zhang
2016/11/03 07:10:45
typo
raymes
2016/11/07 03:25:44
Done.
| |
23 // should continue. 4xx error indicate subsequent requests will fail too. | |
24 // e.g. resource has been removed from the server while loading it. 301 | |
25 // indicates a redirect was returned which won't be successful because we | |
26 // disable following redirects for PDF loading (we assume they are already | |
27 // resolved by the browser. | |
28 bool ResponseStatusSuccess(const pp::URLLoader& loader) { | |
29 int32_t http_code = loader.GetResponseInfo().GetStatusCode(); | |
30 if ((http_code >= 400 && http_code < 500) || http_code == 301) | |
Lei Zhang
2016/11/03 07:10:45
Just: return (http_code < 400 && http_code != 301)
raymes
2016/11/07 03:25:44
Done.
| |
31 return false; | |
32 | |
33 return true; | |
34 } | |
35 | |
22 // If the headers have a byte-range response, writes the start and end | 36 // If the headers have a byte-range response, writes the start and end |
23 // positions and returns true if at least the start position was parsed. | 37 // positions and returns true if at least the start position was parsed. |
24 // The end position will be set to 0 if it was not found or parsed from the | 38 // The end position will be set to 0 if it was not found or parsed from the |
25 // response. | 39 // response. |
26 // Returns false if not even a start position could be parsed. | 40 // Returns false if not even a start position could be parsed. |
27 bool GetByteRange(const std::string& headers, uint32_t* start, uint32_t* end) { | 41 bool GetByteRange(const std::string& headers, uint32_t* start, uint32_t* end) { |
28 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); | 42 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); |
29 while (it.GetNext()) { | 43 while (it.GetNext()) { |
30 if (base::LowerCaseEqualsASCII(it.name(), "content-range")) { | 44 if (base::LowerCaseEqualsASCII(it.name(), "content-range")) { |
31 std::string range = it.values().c_str(); | 45 std::string range = it.values().c_str(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 DocumentLoader::~DocumentLoader() { | 109 DocumentLoader::~DocumentLoader() { |
96 } | 110 } |
97 | 111 |
98 bool DocumentLoader::Init(const pp::URLLoader& loader, | 112 bool DocumentLoader::Init(const pp::URLLoader& loader, |
99 const std::string& url, | 113 const std::string& url, |
100 const std::string& headers) { | 114 const std::string& headers) { |
101 DCHECK(url_.empty()); | 115 DCHECK(url_.empty()); |
102 url_ = url; | 116 url_ = url; |
103 loader_ = loader; | 117 loader_ = loader; |
104 | 118 |
119 // Check that the initial response status is a valid one. | |
120 if (!ResponseStatusSuccess(loader_)) | |
121 return false; | |
122 | |
105 std::string response_headers; | 123 std::string response_headers; |
106 if (!headers.empty()) { | 124 if (!headers.empty()) { |
107 response_headers = headers; | 125 response_headers = headers; |
108 } else { | 126 } else { |
109 pp::URLResponseInfo response = loader_.GetResponseInfo(); | 127 pp::URLResponseInfo response = loader_.GetResponseInfo(); |
110 pp::Var headers_var = response.GetHeaders(); | 128 pp::Var headers_var = response.GetHeaders(); |
111 | 129 |
112 if (headers_var.is_string()) { | 130 if (headers_var.is_string()) { |
113 response_headers = headers_var.AsString(); | 131 response_headers = headers_var.AsString(); |
114 } | 132 } |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
351 | 369 |
352 return request; | 370 return request; |
353 } | 371 } |
354 | 372 |
355 void DocumentLoader::DidOpen(int32_t result) { | 373 void DocumentLoader::DidOpen(int32_t result) { |
356 if (result != PP_OK) { | 374 if (result != PP_OK) { |
357 NOTREACHED(); | 375 NOTREACHED(); |
358 return; | 376 return; |
359 } | 377 } |
360 | 378 |
361 int32_t http_code = loader_.GetResponseInfo().GetStatusCode(); | 379 if (!ResponseStatusSuccess(loader_)) { |
362 if (http_code >= 400 && http_code < 500) { | 380 client_->DocumentLoadFailed(); |
363 // Error accessing resource. 4xx error indicate subsequent requests | |
364 // will fail too. | |
365 // E.g. resource has been removed from the server while loading it. | |
366 // https://code.google.com/p/chromium/issues/detail?id=414827 | |
367 return; | 381 return; |
368 } | 382 } |
369 | 383 |
370 is_multipart_ = false; | 384 is_multipart_ = false; |
371 current_chunk_size_ = 0; | 385 current_chunk_size_ = 0; |
372 current_chunk_read_ = 0; | 386 current_chunk_read_ = 0; |
373 | 387 |
374 pp::Var headers_var = loader_.GetResponseInfo().GetHeaders(); | 388 pp::Var headers_var = loader_.GetResponseInfo().GetHeaders(); |
375 std::string headers; | 389 std::string headers; |
376 if (headers_var.is_string()) | 390 if (headers_var.is_string()) |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
537 | 551 |
538 void DocumentLoader::UpdateRendering() { | 552 void DocumentLoader::UpdateRendering() { |
539 if (header_request_) | 553 if (header_request_) |
540 client_->OnPartialDocumentLoaded(); | 554 client_->OnPartialDocumentLoaded(); |
541 else | 555 else |
542 client_->OnPendingRequestComplete(); | 556 client_->OnPendingRequestComplete(); |
543 header_request_ = false; | 557 header_request_ = false; |
544 } | 558 } |
545 | 559 |
546 } // namespace chrome_pdf | 560 } // namespace chrome_pdf |
OLD | NEW |