#pragma once #include #include #include #include "crow/http_request.h" #include "crow/returnable.h" namespace crow { ///Encapsulates anything related to processing and organizing `multipart/xyz` messages namespace multipart { const std::string dd = "--"; const std::string crlf = "\r\n"; ///The first part in a section, contains metadata about the part struct header { std::pair value; ///< The first part of the header, usually `Content-Type` or `Content-Disposition` std::unordered_map params; ///< The parameters of the header, come after the `value` }; ///One part of the multipart message ///It is usually separated from other sections by a `boundary` /// struct part { std::vector
headers; ///< (optional) The first part before the data, Contains information regarding the type of data and encoding std::string body; ///< The actual data in the part }; ///The parsed multipart request/response struct message : public returnable { ci_map headers; std::string boundary; ///< The text boundary that separates different `parts` std::vector parts; ///< The individual parts of the message const std::string& get_header_value(const std::string& key) const { return crow::get_header_value(headers, key); } ///Represent all parts as a string (**does not include message headers**) std::string dump() const override { std::stringstream str; std::string delimiter = dd + boundary; for (unsigned i=0 ; i(header.substr(0, header_split), header.substr(header_split+2)); } //add the parameters while (!line.empty()) { size_t found = line.find("; "); std::string param = line.substr(0, found); if (found != std::string::npos) line.erase(0, found+2); else line = std::string(); size_t param_split = param.find('='); std::string value = param.substr(param_split+1); to_add.params.emplace(param.substr(0, param_split), trim(value)); } part.headers.emplace_back(to_add); } } inline std::string trim (std::string& string, const char& excess = '"') const { if (string.length() > 1 && string[0] == excess && string[string.length()-1] == excess) return string.substr(1, string.length()-2); return string; } inline std::string pad (std::string& string, const char& padding = '"') const { return (padding + string + padding); } }; } }