mime_sniffer.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2011 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. #ifndef NET_BASE_MIME_SNIFFER_H__
  5. #define NET_BASE_MIME_SNIFFER_H__
  6. #include <stddef.h>
  7. #include <string>
  8. #include "base/strings/string_piece.h"
  9. #include "net/base/net_export.h"
  10. class GURL;
  11. namespace net {
  12. // The maximum number of bytes used by any internal mime sniffing routine. May
  13. // be useful for callers to determine an efficient buffer size to pass to
  14. // |SniffMimeType|.
  15. // This must be updated if any internal sniffing routine needs more bytes.
  16. const int kMaxBytesToSniff = 1024;
  17. // Whether to force the MIME sniffer to sniff the contents of file URLs for
  18. // HTML. kDisabled is recommended.
  19. enum class ForceSniffFileUrlsForHtml {
  20. kDisabled,
  21. kEnabled,
  22. };
  23. // Examine the URL and the mime_type and decide whether to sniff a replacement
  24. // mime type from the content.
  25. //
  26. // |url| is the URL from which the content was obtained.
  27. // |mime_type| is the current mime type, e.g. from the Content-Type header.
  28. // Returns true if the mime type should be sniffed.
  29. NET_EXPORT bool ShouldSniffMimeType(const GURL& url,
  30. base::StringPiece mime_type);
  31. // Guess a mime type from the first few bytes of content an its URL. Always
  32. // assigns |result| with its best guess of a mime type.
  33. //
  34. // |content| contains the bytes to sniff.
  35. // |url| is the URL from which the content was obtained.
  36. // |type_hint| is the current mime type, e.g. from the Content-Type header.
  37. // |result| is the address at which to place the sniffed mime type.
  38. // If |force_sniff_file_url_for_html| is enabled, the contents of |file| URLs
  39. // will be sniffed to see if they contain HTML. It is recommended this be
  40. // disabled.
  41. //
  42. // Returns true if |content| had enough data to guess the mime type. Otherwise,
  43. // |result| will be populated with a putative MIME type, but the method should
  44. // be called again with more of the content.
  45. NET_EXPORT bool SniffMimeType(
  46. base::StringPiece content,
  47. const GURL& url,
  48. const std::string& type_hint,
  49. ForceSniffFileUrlsForHtml force_sniff_file_url_for_html,
  50. std::string* result);
  51. // Attempt to identify a MIME type from the first few bytes of content only.
  52. // Uses a bigger set of media file searches than |SniffMimeType()|.
  53. // If finds a match, fills in |result| and returns true,
  54. // otherwise returns false.
  55. //
  56. // The caller should understand the security ramifications of trusting
  57. // uncontrolled data before accepting the results of this function.
  58. //
  59. // |content| contains the bytes to sniff.
  60. // |result| is address at which to place the sniffed mime type.
  61. // Returns true if a MIME type match was found.
  62. NET_EXPORT bool SniffMimeTypeFromLocalData(base::StringPiece content,
  63. std::string* result);
  64. // Returns true if |content| contains bytes that are control codes that do
  65. // not usually appear in plain text.
  66. NET_EXPORT_PRIVATE bool LooksLikeBinary(base::StringPiece content);
  67. } // namespace net
  68. #endif // NET_BASE_MIME_SNIFFER_H__