/src/crow/include/crow/multipart_view.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include <charconv> |
4 | | #include <string> |
5 | | #include <vector> |
6 | | #include <string_view> |
7 | | #include <sstream> |
8 | | |
9 | | #include "crow/http_request.h" |
10 | | // for crow::multipart::dd |
11 | | #include "crow/multipart.h" |
12 | | #include "crow/ci_map.h" |
13 | | |
14 | | namespace crow |
15 | | { |
16 | | |
17 | | /// Encapsulates anything related to processing and organizing `multipart/xyz` messages |
18 | | namespace multipart |
19 | | { |
20 | | /// The first part in a section, contains metadata about the part |
21 | | struct header_view |
22 | | { |
23 | | std::string_view value; ///< The first part of the header, usually `Content-Type` or `Content-Disposition` |
24 | | std::unordered_map<std::string_view, std::string_view> params; ///< The parameters of the header, come after the `value` |
25 | | |
26 | | /// Returns \ref value as integer |
27 | | operator int() const |
28 | 0 | { |
29 | 0 | int result = 0; |
30 | 0 | std::from_chars(value.data(), value.data() + value.size(), result); |
31 | 0 | return result; |
32 | 0 | } |
33 | | |
34 | | /// Returns \ref value as double |
35 | | operator double() const |
36 | 0 | { |
37 | 0 | // There's no std::from_chars for floating-point types in a lot of STLs |
38 | 0 | return std::stod(static_cast<std::string>(value)); |
39 | 0 | } |
40 | | }; |
41 | | |
42 | | /// Multipart header map (key is header key). |
43 | | using mph_view_map = std::unordered_multimap<std::string_view, header_view, ci_hash, ci_key_eq>; |
44 | | |
45 | | /// Finds and returns the header with the specified key. (returns an empty header if nothing is found) |
46 | | inline const header_view& get_header_object(const mph_view_map& headers, const std::string_view key) |
47 | 25.2k | { |
48 | 25.2k | const auto header = headers.find(key); |
49 | 25.2k | if (header != headers.cend()) |
50 | 22.2k | { |
51 | 22.2k | return header->second; |
52 | 22.2k | } |
53 | | |
54 | 3.05k | static header_view empty; |
55 | 3.05k | return empty; |
56 | 25.2k | } |
57 | | |
58 | | /// String padded with the specified padding (double quotes by default) |
59 | | struct padded |
60 | | { |
61 | | std::string_view value; ///< String to pad |
62 | | static constexpr char padding = '"'; ///< Padding to use |
63 | | |
64 | | /// Outputs padded value to the stream |
65 | | friend std::ostream& operator<<(std::ostream& stream, const padded value_) |
66 | 47.6k | { |
67 | 47.6k | return stream << padding << value_.value << padding; |
68 | 47.6k | } |
69 | | }; |
70 | | |
71 | | ///One part of the multipart message |
72 | | |
73 | | /// |
74 | | /// It is usually separated from other sections by a `boundary` |
75 | | struct part_view |
76 | | { |
77 | | mph_view_map headers; ///< (optional) The first part before the data, Contains information regarding the type of data and encoding |
78 | | std::string_view body; ///< The actual data in the part |
79 | | |
80 | | /// Returns \ref body as integer |
81 | | operator int() const |
82 | 0 | { |
83 | 0 | int result = 0; |
84 | 0 | std::from_chars(body.data(), body.data() + body.size(), result); |
85 | 0 | return result; |
86 | 0 | } |
87 | | |
88 | | /// Returns \ref body as double |
89 | | operator double() const |
90 | 0 | { |
91 | 0 | // There's no std::from_chars for floating-point types in a lot of STLs |
92 | 0 | return std::stod(static_cast<std::string>(body)); |
93 | 0 | } |
94 | | |
95 | | const header_view& get_header_object(const std::string_view key) const |
96 | 0 | { |
97 | 0 | return multipart::get_header_object(headers, key); |
98 | 0 | } |
99 | | |
100 | | friend std::ostream& operator<<(std::ostream& stream, const part_view& part) |
101 | 25.6k | { |
102 | 25.6k | for (const auto& [header_key, header_value] : part.headers) |
103 | 72.6k | { |
104 | 72.6k | stream << header_key << ": " << header_value.value; |
105 | 72.6k | for (const auto& [param_key, param_value] : header_value.params) |
106 | 47.6k | { |
107 | 47.6k | stream << "; " << param_key << '=' << padded{param_value}; |
108 | 47.6k | } |
109 | 72.6k | stream << crlf; |
110 | 72.6k | } |
111 | 25.6k | stream << crlf; |
112 | 25.6k | stream << part.body << crlf; |
113 | 25.6k | return stream; |
114 | 25.6k | } |
115 | | }; |
116 | | |
117 | | /// Multipart map (key is the name parameter). |
118 | | using mp_view_map = std::unordered_multimap<std::string_view, part_view, ci_hash, ci_key_eq>; |
119 | | |
120 | | /// The parsed multipart request/response |
121 | | struct message_view |
122 | | { |
123 | | std::reference_wrapper<const ci_map> headers; ///< The request/response headers |
124 | | std::string boundary; ///< The text boundary that separates different `parts` |
125 | | std::vector<part_view> parts; ///< The individual parts of the message |
126 | | mp_view_map part_map; ///< The individual parts of the message, organized in a map with the `name` header parameter being the key |
127 | | |
128 | | const std::string& get_header_value(const std::string& key) const |
129 | 5.78k | { |
130 | 5.78k | return crow::get_header_value(headers.get(), key); |
131 | 5.78k | } |
132 | | |
133 | | part_view get_part_by_name(const std::string_view name) |
134 | 0 | { |
135 | 0 | auto result = part_map.find(name); |
136 | 0 | if (result != part_map.end()) |
137 | 0 | return result->second; |
138 | 0 | else |
139 | 0 | return {}; |
140 | 0 | } |
141 | | |
142 | | friend std::ostream& operator<<(std::ostream& stream, const message_view& message) |
143 | 2.09k | { |
144 | 2.09k | std::string delimiter = dd + message.boundary; |
145 | | |
146 | 2.09k | for (const part_view& part : message.parts) |
147 | 12.8k | { |
148 | 12.8k | stream << delimiter << crlf; |
149 | 12.8k | stream << part; |
150 | 12.8k | } |
151 | 2.09k | stream << delimiter << dd << crlf; |
152 | | |
153 | 2.09k | return stream; |
154 | 2.09k | } |
155 | | |
156 | | /// Represent all parts as a string (**does not include message headers**) |
157 | | std::string dump() const |
158 | 2.09k | { |
159 | 2.09k | std::ostringstream str; |
160 | 2.09k | str << *this; |
161 | 2.09k | return std::move(str).str(); |
162 | 2.09k | } |
163 | | |
164 | | /// Represent an individual part as a string |
165 | | std::string dump(int part_) const |
166 | 12.8k | { |
167 | 12.8k | std::ostringstream str; |
168 | 12.8k | str << parts.at(part_); |
169 | 12.8k | return std::move(str).str(); |
170 | 12.8k | } |
171 | | |
172 | | /// Create a multipart message from a request data |
173 | | explicit message_view(const request& req): |
174 | 5.78k | headers(req.headers), |
175 | 5.78k | boundary(get_boundary(get_header_value("Content-Type"))) |
176 | 5.78k | { |
177 | 5.78k | if (boundary.empty()) |
178 | 0 | throw bad_request("Empty boundary in multipart message"); |
179 | 5.78k | parse_body(req.body); |
180 | 5.78k | } |
181 | | |
182 | | private: |
183 | | static std::string_view get_boundary(const std::string_view header) |
184 | 0 | { |
185 | 0 | constexpr std::string_view boundary_text = "boundary="; |
186 | 0 | const size_t found = header.find(boundary_text); |
187 | 0 | if (found == std::string_view::npos) |
188 | 0 | { |
189 | 0 | return {}; |
190 | 0 | } |
191 | 0 |
|
192 | 0 | const std::string_view to_return = header.substr(found + boundary_text.size()); |
193 | 0 | if (!to_return.empty() && to_return[0] == '\"') |
194 | 0 | { |
195 | 0 | return to_return.substr(1, to_return.length() - 2); |
196 | 0 | } |
197 | 0 | return to_return; |
198 | 0 | } |
199 | | |
200 | | void parse_body(std::string_view body) |
201 | 5.78k | { |
202 | 5.78k | const std::string delimiter = dd + boundary; |
203 | | |
204 | | // TODO(EDev): Exit on error |
205 | 33.5k | while (body != (crlf)) |
206 | 31.2k | { |
207 | 31.2k | const size_t found = body.find(delimiter); |
208 | 31.2k | if (found == std::string_view::npos) |
209 | 109 | { |
210 | | // did not find delimiter; probably an ill-formed body; ignore the rest |
211 | 109 | throw bad_request("Unable to find multipart delimiter. Probably ill-formed body."); |
212 | 109 | } |
213 | | |
214 | 31.1k | const std::string_view section = body.substr(0, found); |
215 | | |
216 | | // +2 is the CRLF. |
217 | | // We don't check it and delete it so that the same delimiter can be used for The last delimiter (--delimiter--CRLF). |
218 | 31.1k | body = body.substr(found + delimiter.length() + 2); |
219 | 31.1k | if (!section.empty()) |
220 | 25.3k | { |
221 | 25.3k | part_view parsed_section = parse_section(section); |
222 | 25.3k | const auto& section_params = get_header_object(parsed_section.headers, "Content-Disposition").params; |
223 | 25.3k | const auto name_header = section_params.find("name"); |
224 | 25.3k | if (name_header == section_params.end()) |
225 | 3.38k | { |
226 | 3.38k | throw bad_request("Unable to find header 'name' in multipart section. Probably ill-formed body."); |
227 | 3.38k | } |
228 | 21.9k | part_map.emplace(name_header->second, parsed_section); |
229 | 21.9k | parts.push_back(std::move(parsed_section)); |
230 | 21.9k | } |
231 | 31.1k | } |
232 | 5.78k | } |
233 | | |
234 | | [[nodiscard]] static part_view parse_section(std::string_view section) |
235 | 25.3k | { |
236 | 25.3k | const size_t found = section.find(crlfcrlf); |
237 | 25.3k | if (found==std::string_view::npos) |
238 | 45 | { |
239 | 45 | throw bad_request("Section Header end missing in multipart message"); |
240 | 45 | } |
241 | 25.2k | const std::string_view head_line = section.substr(0, found + 2); |
242 | 25.2k | section = section.substr(found + 4); |
243 | | |
244 | 25.2k | return part_view{ |
245 | 25.2k | parse_section_head(head_line), |
246 | 25.2k | section.substr(0, section.length() - 2), |
247 | 25.2k | }; |
248 | 25.3k | } |
249 | | |
250 | | static mph_view_map parse_section_head(std::string_view lines) |
251 | 0 | { |
252 | 0 | mph_view_map result; |
253 | 0 |
|
254 | 0 | while (!lines.empty()) |
255 | 0 | { |
256 | 0 | header_view to_add; |
257 | 0 |
|
258 | 0 | const size_t found_crlf = lines.find(crlf); |
259 | 0 | std::string_view line = lines.substr(0, found_crlf); |
260 | 0 | std::string_view key; |
261 | 0 | lines = lines.substr(found_crlf + 2); |
262 | 0 | // Add the header if available |
263 | 0 | if (!line.empty()) |
264 | 0 | { |
265 | 0 | const size_t found_semicolon = line.find("; "); |
266 | 0 | std::string_view header = line.substr(0, found_semicolon); |
267 | 0 | if (found_semicolon != std::string_view::npos) |
268 | 0 | line = line.substr(found_semicolon + 2); |
269 | 0 | else |
270 | 0 | line = std::string_view(); |
271 | 0 |
|
272 | 0 | const size_t header_split = header.find(": "); |
273 | 0 | key = header.substr(0, header_split); |
274 | 0 |
|
275 | 0 | to_add.value = header.substr(header_split + 2); |
276 | 0 | } |
277 | 0 |
|
278 | 0 | // Add the parameters |
279 | 0 | while (!line.empty()) |
280 | 0 | { |
281 | 0 | const size_t found_semicolon = line.find("; "); |
282 | 0 | std::string_view param = line.substr(0, found_semicolon); |
283 | 0 | if (found_semicolon != std::string_view::npos) |
284 | 0 | line = line.substr(found_semicolon + 2); |
285 | 0 | else |
286 | 0 | line = std::string_view(); |
287 | 0 |
|
288 | 0 | const size_t param_split = param.find('='); |
289 | 0 |
|
290 | 0 | const std::string_view value = param.substr(param_split + 1); |
291 | 0 |
|
292 | 0 | to_add.params.emplace(param.substr(0, param_split), trim(value)); |
293 | 0 | } |
294 | 0 | result.emplace(key, to_add); |
295 | 0 | } |
296 | 0 |
|
297 | 0 | return result; |
298 | 0 | } |
299 | | |
300 | | [[nodiscard]] static std::string_view trim(const std::string_view string, const char excess = '"') |
301 | 58.0k | { |
302 | 58.0k | if (string.length() > 1 && string[0] == excess && string[string.length() - 1] == excess) |
303 | 7.11k | return string.substr(1, string.length() - 2); |
304 | 50.8k | return string; |
305 | 58.0k | } |
306 | | }; |
307 | | } // namespace multipart |
308 | | } // namespace crow |