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