[email protected] | 4172081 | 2012-04-03 01:34:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #ifndef NET_BASE_MIME_UTIL_H__ |
| 6 | #define NET_BASE_MIME_UTIL_H__ |
| 7 | |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 8 | // This file defines MIME utility functions. All of them assume the MIME type |
| 9 | // to be of the format specified by rfc2045. According to it, MIME types are |
| 10 | // case strongly insensitive except parameter values, which may or may not be |
| 11 | // case sensitive. |
| 12 | // |
| 13 | // These utilities perform a *case-sensitive* matching for parameter values, |
| 14 | // which may produce some false negatives. Except that, matching is |
| 15 | // case-insensitive. |
| 16 | // |
| 17 | // All constants in mime_util.cc must be written in lower case, except parameter |
| 18 | // values, which can be any case. |
| 19 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 20 | #include <stddef.h> |
| 21 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 22 | #include <string> |
[email protected] | e9696d57 | 2009-07-17 23:22:55 | [diff] [blame] | 23 | #include <vector> |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 24 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 26 | #include "net/base/net_export.h" |
[email protected] | bae0ea1 | 2009-02-14 01:20:41 | [diff] [blame] | 27 | |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 28 | namespace net { |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 29 | |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 30 | // Gets the mime type (if any) that is associated with the given file extension. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 31 | // Returns true if a corresponding mime type exists. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 32 | NET_EXPORT bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 33 | std::string* mime_type); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 34 | |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 35 | // Gets the mime type (if any) that is associated with the given file extension. |
[email protected] | f7359a1 | 2011-06-21 00:57:45 | [diff] [blame] | 36 | // Returns true if a corresponding mime type exists. In this method, |
| 37 | // the search for a mime type is constrained to a limited set of |
| 38 | // types known to the net library, the OS/registry is not consulted. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 39 | NET_EXPORT bool GetWellKnownMimeTypeFromExtension( |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 40 | const base::FilePath::StringType& ext, |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 41 | std::string* mime_type); |
[email protected] | f7359a1 | 2011-06-21 00:57:45 | [diff] [blame] | 42 | |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 43 | // Gets the mime type (if any) that is associated with the given file. Returns |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 44 | // true if a corresponding mime type exists. |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 45 | NET_EXPORT bool GetMimeTypeFromFile(const base::FilePath& file_path, |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 46 | std::string* mime_type); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 47 | |
Sergey Ulanov | 77132a2 | 2017-07-21 02:34:05 | [diff] [blame] | 48 | // Gets the preferred extension (if any) associated with the given mime type. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 49 | // Returns true if a corresponding file extension exists. The extension is |
[email protected] | ceac4d2 | 2009-02-06 20:26:16 | [diff] [blame] | 50 | // returned without a prefixed dot, ex "html". |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 51 | NET_EXPORT bool GetPreferredExtensionForMimeType( |
| 52 | const std::string& mime_type, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 53 | base::FilePath::StringType* extension); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 54 | |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 55 | // Returns true if this the mime_type_pattern matches a given mime-type. |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 56 | // Checks for absolute matching and wildcards. MIME types are case insensitive. |
[email protected] | 4172081 | 2012-04-03 01:34:14 | [diff] [blame] | 57 | NET_EXPORT bool MatchesMimeType(const std::string& mime_type_pattern, |
| 58 | const std::string& mime_type); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 59 | |
[email protected] | 5a665643 | 2014-05-21 23:52:08 | [diff] [blame] | 60 | // Returns true if the |type_string| is a correctly-formed mime type specifier |
| 61 | // with no parameter, i.e. string that matches the following ABNF (see the |
| 62 | // definition of content ABNF in RFC2045 and media-type ABNF httpbis p2 |
| 63 | // semantics). |
| 64 | // |
| 65 | // token "/" token |
| 66 | // |
| 67 | // If |top_level_type| is non-NULL, sets it to parsed top-level type string. |
| 68 | // If |subtype| is non-NULL, sets it to parsed subtype string. |
| 69 | NET_EXPORT bool ParseMimeTypeWithoutParameter(const std::string& type_string, |
| 70 | std::string* top_level_type, |
| 71 | std::string* subtype); |
| 72 | |
| 73 | // Returns true if the |type_string| is a top-level type of any media type |
| 74 | // registered with IANA media types registry at |
| 75 | // http://www.iana.org/assignments/media-types/media-types.xhtml or an |
| 76 | // experimental type (type with x- prefix). |
| 77 | // |
| 78 | // This method doesn't check that the input conforms to token ABNF, so if input |
| 79 | // is experimental type strings, you need to check check that before using |
| 80 | // this method. |
| 81 | NET_EXPORT bool IsValidTopLevelMimeType(const std::string& type_string); |
[email protected] | 0b99ae5 | 2012-06-06 00:05:59 | [diff] [blame] | 82 | |
mtomasz | 7b54be1 | 2015-02-16 06:59:53 | [diff] [blame] | 83 | // Get the extensions associated with the given mime type. There could be |
| 84 | // multiple extensions for a given mime type, like "html,htm" for "text/html", |
| 85 | // or "txt,text,html,..." for "text/*". |
[email protected] | 09994913 | 2010-09-08 20:24:59 | [diff] [blame] | 86 | // Note that we do not erase the existing elements in the the provided vector. |
| 87 | // Instead, we append the result to it. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 88 | NET_EXPORT void GetExtensionsForMimeType( |
[email protected] | 13677b8 | 2011-05-18 18:29:36 | [diff] [blame] | 89 | const std::string& mime_type, |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame] | 90 | std::vector<base::FilePath::StringType>* extensions); |
[email protected] | 09994913 | 2010-09-08 20:24:59 | [diff] [blame] | 91 | |
lukasza | c644f371 | 2016-01-08 19:35:04 | [diff] [blame] | 92 | // Generates a random MIME multipart boundary. |
| 93 | // The returned string is guaranteed to be at most 70 characters long. |
| 94 | NET_EXPORT std::string GenerateMimeMultipartBoundary(); |
| 95 | |
[email protected] | 811d279 | 2013-05-16 12:59:18 | [diff] [blame] | 96 | // Prepares one value as part of a multi-part upload request. |
| 97 | NET_EXPORT void AddMultipartValueForUpload(const std::string& value_name, |
| 98 | const std::string& value, |
| 99 | const std::string& mime_boundary, |
| 100 | const std::string& content_type, |
| 101 | std::string* post_data); |
| 102 | |
| 103 | // Adds the final delimiter to a multi-part upload request. |
| 104 | NET_EXPORT void AddMultipartFinalDelimiterForUpload( |
| 105 | const std::string& mime_boundary, |
| 106 | std::string* post_data); |
| 107 | |
[email protected] | a9bb6f69 | 2008-07-30 16:40:10 | [diff] [blame] | 108 | } // namespace net |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 109 | |
| 110 | #endif // NET_BASE_MIME_UTIL_H__ |