URL.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
  4. * Copyright (c) 2023, Karol Kosek <krkk@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Base64.h>
  9. #include <LibURL/URL.h>
  10. #include <LibWeb/Fetch/Infrastructure/URL.h>
  11. #include <LibWeb/MimeSniff/MimeType.h>
  12. namespace Web::Fetch::Infrastructure {
  13. // https://fetch.spec.whatwg.org/#is-local
  14. bool is_local_url(URL::URL const& url)
  15. {
  16. // A URL is local if its scheme is a local scheme.
  17. return any_of(LOCAL_SCHEMES, [&](auto scheme) { return url.scheme() == scheme; });
  18. }
  19. // https://fetch.spec.whatwg.org/#fetch-scheme
  20. bool is_fetch_scheme(StringView scheme)
  21. {
  22. // A fetch scheme is "about", "blob", "data", "file", or an HTTP(S) scheme.
  23. return any_of(FETCH_SCHEMES, [&](auto fetch_scheme) { return scheme == fetch_scheme; });
  24. }
  25. // https://fetch.spec.whatwg.org/#http-scheme
  26. bool is_http_or_https_scheme(StringView scheme)
  27. {
  28. // An HTTP(S) scheme is "http" or "https".
  29. return any_of(HTTP_SCHEMES, [&](auto http_scheme) { return scheme == http_scheme; });
  30. }
  31. // https://fetch.spec.whatwg.org/#data-url-processor
  32. ErrorOr<DataURL> process_data_url(URL::URL const& data_url)
  33. {
  34. // 1. Assert: dataURL’s scheme is "data".
  35. VERIFY(data_url.scheme() == "data");
  36. // 2. Let input be the result of running the URL serializer on dataURL with exclude fragment set to true.
  37. auto input_serialized = data_url.serialize(URL::ExcludeFragment::Yes);
  38. StringView input = input_serialized;
  39. // 3. Remove the leading "data:" from input.
  40. input = input.substring_view("data:"sv.length());
  41. // 4. Let position point at the start of input.
  42. // 5. Let mimeType be the result of collecting a sequence of code points that are not equal to U+002C (,), given position.
  43. auto position = input.find(',');
  44. auto mime_type = input.substring_view(0, position.value_or(input.length()));
  45. // 6. Strip leading and trailing ASCII whitespace from mimeType.
  46. mime_type = mime_type.trim_whitespace(TrimMode::Both);
  47. // 7. If position is past the end of input, then return failure.
  48. if (!position.has_value())
  49. return Error::from_string_literal("Missing a comma character");
  50. // 8. Advance position by 1.
  51. position = position.value() + 1;
  52. // 9. Let encodedBody be the remainder of input.
  53. auto encoded_body = input.substring_view(position.value());
  54. // 10. Let body be the percent-decoding of encodedBody.
  55. auto body = URL::percent_decode(encoded_body).to_byte_buffer();
  56. // 11. If mimeType ends with U+003B (;), followed by zero or more U+0020 SPACE, followed by an ASCII case-insensitive match for "base64", then:
  57. if (mime_type.ends_with("base64"sv, CaseSensitivity::CaseInsensitive)) {
  58. auto trimmed_substring_view = mime_type.substring_view(0, mime_type.length() - 6);
  59. trimmed_substring_view = trimmed_substring_view.trim(" "sv, TrimMode::Right);
  60. if (trimmed_substring_view.ends_with(';')) {
  61. // 1. Let stringBody be the isomorphic decode of body.
  62. auto string_body = StringView(body);
  63. // 2. Set body to the forgiving-base64 decode of stringBody.
  64. // 3. If body is failure, then return failure.
  65. body = TRY(decode_base64(string_body));
  66. // 4. Remove the last 6 code points from mimeType.
  67. // 5. Remove trailing U+0020 SPACE code points from mimeType, if any.
  68. // 6. Remove the last U+003B (;) from mimeType.
  69. mime_type = trimmed_substring_view.substring_view(0, trimmed_substring_view.length() - 1);
  70. }
  71. }
  72. // 12. If mimeType starts with ";", then prepend "text/plain" to mimeType.
  73. StringBuilder builder;
  74. if (mime_type.starts_with(';')) {
  75. builder.append("text/plain"sv);
  76. builder.append(mime_type);
  77. mime_type = builder.string_view();
  78. }
  79. // 13. Let mimeTypeRecord be the result of parsing mimeType.
  80. auto mime_type_record = MimeSniff::MimeType::parse(mime_type);
  81. // 14. If mimeTypeRecord is failure, then set mimeTypeRecord to text/plain;charset=US-ASCII.
  82. if (!mime_type_record.has_value()) {
  83. mime_type_record = MimeSniff::MimeType::create("text"_string, "plain"_string);
  84. mime_type_record->set_parameter("charset"_string, "US-ASCII"_string);
  85. }
  86. // 15. Return a new data: URL struct whose MIME type is mimeTypeRecord and body is body.
  87. return DataURL { mime_type_record.release_value(), body };
  88. }
  89. }