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