data_url.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (c) 2012 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. // NOTE: based loosely on mozilla's nsDataChannel.cpp
  5. #include <algorithm>
  6. #include "net/base/data_url.h"
  7. #include "base/base64.h"
  8. #include "base/containers/cxx20_erase.h"
  9. #include "base/feature_list.h"
  10. #include "base/features.h"
  11. #include "base/strings/escape.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/strings/string_split.h"
  14. #include "base/strings/string_util.h"
  15. #include "net/base/mime_util.h"
  16. #include "net/http/http_response_headers.h"
  17. #include "net/http/http_util.h"
  18. #include "url/gurl.h"
  19. namespace net {
  20. namespace {
  21. // A data URL is ready for decode if it:
  22. // - Doesn't need any extra padding.
  23. // - Does not have any escaped characters.
  24. // - Does not have any whitespace.
  25. bool IsDataURLReadyForDecode(base::StringPiece body) {
  26. return (body.length() % 4) == 0 && base::ranges::find_if(body, [](char c) {
  27. return c == '%' ||
  28. base::IsAsciiWhitespace(c);
  29. }) == std::end(body);
  30. }
  31. } // namespace
  32. bool DataURL::Parse(const GURL& url,
  33. std::string* mime_type,
  34. std::string* charset,
  35. std::string* data) {
  36. if (!url.is_valid() || !url.has_scheme())
  37. return false;
  38. DCHECK(mime_type->empty());
  39. DCHECK(charset->empty());
  40. DCHECK(!data || data->empty());
  41. base::StringPiece content;
  42. std::string content_string;
  43. if (base::FeatureList::IsEnabled(base::features::kOptimizeDataUrls)) {
  44. // Avoid copying the URL content which can be expensive for large URLs.
  45. content = url.GetContentPiece();
  46. } else {
  47. content_string = url.GetContent();
  48. content = content_string;
  49. }
  50. base::StringPiece::const_iterator begin = content.begin();
  51. base::StringPiece::const_iterator end = content.end();
  52. base::StringPiece::const_iterator comma = std::find(begin, end, ',');
  53. if (comma == end)
  54. return false;
  55. std::vector<base::StringPiece> meta_data =
  56. base::SplitStringPiece(base::MakeStringPiece(begin, comma), ";",
  57. base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  58. // These are moved to |mime_type| and |charset| on success.
  59. std::string mime_type_value;
  60. std::string charset_value;
  61. auto iter = meta_data.cbegin();
  62. if (iter != meta_data.cend()) {
  63. mime_type_value = base::ToLowerASCII(*iter);
  64. ++iter;
  65. }
  66. static constexpr base::StringPiece kBase64Tag("base64");
  67. static constexpr base::StringPiece kCharsetTag("charset=");
  68. bool base64_encoded = false;
  69. for (; iter != meta_data.cend(); ++iter) {
  70. if (!base64_encoded &&
  71. base::EqualsCaseInsensitiveASCII(*iter, kBase64Tag)) {
  72. base64_encoded = true;
  73. } else if (charset_value.empty() &&
  74. base::StartsWith(*iter, kCharsetTag,
  75. base::CompareCase::INSENSITIVE_ASCII)) {
  76. charset_value = std::string(iter->substr(kCharsetTag.size()));
  77. // The grammar for charset is not specially defined in RFC2045 and
  78. // RFC2397. It just needs to be a token.
  79. if (!HttpUtil::IsToken(charset_value))
  80. return false;
  81. }
  82. }
  83. if (mime_type_value.empty()) {
  84. // Fallback to the default if nothing specified in the mediatype part as
  85. // specified in RFC2045. As specified in RFC2397, we use |charset| even if
  86. // |mime_type| is empty.
  87. mime_type_value = "text/plain";
  88. if (charset_value.empty())
  89. charset_value = "US-ASCII";
  90. } else if (!ParseMimeTypeWithoutParameter(mime_type_value, nullptr,
  91. nullptr)) {
  92. // Fallback to the default as recommended in RFC2045 when the mediatype
  93. // value is invalid. For this case, we don't respect |charset| but force it
  94. // set to "US-ASCII".
  95. mime_type_value = "text/plain";
  96. charset_value = "US-ASCII";
  97. }
  98. // The caller may not be interested in receiving the data.
  99. if (data) {
  100. // Preserve spaces if dealing with text or xml input, same as mozilla:
  101. // https://bugzilla.mozilla.org/show_bug.cgi?id=138052
  102. // but strip them otherwise:
  103. // https://bugzilla.mozilla.org/show_bug.cgi?id=37200
  104. // (Spaces in a data URL should be escaped, which is handled below, so any
  105. // spaces now are wrong. People expect to be able to enter them in the URL
  106. // bar for text, and it can't hurt, so we allow it.)
  107. //
  108. // TODO(mmenke): Is removing all spaces reasonable? GURL removes trailing
  109. // spaces itself, anyways. Should we just trim leading spaces instead?
  110. // Allowing random intermediary spaces seems unnecessary.
  111. auto raw_body = base::MakeStringPiece(comma + 1, end);
  112. // For base64, we may have url-escaped whitespace which is not part
  113. // of the data, and should be stripped. Otherwise, the escaped whitespace
  114. // could be part of the payload, so don't strip it.
  115. if (base64_encoded) {
  116. // If the data URL is well formed, we can decode it immediately.
  117. if (base::FeatureList::IsEnabled(base::features::kOptimizeDataUrls) &&
  118. IsDataURLReadyForDecode(raw_body)) {
  119. if (!base::Base64Decode(raw_body, data))
  120. return false;
  121. } else {
  122. std::string unescaped_body = base::UnescapeBinaryURLComponent(raw_body);
  123. // Strip spaces, which aren't allowed in Base64 encoding.
  124. base::EraseIf(unescaped_body, base::IsAsciiWhitespace<char>);
  125. size_t length = unescaped_body.length();
  126. size_t padding_needed = 4 - (length % 4);
  127. // If the input wasn't padded, then we pad it as necessary until we have
  128. // a length that is a multiple of 4 as required by our decoder. We don't
  129. // correct if the input was incorrectly padded. If |padding_needed| ==
  130. // 3, then the input isn't well formed and decoding will fail with or
  131. // without padding.
  132. if ((padding_needed == 1 || padding_needed == 2) &&
  133. unescaped_body[length - 1] != '=') {
  134. unescaped_body.resize(length + padding_needed, '=');
  135. }
  136. if (!base::Base64Decode(unescaped_body, data))
  137. return false;
  138. }
  139. } else {
  140. // Strip whitespace for non-text MIME types.
  141. std::string temp;
  142. if (!(mime_type_value.compare(0, 5, "text/") == 0 ||
  143. mime_type_value.find("xml") != std::string::npos)) {
  144. temp = std::string(raw_body);
  145. base::EraseIf(temp, base::IsAsciiWhitespace<char>);
  146. raw_body = temp;
  147. }
  148. *data = base::UnescapeBinaryURLComponent(raw_body);
  149. }
  150. }
  151. *mime_type = std::move(mime_type_value);
  152. *charset = std::move(charset_value);
  153. return true;
  154. }
  155. Error DataURL::BuildResponse(const GURL& url,
  156. base::StringPiece method,
  157. std::string* mime_type,
  158. std::string* charset,
  159. std::string* data,
  160. scoped_refptr<HttpResponseHeaders>* headers) {
  161. DCHECK(data);
  162. DCHECK(!*headers);
  163. if (!DataURL::Parse(url, mime_type, charset, data))
  164. return ERR_INVALID_URL;
  165. // |mime_type| set by DataURL::Parse() is guaranteed to be in
  166. // token "/" token
  167. // form. |charset| can be an empty string.
  168. DCHECK(!mime_type->empty());
  169. // "charset" in the Content-Type header is specified explicitly to follow
  170. // the "token" ABNF in the HTTP spec. When the DataURL::Parse() call is
  171. // successful, it's guaranteed that the string in |charset| follows the
  172. // "token" ABNF.
  173. std::string content_type = *mime_type;
  174. if (!charset->empty())
  175. content_type.append(";charset=" + *charset);
  176. // The terminal double CRLF isn't needed by TryToCreate().
  177. *headers = HttpResponseHeaders::TryToCreate(
  178. "HTTP/1.1 200 OK\r\n"
  179. "Content-Type:" +
  180. content_type);
  181. // Above line should always succeed - TryToCreate() only fails when there are
  182. // nulls in the string, and DataURL::Parse() can't return nulls in anything
  183. // but the |data| argument.
  184. DCHECK(*headers);
  185. if (base::EqualsCaseInsensitiveASCII(method, "HEAD"))
  186. data->clear();
  187. return OK;
  188. }
  189. } // namespace net