blob: 53c9c26c9ec5653cc9faa17a7d785465b5073313 [file] [log] [blame]
[email protected]41720812012-04-03 01:34:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit586acc5fe2008-07-26 22:42:524
5#ifndef NET_BASE_MIME_UTIL_H__
6#define NET_BASE_MIME_UTIL_H__
7
mtomasz7b54be12015-02-16 06:59:538// 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 Drissman13fc8932015-12-20 04:40:4620#include <stddef.h>
21
initial.commit586acc5fe2008-07-26 22:42:5222#include <string>
[email protected]e9696d572009-07-17 23:22:5523#include <vector>
initial.commit586acc5fe2008-07-26 22:42:5224
[email protected]57999812013-02-24 05:40:5225#include "base/files/file_path.h"
[email protected]172da1b2011-08-12 15:52:2626#include "net/base/net_export.h"
[email protected]bae0ea12009-02-14 01:20:4127
[email protected]a9bb6f692008-07-30 16:40:1028namespace net {
initial.commit586acc5fe2008-07-26 22:42:5229
Sergey Ulanov77132a22017-07-21 02:34:0530// Gets the mime type (if any) that is associated with the given file extension.
initial.commit586acc5fe2008-07-26 22:42:5231// Returns true if a corresponding mime type exists.
[email protected]6cdfd7f2013-02-08 20:40:1532NET_EXPORT bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
[email protected]172da1b2011-08-12 15:52:2633 std::string* mime_type);
initial.commit586acc5fe2008-07-26 22:42:5234
Sergey Ulanov77132a22017-07-21 02:34:0535// Gets the mime type (if any) that is associated with the given file extension.
[email protected]f7359a12011-06-21 00:57:4536// 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]172da1b2011-08-12 15:52:2639NET_EXPORT bool GetWellKnownMimeTypeFromExtension(
[email protected]6cdfd7f2013-02-08 20:40:1540 const base::FilePath::StringType& ext,
[email protected]172da1b2011-08-12 15:52:2641 std::string* mime_type);
[email protected]f7359a12011-06-21 00:57:4542
Sergey Ulanov77132a22017-07-21 02:34:0543// Gets the mime type (if any) that is associated with the given file. Returns
initial.commit586acc5fe2008-07-26 22:42:5244// true if a corresponding mime type exists.
[email protected]6cdfd7f2013-02-08 20:40:1545NET_EXPORT bool GetMimeTypeFromFile(const base::FilePath& file_path,
[email protected]172da1b2011-08-12 15:52:2646 std::string* mime_type);
initial.commit586acc5fe2008-07-26 22:42:5247
Sergey Ulanov77132a22017-07-21 02:34:0548// Gets the preferred extension (if any) associated with the given mime type.
initial.commit586acc5fe2008-07-26 22:42:5249// Returns true if a corresponding file extension exists. The extension is
[email protected]ceac4d22009-02-06 20:26:1650// returned without a prefixed dot, ex "html".
[email protected]172da1b2011-08-12 15:52:2651NET_EXPORT bool GetPreferredExtensionForMimeType(
52 const std::string& mime_type,
[email protected]6cdfd7f2013-02-08 20:40:1553 base::FilePath::StringType* extension);
initial.commit586acc5fe2008-07-26 22:42:5254
initial.commit586acc5fe2008-07-26 22:42:5255// Returns true if this the mime_type_pattern matches a given mime-type.
mtomasz7b54be12015-02-16 06:59:5356// Checks for absolute matching and wildcards. MIME types are case insensitive.
[email protected]41720812012-04-03 01:34:1457NET_EXPORT bool MatchesMimeType(const std::string& mime_type_pattern,
58 const std::string& mime_type);
initial.commit586acc5fe2008-07-26 22:42:5259
[email protected]5a6656432014-05-21 23:52:0860// 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.
69NET_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.
81NET_EXPORT bool IsValidTopLevelMimeType(const std::string& type_string);
[email protected]0b99ae52012-06-06 00:05:5982
mtomasz7b54be12015-02-16 06:59:5383// 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]099949132010-09-08 20:24:5986// Note that we do not erase the existing elements in the the provided vector.
87// Instead, we append the result to it.
[email protected]172da1b2011-08-12 15:52:2688NET_EXPORT void GetExtensionsForMimeType(
[email protected]13677b82011-05-18 18:29:3689 const std::string& mime_type,
[email protected]6cdfd7f2013-02-08 20:40:1590 std::vector<base::FilePath::StringType>* extensions);
[email protected]099949132010-09-08 20:24:5991
lukaszac644f3712016-01-08 19:35:0492// Generates a random MIME multipart boundary.
93// The returned string is guaranteed to be at most 70 characters long.
94NET_EXPORT std::string GenerateMimeMultipartBoundary();
95
[email protected]811d2792013-05-16 12:59:1896// Prepares one value as part of a multi-part upload request.
97NET_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.
104NET_EXPORT void AddMultipartFinalDelimiterForUpload(
105 const std::string& mime_boundary,
106 std::string* post_data);
107
[email protected]a9bb6f692008-07-30 16:40:10108} // namespace net
initial.commit586acc5fe2008-07-26 22:42:52109
110#endif // NET_BASE_MIME_UTIL_H__