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