1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "pdf/loader/url_loader_wrapper_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include "base/check_op.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/http/http_util.h"
#include "pdf/loader/result_codes.h"
#include "pdf/loader/url_loader.h"
#include "ui/gfx/range/range.h"
namespace chrome_pdf {
namespace {
// We should read with delay to prevent block UI thread, and reduce CPU usage.
constexpr base::TimeDelta kReadDelayMs = base::Milliseconds(2);
UrlRequest MakeRangeRequest(const std::string& url,
const std::string& referrer_url,
uint32_t position,
uint32_t size) {
UrlRequest request;
request.url = url;
request.method = "GET";
request.ignore_redirects = true;
request.custom_referrer_url = referrer_url;
// According to rfc2616, byte range specifies position of the first and last
// bytes in the requested range inclusively. Therefore we should subtract 1
// from the position + size, to get index of the last byte that needs to be
// downloaded.
request.headers =
base::StringPrintf("Range: bytes=%d-%d", position, position + size - 1);
return request;
}
bool GetByteRangeFromStr(const std::string& content_range_str,
int* start,
int* end) {
std::string range = content_range_str;
if (!base::StartsWith(range, "bytes", base::CompareCase::INSENSITIVE_ASCII))
return false;
range = range.substr(strlen("bytes"));
std::string::size_type pos = range.find('-');
std::string range_end;
if (pos != std::string::npos)
range_end = range.substr(pos + 1);
base::TrimWhitespaceASCII(range, base::TRIM_LEADING, &range);
base::TrimWhitespaceASCII(range_end, base::TRIM_LEADING, &range_end);
*start = atoi(range.c_str());
*end = atoi(range_end.c_str());
return true;
}
// If the headers have a byte-range response, writes the start and end
// positions and returns true if at least the start position was parsed.
// The end position will be set to 0 if it was not found or parsed from the
// response.
// Returns false if not even a start position could be parsed.
bool GetByteRangeFromHeaders(const std::string& headers, int* start, int* end) {
net::HttpUtil::HeadersIterator it(headers, "\n");
while (it.GetNext()) {
if (base::EqualsCaseInsensitiveASCII(it.name_piece(), "content-range")) {
if (GetByteRangeFromStr(it.values().c_str(), start, end))
return true;
}
}
return false;
}
bool IsDoubleEndLineAtEnd(const char* buffer, int size) {
if (size < 2)
return false;
UNSAFE_TODO({
if (buffer[size - 1] == '\n' && buffer[size - 2] == '\n') {
return true;
}
});
if (size < 4) {
return false;
}
UNSAFE_TODO({
return buffer[size - 1] == '\n' && buffer[size - 2] == '\r' &&
buffer[size - 3] == '\n' && buffer[size - 4] == '\r';
});
}
} // namespace
URLLoaderWrapperImpl::URLLoaderWrapperImpl(
std::unique_ptr<UrlLoader> url_loader)
: url_loader_(std::move(url_loader)) {
SetHeadersFromLoader();
}
URLLoaderWrapperImpl::~URLLoaderWrapperImpl() {
Close();
}
int URLLoaderWrapperImpl::GetContentLength() const {
return content_length_;
}
bool URLLoaderWrapperImpl::IsAcceptRangesBytes() const {
return accept_ranges_bytes_;
}
bool URLLoaderWrapperImpl::IsContentEncoded() const {
return content_encoded_;
}
std::string URLLoaderWrapperImpl::GetContentType() const {
return content_type_;
}
std::string URLLoaderWrapperImpl::GetContentDisposition() const {
return content_disposition_;
}
int URLLoaderWrapperImpl::GetStatusCode() const {
return url_loader_->response().status_code;
}
bool URLLoaderWrapperImpl::IsMultipart() const {
return is_multipart_;
}
bool URLLoaderWrapperImpl::GetByteRangeStart(int* start) const {
DCHECK(start);
*start = byte_range_.start();
return byte_range_.IsValid();
}
void URLLoaderWrapperImpl::Close() {
url_loader_->Close();
read_starter_.Stop();
}
void URLLoaderWrapperImpl::OpenRange(const std::string& url,
const std::string& referrer_url,
uint32_t position,
uint32_t size,
base::OnceCallback<void(bool)> callback) {
url_loader_->Open(
MakeRangeRequest(url, referrer_url, position, size),
base::BindOnce(&URLLoaderWrapperImpl::DidOpen, weak_factory_.GetWeakPtr(),
std::move(callback)));
}
void URLLoaderWrapperImpl::ReadResponseBody(
base::span<char> buffer,
base::OnceCallback<void(int)> callback) {
buffer_ = buffer;
read_starter_.Start(
FROM_HERE, kReadDelayMs,
base::BindOnce(&URLLoaderWrapperImpl::ReadResponseBodyImpl,
base::Unretained(this), std::move(callback)));
}
void URLLoaderWrapperImpl::ReadResponseBodyImpl(
base::OnceCallback<void(int)> callback) {
url_loader_->ReadResponseBody(
buffer_, base::BindOnce(&URLLoaderWrapperImpl::DidRead,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void URLLoaderWrapperImpl::ParseHeaders(const std::string& response_headers) {
content_length_ = -1;
accept_ranges_bytes_ = false;
content_encoded_ = false;
content_type_.clear();
content_disposition_.clear();
multipart_boundary_.clear();
byte_range_ = gfx::Range::InvalidRange();
is_multipart_ = false;
if (response_headers.empty())
return;
net::HttpUtil::HeadersIterator it(response_headers, "\n");
while (it.GetNext()) {
std::string_view name = it.name_piece();
if (base::EqualsCaseInsensitiveASCII(name, "content-length")) {
content_length_ = atoi(it.values().c_str());
} else if (base::EqualsCaseInsensitiveASCII(name, "accept-ranges")) {
accept_ranges_bytes_ =
base::EqualsCaseInsensitiveASCII(it.values(), "bytes");
} else if (base::EqualsCaseInsensitiveASCII(name, "content-encoding")) {
content_encoded_ = true;
} else if (base::EqualsCaseInsensitiveASCII(name, "content-type")) {
content_type_ = it.values();
size_t semi_colon_pos = content_type_.find(';');
if (semi_colon_pos != std::string::npos) {
content_type_ = content_type_.substr(0, semi_colon_pos);
}
base::TrimWhitespaceASCII(content_type_, base::TRIM_ALL, &content_type_);
// multipart boundary.
std::string type = base::ToLowerASCII(it.values_piece());
if (base::StartsWith(type, "multipart/", base::CompareCase::SENSITIVE)) {
const char* boundary = strstr(type.c_str(), "boundary=");
DCHECK(boundary);
if (boundary) {
UNSAFE_TODO({ multipart_boundary_ = std::string(boundary + 9); });
is_multipart_ = !multipart_boundary_.empty();
}
}
} else if (base::EqualsCaseInsensitiveASCII(name, "content-disposition")) {
content_disposition_ = it.values();
} else if (base::EqualsCaseInsensitiveASCII(name, "content-range")) {
int start = 0;
int end = 0;
if (GetByteRangeFromStr(it.values().c_str(), &start, &end)) {
byte_range_ = gfx::Range(start, end);
}
}
}
}
void URLLoaderWrapperImpl::DidOpen(base::OnceCallback<void(bool)> callback,
Result result) {
SetHeadersFromLoader();
std::move(callback).Run(result == Result::kSuccess);
}
void URLLoaderWrapperImpl::DidRead(base::OnceCallback<void(int)> callback,
int32_t result) {
if (multi_part_processed_) {
// Reset this flag so we look inside the buffer in calls of DidRead for this
// response only once. Note that this code DOES NOT handle multi part
// responses with more than one part (we don't issue them at the moment, so
// they shouldn't arrive).
is_multipart_ = false;
}
if (result <= 0 || !is_multipart_) {
std::move(callback).Run(result);
return;
}
if (result <= 2) {
// TODO(art-snake): Accumulate data for parse headers.
std::move(callback).Run(result);
return;
}
char* start = buffer_.data();
size_t length = result;
multi_part_processed_ = true;
for (int i = 2; i < result; ++i) {
if (IsDoubleEndLineAtEnd(buffer_.data(), i)) {
int start_pos = 0;
int end_pos = 0;
if (GetByteRangeFromHeaders(std::string(buffer_.data(), i), &start_pos,
&end_pos)) {
byte_range_ = gfx::Range(start_pos, end_pos);
UNSAFE_TODO({ start += i; });
length -= i;
}
break;
}
}
result = length;
if (result == 0) {
// Continue receiving.
return ReadResponseBodyImpl(std::move(callback));
}
DCHECK_GT(result, 0);
memmove(buffer_.data(), start, result);
std::move(callback).Run(result);
}
void URLLoaderWrapperImpl::SetHeadersFromLoader() {
ParseHeaders(url_loader_->response().headers);
}
} // namespace chrome_pdf
|