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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "net/http/http_util.h" | 9 #include "net/http/http_util.h" |
10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 break; | 60 break; |
61 } | 61 } |
62 | 62 |
63 return std::string(boundary + 9); | 63 return std::string(boundary + 9); |
64 } | 64 } |
65 } | 65 } |
66 } | 66 } |
67 return std::string(); | 67 return std::string(); |
68 } | 68 } |
69 | 69 |
| 70 bool IsValidContentType(const std::string& type) { |
| 71 return (EndsWith(type, "/pdf", false) || |
| 72 EndsWith(type, ".pdf", false) || |
| 73 EndsWith(type, "/x-pdf", false) || |
| 74 EndsWith(type, "/*", false) || |
| 75 EndsWith(type, "/acrobat", false) || |
| 76 EndsWith(type, "/unknown", false)); |
| 77 } |
| 78 |
70 } // namespace | 79 } // namespace |
71 | 80 |
72 DocumentLoader::Client::~Client() { | 81 DocumentLoader::Client::~Client() { |
73 } | 82 } |
74 | 83 |
75 DocumentLoader::DocumentLoader(Client* client) | 84 DocumentLoader::DocumentLoader(Client* client) |
76 : client_(client), partial_document_(false), request_pending_(false), | 85 : client_(client), partial_document_(false), request_pending_(false), |
77 current_pos_(0), current_chunk_size_(0), current_chunk_read_(0), | 86 current_pos_(0), current_chunk_size_(0), current_chunk_read_(0), |
78 document_size_(0), header_request_(true), is_multipart_(false) { | 87 document_size_(0), header_request_(true), is_multipart_(false) { |
79 loader_factory_.Initialize(this); | 88 loader_factory_.Initialize(this); |
(...skipping 19 matching lines...) Expand all Loading... |
99 if (headers_var.is_string()) { | 108 if (headers_var.is_string()) { |
100 response_headers = headers_var.AsString(); | 109 response_headers = headers_var.AsString(); |
101 } | 110 } |
102 } | 111 } |
103 | 112 |
104 bool accept_ranges_bytes = false; | 113 bool accept_ranges_bytes = false; |
105 bool content_encoded = false; | 114 bool content_encoded = false; |
106 uint32_t content_length = 0; | 115 uint32_t content_length = 0; |
107 std::string type; | 116 std::string type; |
108 std::string disposition; | 117 std::string disposition; |
109 if (!response_headers.empty()) { | 118 |
| 119 // This happens for PDFs not loaded from http(s) sources. |
| 120 if (response_headers == "Content-Type: text/plain") { |
| 121 if (!StartsWithASCII(url, "http://", false) && |
| 122 !StartsWithASCII(url, "https://", false)) { |
| 123 type = "application/pdf"; |
| 124 } |
| 125 } |
| 126 if (type.empty() && !response_headers.empty()) { |
110 net::HttpUtil::HeadersIterator it(response_headers.begin(), | 127 net::HttpUtil::HeadersIterator it(response_headers.begin(), |
111 response_headers.end(), "\n"); | 128 response_headers.end(), "\n"); |
112 while (it.GetNext()) { | 129 while (it.GetNext()) { |
113 if (LowerCaseEqualsASCII(it.name(), "content-length")) { | 130 if (LowerCaseEqualsASCII(it.name(), "content-length")) { |
114 content_length = atoi(it.values().c_str()); | 131 content_length = atoi(it.values().c_str()); |
115 } else if (LowerCaseEqualsASCII(it.name(), "accept-ranges")) { | 132 } else if (LowerCaseEqualsASCII(it.name(), "accept-ranges")) { |
116 accept_ranges_bytes = LowerCaseEqualsASCII(it.values(), "bytes"); | 133 accept_ranges_bytes = LowerCaseEqualsASCII(it.values(), "bytes"); |
117 } else if (LowerCaseEqualsASCII(it.name(), "content-encoding")) { | 134 } else if (LowerCaseEqualsASCII(it.name(), "content-encoding")) { |
118 content_encoded = true; | 135 content_encoded = true; |
119 } else if (LowerCaseEqualsASCII(it.name(), "content-type")) { | 136 } else if (LowerCaseEqualsASCII(it.name(), "content-type")) { |
120 type = it.values(); | 137 type = it.values(); |
121 size_t semi_colon_pos = type.find(';'); | 138 size_t semi_colon_pos = type.find(';'); |
122 if (semi_colon_pos != std::string::npos) { | 139 if (semi_colon_pos != std::string::npos) { |
123 type = type.substr(0, semi_colon_pos); | 140 type = type.substr(0, semi_colon_pos); |
124 } | 141 } |
125 TrimWhitespace(type, base::TRIM_ALL, &type); | 142 TrimWhitespace(type, base::TRIM_ALL, &type); |
126 } else if (LowerCaseEqualsASCII(it.name(), "content-disposition")) { | 143 } else if (LowerCaseEqualsASCII(it.name(), "content-disposition")) { |
127 disposition = it.values(); | 144 disposition = it.values(); |
128 } | 145 } |
129 } | 146 } |
130 } | 147 } |
131 if (!type.empty() && | 148 if (!type.empty() && !IsValidContentType(type)) |
132 !EndsWith(type, "/pdf", false) && | |
133 !EndsWith(type, ".pdf", false) && | |
134 !EndsWith(type, "/x-pdf", false) && | |
135 !EndsWith(type, "/*", false) && | |
136 !EndsWith(type, "/acrobat", false) && | |
137 !EndsWith(type, "/unknown", false)) { | |
138 return false; | 149 return false; |
139 } | 150 if (StartsWithASCII(disposition, "attachment", false)) |
140 if (StartsWithASCII(disposition, "attachment", false)) { | |
141 return false; | 151 return false; |
142 } | |
143 | 152 |
144 if (content_length > 0) | 153 if (content_length > 0) |
145 chunk_stream_.Preallocate(content_length); | 154 chunk_stream_.Preallocate(content_length); |
146 | 155 |
147 document_size_ = content_length; | 156 document_size_ = content_length; |
148 requests_count_ = 0; | 157 requests_count_ = 0; |
149 | 158 |
150 // Enable partial loading only if file size is above the threshold. | 159 // Enable partial loading only if file size is above the threshold. |
151 // It will allow avoiding latency for multiple requests. | 160 // It will allow avoiding latency for multiple requests. |
152 if (content_length > kMinFileSize && | 161 if (content_length > kMinFileSize && |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 uint32_t DocumentLoader::GetRequestSize() const { | 526 uint32_t DocumentLoader::GetRequestSize() const { |
518 // Document loading strategy: | 527 // Document loading strategy: |
519 // For first 10 requests, we use 32k chunk sizes, for the next 10 requests we | 528 // For first 10 requests, we use 32k chunk sizes, for the next 10 requests we |
520 // double the size (64k), and so on, until we cap max request size at 2M for | 529 // double the size (64k), and so on, until we cap max request size at 2M for |
521 // 71 or more requests. | 530 // 71 or more requests. |
522 uint32_t limited_count = std::min(std::max(requests_count_, 10u), 70u); | 531 uint32_t limited_count = std::min(std::max(requests_count_, 10u), 70u); |
523 return 32 * 1024 * (1 << ((limited_count - 1) / 10u)); | 532 return 32 * 1024 * (1 << ((limited_count - 1) / 10u)); |
524 } | 533 } |
525 | 534 |
526 } // namespace chrome_pdf | 535 } // namespace chrome_pdf |
OLD | NEW |