Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 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 | #include "buffet/mime_utils.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <base/strings/string_util.h> |
| 9 | |
| 10 | #include "buffet/string_utils.h" |
| 11 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 12 | namespace buffet { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 14 | // *************************************************************************** |
| 15 | // ******************************* MIME types ******************************** |
| 16 | // *************************************************************************** |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 17 | const char mime::types::kApplication[] = "application"; |
| 18 | const char mime::types::kAudio[] = "audio"; |
| 19 | const char mime::types::kImage[] = "image"; |
| 20 | const char mime::types::kMessage[] = "message"; |
| 21 | const char mime::types::kMultipart[] = "multipart"; |
| 22 | const char mime::types::kText[] = "text"; |
| 23 | const char mime::types::kVideo[] = "video"; |
| 24 | |
| 25 | const char mime::parameters::kCharset[] = "charset"; |
| 26 | |
| 27 | const char mime::image::kJpeg[] = "image/jpeg"; |
| 28 | const char mime::image::kPng[] = "image/png"; |
| 29 | const char mime::image::kBmp[] = "image/bmp"; |
| 30 | const char mime::image::kTiff[] = "image/tiff"; |
| 31 | const char mime::image::kGif[] = "image/gif"; |
| 32 | |
| 33 | const char mime::text::kPlain[] = "text/plain"; |
| 34 | const char mime::text::kHtml[] = "text/html"; |
| 35 | const char mime::text::kXml[] = "text/xml"; |
| 36 | |
| 37 | const char mime::application::kOctet_stream[] = "application/octet-stream"; |
| 38 | const char mime::application::kJson[] = "application/json"; |
| 39 | const char mime::application::kWwwFormUrlEncoded[] = |
| 40 | "application/x-www-form-urlencoded"; |
| 41 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 42 | // *************************************************************************** |
| 43 | // **************************** Utility Functions **************************** |
| 44 | // *************************************************************************** |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 45 | static std::string EncodeParam(const std::string& param) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 46 | // If the string contains one of "tspecials" characters as |
| 47 | // specified in RFC 1521, enclose it in quotes. |
| 48 | if (param.find_first_of("()<>@,;:\\\"/[]?=") != std::string::npos) { |
| 49 | return '"' + param + '"'; |
| 50 | } |
| 51 | return param; |
| 52 | } |
| 53 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 54 | static std::string DecodeParam(const std::string& param) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 55 | if (param.size() > 1 && param.front() == '"' && param.back() == '"') { |
| 56 | return param.substr(1, param.size() - 2); |
| 57 | } |
| 58 | return param; |
| 59 | } |
| 60 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 61 | // *************************************************************************** |
| 62 | // ******************** Main MIME manipulation functions ********************* |
| 63 | // *************************************************************************** |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 64 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 65 | bool mime::Split(const std::string& mime_string, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 66 | std::string* type, std::string* subtype, |
| 67 | mime::Parameters* parameters) { |
| 68 | std::vector<std::string> parts = string_utils::Split(mime_string, ';'); |
| 69 | if (parts.empty()) |
| 70 | return false; |
| 71 | |
| 72 | if (!mime::Split(parts.front(), type, subtype)) |
| 73 | return false; |
| 74 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 75 | if (parameters) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 76 | parameters->clear(); |
| 77 | parameters->reserve(parts.size() - 1); |
| 78 | for (size_t i = 1; i < parts.size(); i++) { |
| 79 | auto pair = string_utils::SplitAtFirst(parts[i], '='); |
| 80 | pair.second = DecodeParam(pair.second); |
| 81 | parameters->push_back(pair); |
| 82 | } |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 87 | bool mime::Split(const std::string& mime_string, |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 88 | std::string* type, std::string* subtype) { |
| 89 | std::string mime = mime::RemoveParameters(mime_string); |
| 90 | auto types = string_utils::SplitAtFirst(mime, '/'); |
| 91 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 92 | if (type) |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 93 | *type = types.first; |
| 94 | |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 95 | if (subtype) |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 96 | *subtype = types.second; |
| 97 | |
| 98 | return !types.first.empty() && !types.second.empty(); |
| 99 | } |
| 100 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 101 | std::string mime::Combine(const std::string& type, const std::string& subtype, |
| 102 | const mime::Parameters& parameters) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 103 | std::vector<std::string> parts; |
| 104 | parts.push_back(string_utils::Join('/', type, subtype)); |
Alex Vakulenko | a0424dd | 2014-06-13 16:10:17 -0700 | [diff] [blame] | 105 | for (const auto& pair : parameters) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 106 | parts.push_back(string_utils::Join('=', pair.first, |
| 107 | EncodeParam(pair.second))); |
| 108 | } |
| 109 | return string_utils::Join("; ", parts); |
| 110 | } |
| 111 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 112 | std::string mime::GetType(const std::string& mime_string) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 113 | std::string mime = mime::RemoveParameters(mime_string); |
| 114 | return string_utils::SplitAtFirst(mime, '/').first; |
| 115 | } |
| 116 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 117 | std::string mime::GetSubtype(const std::string& mime_string) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 118 | std::string mime = mime::RemoveParameters(mime_string); |
| 119 | return string_utils::SplitAtFirst(mime, '/').second; |
| 120 | } |
| 121 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 122 | mime::Parameters mime::GetParameters(const std::string& mime_string) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 123 | std::string type; |
| 124 | std::string subtype; |
| 125 | mime::Parameters parameters; |
| 126 | |
| 127 | if (mime::Split(mime_string, &type, &subtype, ¶meters)) |
| 128 | return std::move(parameters); |
| 129 | |
| 130 | return mime::Parameters(); |
| 131 | } |
| 132 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 133 | std::string mime::RemoveParameters(const std::string& mime_string) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 134 | return string_utils::SplitAtFirst(mime_string, ';').first; |
| 135 | } |
| 136 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 137 | std::string mime::AppendParameter(const std::string& mime_string, |
| 138 | const std::string& paramName, |
| 139 | const std::string& paramValue) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 140 | std::string mime(mime_string); |
| 141 | mime += "; "; |
| 142 | mime += string_utils::Join('=', paramName, EncodeParam(paramValue)); |
| 143 | return mime; |
| 144 | } |
| 145 | |
Alex Vakulenko | b8ba595 | 2014-04-17 11:35:56 -0700 | [diff] [blame] | 146 | std::string mime::GetParameterValue(const std::string& mime_string, |
| 147 | const std::string& paramName) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 148 | mime::Parameters params = mime::GetParameters(mime_string); |
Alex Vakulenko | a0424dd | 2014-06-13 16:10:17 -0700 | [diff] [blame] | 149 | for (const auto& pair : params) { |
Chris Sosa | 45d9f10 | 2014-03-24 11:18:54 -0700 | [diff] [blame] | 150 | if (base::strcasecmp(pair.first.c_str(), paramName.c_str()) == 0) |
| 151 | return pair.second; |
| 152 | } |
| 153 | return std::string(); |
| 154 | } |
Alex Vakulenko | af23b32 | 2014-05-08 16:25:45 -0700 | [diff] [blame] | 155 | |
| 156 | } // namespace buffet |