1 // Copyright 2012 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_UTIL_H__ 6 #define NET_BASE_MIME_UTIL_H__ 7 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 20 #include <stddef.h> 21 22 #include <optional> 23 #include <string> 24 #include <string_view> 25 #include <vector> 26 27 #include "base/files/file_path.h" 28 #include "base/strings/string_split.h" 29 #include "net/base/net_export.h" 30 31 namespace net { 32 33 // Gets the mime type (if any) that is associated with the given file extension. 34 // Returns true if a corresponding mime type exists. 35 NET_EXPORT bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext, 36 std::string* mime_type); 37 38 // Gets the mime type (if any) that is associated with the given file extension. 39 // Returns true if a corresponding mime type exists. In this method, 40 // the search for a mime type is constrained to a limited set of 41 // types known to the net library, the OS/registry is not consulted. 42 NET_EXPORT bool GetWellKnownMimeTypeFromExtension( 43 const base::FilePath::StringType& ext, 44 std::string* mime_type); 45 46 // Gets the mime type (if any) that is associated with the given file. Returns 47 // true if a corresponding mime type exists. 48 NET_EXPORT bool GetMimeTypeFromFile(const base::FilePath& file_path, 49 std::string* mime_type); 50 51 // Gets the preferred extension (if any) associated with the given mime type. 52 // Returns true if a corresponding file extension exists. The extension is 53 // returned without a prefixed dot, ex "html". 54 NET_EXPORT bool GetPreferredExtensionForMimeType( 55 const std::string& mime_type, 56 base::FilePath::StringType* extension); 57 58 // Returns true if this the mime_type_pattern matches a given mime-type. 59 // Checks for absolute matching and wildcards. MIME types are case insensitive. 60 NET_EXPORT bool MatchesMimeType(const std::string& mime_type_pattern, 61 const std::string& mime_type); 62 63 // Parses |type_str| for |mime_type| and any |params|. Returns false if mime 64 // cannot be parsed, and does not modify |mime_type| or |params|. 65 // 66 // Returns true when mime can be parsed and: 67 // If |mime_type| is non-NULL, sets it to parsed mime string. 68 // If |params| is non-NULL, clears it and sets it with name-value pairs of 69 // parsed parameters. Parsing of parameters is lenient, and invalid params are 70 // ignored. 71 NET_EXPORT bool ParseMimeType(const std::string& type_str, 72 std::string* mime_type, 73 base::StringPairs* params); 74 75 // Returns true if the |type_string| is a correctly-formed mime type specifier 76 // with no parameter, i.e. string that matches the following ABNF (see the 77 // definition of content ABNF in RFC2045 and media-type ABNF httpbis p2 78 // semantics). 79 // 80 // token "/" token 81 // 82 // If |top_level_type| is non-NULL, sets it to parsed top-level type string. 83 // If |subtype| is non-NULL, sets it to parsed subtype string. 84 // 85 // This function strips leading and trailing whitespace from the MIME type. 86 // TODO: investigate if we should strip strictly HTTP whitespace. 87 NET_EXPORT bool ParseMimeTypeWithoutParameter(std::string_view type_string, 88 std::string* top_level_type, 89 std::string* subtype); 90 91 // Returns `std::optional` with value containing the extracted `type/sub_type` 92 // if `type_string` is a correctly-formed mime type specifier. Returns optional 93 // with empty otherwise. 94 // Set `accept_comma_separated` to accept a type_string like "text/html, 95 // text/xml". This behavior was inherited from Blink's 96 // platform/network/http_parsers. A string such as "text/html, text/xml" is 97 // possible when the response has multiple Content-Type headers. For instance: 98 // Content-Type: text/html 99 // Content-Type: text/xml 100 // becomes: text/html, text/xml 101 // 102 // While RFC 2616 does not allow it, other browsers allow multiple values in 103 // the HTTP media type header field, Content-Type. In such cases, the media 104 // type passed here may contain the multiple values separated by commas. 105 NET_EXPORT std::optional<std::string> ExtractMimeTypeFromMediaType( 106 const std::string& type_string, 107 bool accept_comma_separated); 108 109 // Returns true if the |type_string| is a top-level type of any media type 110 // registered with IANA media types registry at 111 // http://www.iana.org/assignments/media-types/media-types.xhtml or an 112 // experimental type (type with x- prefix). 113 // 114 // This method doesn't check that the input conforms to token ABNF, so if input 115 // is experimental type strings, you need to check check that before using 116 // this method. 117 NET_EXPORT bool IsValidTopLevelMimeType(const std::string& type_string); 118 119 // Get the extensions associated with the given mime type. 120 // 121 // There could be multiple extensions for a given mime type, like "html,htm" for 122 // "text/html", or "txt,text,html,..." for "text/*". Note that we do not erase 123 // the existing elements in the the provided vector. Instead, we append the 124 // result to it. The new extensions are returned in no particular order. 125 NET_EXPORT void GetExtensionsForMimeType( 126 const std::string& mime_type, 127 std::vector<base::FilePath::StringType>* extensions); 128 129 // Generates a random MIME multipart boundary. 130 // The returned string is guaranteed to be at most 70 characters long. 131 NET_EXPORT std::string GenerateMimeMultipartBoundary(); 132 133 // Prepares one value as part of a multi-part upload request. 134 NET_EXPORT void AddMultipartValueForUpload(const std::string& value_name, 135 const std::string& value, 136 const std::string& mime_boundary, 137 const std::string& content_type, 138 std::string* post_data); 139 140 // Prepares one value as part of a multi-part upload request, with file name as 141 // an additional parameter. 142 NET_EXPORT void AddMultipartValueForUploadWithFileName( 143 const std::string& value_name, 144 const std::string& file_name, 145 const std::string& value, 146 const std::string& mime_boundary, 147 const std::string& content_type, 148 std::string* post_data); 149 150 // Adds the final delimiter to a multi-part upload request. 151 NET_EXPORT void AddMultipartFinalDelimiterForUpload( 152 const std::string& mime_boundary, 153 std::string* post_data); 154 155 } // namespace net 156 157 #endif // NET_BASE_MIME_UTIL_H__ 158