• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef NET_BASE_MIME_SNIFFER_H__
6 #define NET_BASE_MIME_SNIFFER_H__
7 
8 #include <string>
9 
10 #include "net/base/net_export.h"
11 
12 class GURL;
13 
14 namespace net {
15 
16 // The maximum number of bytes used by any internal mime sniffing routine. May
17 // be useful for callers to determine an efficient buffer size to pass to
18 // |SniffMimeType|.
19 // This must be updated if any internal sniffing routine needs more bytes.
20 const int kMaxBytesToSniff = 1024;
21 
22 // Examine the URL and the mime_type and decide whether we should sniff a
23 // replacement mime type from the content.
24 //
25 // @param url The URL from which we obtained the content.
26 // @param mime_type The current mime type, e.g. from the Content-Type header.
27 // @return Returns true if we should sniff the mime type.
28 NET_EXPORT bool ShouldSniffMimeType(const GURL& url,
29                                     const std::string& mime_type);
30 
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 // @param content A buffer containing the bytes to sniff.
35 // @param content_size The number of bytes in the |content| buffer.
36 // @param url The URL from which we obtained this content.
37 // @param type_hint The current mime type, e.g. from the Content-Type header.
38 // @param result Address at which to place the sniffed mime type.
39 // @return Returns true if we have enough content to guess the mime type.
40 NET_EXPORT bool SniffMimeType(const char* content, size_t content_size,
41                               const GURL& url, const std::string& type_hint,
42                               std::string* result);
43 
44 // Attempt to identify a MIME type from the first few bytes of content only.
45 // Uses a bigger set of media file searches than |SniffMimeType()|.
46 // If finds a match, fills in |result| and returns true,
47 // otherwise returns false.
48 //
49 // The caller should understand the security ramifications of trusting
50 // uncontrolled data before accepting the results of this function.
51 //
52 // @param content A buffer containing the bytes to sniff.
53 // @param content_size The number of bytes in the |content| buffer.
54 // @param result Address at which to place the sniffed mime type.
55 // @return Returns true if a MIME type match was found.
56 NET_EXPORT bool SniffMimeTypeFromLocalData(const char* content,
57                                            size_t content_size,
58                                            std::string* result);
59 
60 }  // namespace net
61 
62 #endif  // NET_BASE_MIME_SNIFFER_H__
63