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