Resource.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2023, Kemal Zebari <kemalzebra@gmail.com>.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibWeb/MimeSniff/MimeType.h>
  8. namespace Web::MimeSniff {
  9. enum class SniffingContext {
  10. None,
  11. Browsing,
  12. Image,
  13. AudioOrVideo,
  14. Font,
  15. // Non-standard but used in cases where the spec expects us to only run
  16. // https://mimesniff.spec.whatwg.org/#sniffing-a-mislabeled-binary-resource
  17. TextOrBinary,
  18. };
  19. struct SniffingConfiguration {
  20. SniffingContext sniffing_context { SniffingContext::None };
  21. StringView scheme { ""sv };
  22. Optional<MimeType> supplied_type = {};
  23. bool no_sniff { false };
  24. };
  25. // https://mimesniff.spec.whatwg.org/#resource
  26. class Resource {
  27. public:
  28. static ErrorOr<Resource> create(ReadonlyBytes data, SniffingConfiguration configuration = {});
  29. static ErrorOr<MimeType> sniff(ReadonlyBytes data, SniffingConfiguration configuration = {});
  30. ~Resource();
  31. MimeType const& computed_mime_type() const { return m_computed_mime_type; }
  32. ReadonlyBytes resource_header() const { return m_resource_header; }
  33. private:
  34. Resource(ReadonlyBytes data, bool no_sniff, MimeType&& default_computed_mime_type);
  35. void read_the_resource_header(ReadonlyBytes data);
  36. ErrorOr<void> supplied_mime_type_detection_algorithm(StringView scheme, Optional<MimeType> supplied_type);
  37. ErrorOr<void> mime_type_sniffing_algorithm();
  38. ErrorOr<void> rules_for_distinguishing_if_a_resource_is_text_or_binary();
  39. ErrorOr<void> context_specific_sniffing_algorithm(SniffingContext sniffing_context);
  40. ErrorOr<void> rules_for_sniffing_images_specifically();
  41. ErrorOr<void> rules_for_sniffing_audio_or_video_specifically();
  42. ErrorOr<void> rules_for_sniffing_fonts_specifically();
  43. // https://mimesniff.spec.whatwg.org/#supplied-mime-type
  44. // A supplied MIME type, the MIME type determined by the supplied MIME type detection algorithm.
  45. Optional<MimeType> m_supplied_mime_type;
  46. // https://mimesniff.spec.whatwg.org/#check-for-apache-bug-flag
  47. // A check-for-apache-bug flag, which defaults to unset.
  48. bool m_check_for_apache_bug_flag { false };
  49. // https://mimesniff.spec.whatwg.org/#no-sniff-flag
  50. // A no-sniff flag, which defaults to set if the user agent does not wish to perform sniffing on the resource and unset otherwise.
  51. bool m_no_sniff { false };
  52. // https://mimesniff.spec.whatwg.org/#computed-mime-type
  53. // A computed MIME type, the MIME type determined by the MIME type sniffing algorithm.
  54. MimeType m_computed_mime_type;
  55. // https://mimesniff.spec.whatwg.org/#resource-header
  56. // A resource header is the byte sequence at the beginning of a resource, as determined by reading the resource header.
  57. ByteBuffer m_resource_header;
  58. };
  59. }