xref: /aosp_15_r20/external/libbrillo/brillo/mime_utils.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_MIME_UTILS_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_MIME_UTILS_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <string>
9*1a96fba6SXin Li #include <utility>
10*1a96fba6SXin Li #include <vector>
11*1a96fba6SXin Li 
12*1a96fba6SXin Li #include <base/compiler_specific.h>
13*1a96fba6SXin Li #include <base/macros.h>
14*1a96fba6SXin Li #include <brillo/brillo_export.h>
15*1a96fba6SXin Li 
16*1a96fba6SXin Li namespace brillo {
17*1a96fba6SXin Li namespace mime {
18*1a96fba6SXin Li 
19*1a96fba6SXin Li namespace types {
20*1a96fba6SXin Li // Main MIME type categories
21*1a96fba6SXin Li BRILLO_EXPORT extern const char kApplication[];        // application
22*1a96fba6SXin Li BRILLO_EXPORT extern const char kAudio[];              // audio
23*1a96fba6SXin Li BRILLO_EXPORT extern const char kImage[];              // image
24*1a96fba6SXin Li BRILLO_EXPORT extern const char kMessage[];            // message
25*1a96fba6SXin Li BRILLO_EXPORT extern const char kMultipart[];          // multipart
26*1a96fba6SXin Li BRILLO_EXPORT extern const char kText[];               // test
27*1a96fba6SXin Li BRILLO_EXPORT extern const char kVideo[];              // video
28*1a96fba6SXin Li }  // namespace types
29*1a96fba6SXin Li 
30*1a96fba6SXin Li namespace parameters {
31*1a96fba6SXin Li // Common MIME parameters
32*1a96fba6SXin Li BRILLO_EXPORT extern const char kCharset[];            // charset=...
33*1a96fba6SXin Li }  // namespace parameters
34*1a96fba6SXin Li 
35*1a96fba6SXin Li namespace image {
36*1a96fba6SXin Li // Common image MIME types
37*1a96fba6SXin Li BRILLO_EXPORT extern const char kJpeg[];               // image/jpeg
38*1a96fba6SXin Li BRILLO_EXPORT extern const char kPng[];                // image/png
39*1a96fba6SXin Li BRILLO_EXPORT extern const char kBmp[];                // image/bmp
40*1a96fba6SXin Li BRILLO_EXPORT extern const char kTiff[];               // image/tiff
41*1a96fba6SXin Li BRILLO_EXPORT extern const char kGif[];                // image/gif
42*1a96fba6SXin Li }  // namespace image
43*1a96fba6SXin Li 
44*1a96fba6SXin Li namespace text {
45*1a96fba6SXin Li // Common text MIME types
46*1a96fba6SXin Li BRILLO_EXPORT extern const char kPlain[];              // text/plain
47*1a96fba6SXin Li BRILLO_EXPORT extern const char kHtml[];               // text/html
48*1a96fba6SXin Li BRILLO_EXPORT extern const char kXml[];                // text/xml
49*1a96fba6SXin Li }  // namespace text
50*1a96fba6SXin Li 
51*1a96fba6SXin Li namespace application {
52*1a96fba6SXin Li // Common application MIME types
53*1a96fba6SXin Li // application/octet-stream
54*1a96fba6SXin Li BRILLO_EXPORT extern const char kOctet_stream[];
55*1a96fba6SXin Li // application/json
56*1a96fba6SXin Li BRILLO_EXPORT extern const char kJson[];
57*1a96fba6SXin Li // application/x-www-form-urlencoded
58*1a96fba6SXin Li BRILLO_EXPORT extern const char kWwwFormUrlEncoded[];
59*1a96fba6SXin Li // application/x-protobuf
60*1a96fba6SXin Li BRILLO_EXPORT extern const char kProtobuf[];
61*1a96fba6SXin Li }  // namespace application
62*1a96fba6SXin Li 
63*1a96fba6SXin Li namespace multipart {
64*1a96fba6SXin Li // Common multipart MIME types
65*1a96fba6SXin Li // multipart/form-data
66*1a96fba6SXin Li BRILLO_EXPORT extern const char kFormData[];
67*1a96fba6SXin Li // multipart/mixed
68*1a96fba6SXin Li BRILLO_EXPORT extern const char kMixed[];
69*1a96fba6SXin Li }  // namespace multipart
70*1a96fba6SXin Li 
71*1a96fba6SXin Li using Parameters = std::vector<std::pair<std::string, std::string>>;
72*1a96fba6SXin Li 
73*1a96fba6SXin Li // Combine a MIME type, subtype and parameters into a MIME string.
74*1a96fba6SXin Li // e.g. Combine("text", "plain", {{"charset", "utf-8"}}) will give:
75*1a96fba6SXin Li //      "text/plain; charset=utf-8"
76*1a96fba6SXin Li BRILLO_EXPORT std::string Combine(
77*1a96fba6SXin Li     const std::string& type,
78*1a96fba6SXin Li     const std::string& subtype,
79*1a96fba6SXin Li     const Parameters& parameters = {}) WARN_UNUSED_RESULT;
80*1a96fba6SXin Li 
81*1a96fba6SXin Li // Splits a MIME string into type and subtype.
82*1a96fba6SXin Li // "text/plain;charset=utf-8" => ("text", "plain")
83*1a96fba6SXin Li BRILLO_EXPORT bool Split(const std::string& mime_string,
84*1a96fba6SXin Li                          std::string* type,
85*1a96fba6SXin Li                          std::string* subtype);
86*1a96fba6SXin Li 
87*1a96fba6SXin Li // Splits a MIME string into type, subtype, and parameters.
88*1a96fba6SXin Li // "text/plain;charset=utf-8" => ("text", "plain", {{"charset","utf-8"}})
89*1a96fba6SXin Li BRILLO_EXPORT bool Split(const std::string& mime_string,
90*1a96fba6SXin Li                          std::string* type,
91*1a96fba6SXin Li                          std::string* subtype,
92*1a96fba6SXin Li                          Parameters* parameters);
93*1a96fba6SXin Li 
94*1a96fba6SXin Li // Returns the MIME type from MIME string.
95*1a96fba6SXin Li // "text/plain;charset=utf-8" => "text"
96*1a96fba6SXin Li BRILLO_EXPORT std::string GetType(const std::string& mime_string);
97*1a96fba6SXin Li 
98*1a96fba6SXin Li // Returns the MIME sub-type from MIME string.
99*1a96fba6SXin Li // "text/plain;charset=utf-8" => "plain"
100*1a96fba6SXin Li BRILLO_EXPORT std::string GetSubtype(const std::string& mime_string);
101*1a96fba6SXin Li 
102*1a96fba6SXin Li // Returns the MIME parameters from MIME string.
103*1a96fba6SXin Li // "text/plain;charset=utf-8" => {{"charset","utf-8"}}
104*1a96fba6SXin Li BRILLO_EXPORT Parameters GetParameters(const std::string& mime_string);
105*1a96fba6SXin Li 
106*1a96fba6SXin Li // Removes parameters from a MIME string
107*1a96fba6SXin Li // "text/plain;charset=utf-8" => "text/plain"
108*1a96fba6SXin Li BRILLO_EXPORT std::string RemoveParameters(
109*1a96fba6SXin Li     const std::string& mime_string) WARN_UNUSED_RESULT;
110*1a96fba6SXin Li 
111*1a96fba6SXin Li // Appends a parameter to a MIME string.
112*1a96fba6SXin Li // "text/plain" => "text/plain; charset=utf-8"
113*1a96fba6SXin Li BRILLO_EXPORT std::string AppendParameter(
114*1a96fba6SXin Li     const std::string& mime_string,
115*1a96fba6SXin Li     const std::string& paramName,
116*1a96fba6SXin Li     const std::string& paramValue) WARN_UNUSED_RESULT;
117*1a96fba6SXin Li 
118*1a96fba6SXin Li // Returns the value of a parameter on a MIME string (empty string if missing).
119*1a96fba6SXin Li // ("text/plain;charset=utf-8","charset") => "utf-8"
120*1a96fba6SXin Li BRILLO_EXPORT std::string GetParameterValue(const std::string& mime_string,
121*1a96fba6SXin Li                                             const std::string& paramName);
122*1a96fba6SXin Li 
123*1a96fba6SXin Li }  // namespace mime
124*1a96fba6SXin Li }  // namespace brillo
125*1a96fba6SXin Li 
126*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_MIME_UTILS_H_
127