url_loader_wrapper_impl.cc 9.2 KB

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