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 16namespace brillo { 17namespace mime { 18 19namespace types { 20// Main MIME type categories 21BRILLO_EXPORT extern const char kApplication[]; // application 22BRILLO_EXPORT extern const char kAudio[]; // audio 23BRILLO_EXPORT extern const char kImage[]; // image 24BRILLO_EXPORT extern const char kMessage[]; // message 25BRILLO_EXPORT extern const char kMultipart[]; // multipart 26BRILLO_EXPORT extern const char kText[]; // test 27BRILLO_EXPORT extern const char kVideo[]; // video 28} // namespace types 29 30namespace parameters { 31// Common MIME parameters 32BRILLO_EXPORT extern const char kCharset[]; // charset=... 33} // namespace parameters 34 35namespace image { 36// Common image MIME types 37BRILLO_EXPORT extern const char kJpeg[]; // image/jpeg 38BRILLO_EXPORT extern const char kPng[]; // image/png 39BRILLO_EXPORT extern const char kBmp[]; // image/bmp 40BRILLO_EXPORT extern const char kTiff[]; // image/tiff 41BRILLO_EXPORT extern const char kGif[]; // image/gif 42} // namespace image 43 44namespace text { 45// Common text MIME types 46BRILLO_EXPORT extern const char kPlain[]; // text/plain 47BRILLO_EXPORT extern const char kHtml[]; // text/html 48BRILLO_EXPORT extern const char kXml[]; // text/xml 49} // namespace text 50 51namespace application { 52// Common application MIME types 53// application/octet-stream 54BRILLO_EXPORT extern const char kOctet_stream[]; 55// application/json 56BRILLO_EXPORT extern const char kJson[]; 57// application/x-www-form-urlencoded 58BRILLO_EXPORT extern const char kWwwFormUrlEncoded[]; 59// application/x-protobuf 60BRILLO_EXPORT extern const char kProtobuf[]; 61} // namespace application 62 63namespace multipart { 64// Common multipart MIME types 65// multipart/form-data 66BRILLO_EXPORT extern const char kFormData[]; 67// multipart/mixed 68BRILLO_EXPORT extern const char kMixed[]; 69} // namespace multipart 70 71using 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" 76BRILLO_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") 83BRILLO_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"}}) 89BRILLO_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" 96BRILLO_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" 100BRILLO_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"}} 104BRILLO_EXPORT Parameters GetParameters(const std::string& mime_string); 105 106// Removes parameters from a MIME string 107// "text/plain;charset=utf-8" => "text/plain" 108BRILLO_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" 113BRILLO_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" 120BRILLO_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