Code cleanup

This commit is contained in:
yhirose 2019-12-12 21:50:12 -05:00
parent 69a28d50f6
commit 151ccba57e
2 changed files with 13 additions and 17 deletions

View File

@ -211,13 +211,6 @@ using Progress = std::function<bool(uint64_t current, uint64_t total)>;
struct Response; struct Response;
using ResponseHandler = std::function<bool(const Response &response)>; using ResponseHandler = std::function<bool(const Response &response)>;
struct MultipartFile {
std::string filename;
std::string content_type;
std::string content;
};
using MultipartFiles = std::multimap<std::string, MultipartFile>;
struct MultipartFormData { struct MultipartFormData {
std::string name; std::string name;
std::string content; std::string content;
@ -225,12 +218,13 @@ struct MultipartFormData {
std::string content_type; std::string content_type;
}; };
using MultipartFormDataItems = std::vector<MultipartFormData>; using MultipartFormDataItems = std::vector<MultipartFormData>;
using MultipartFormDataMap = std::multimap<std::string, MultipartFormData>;
using ContentReceiver = using ContentReceiver =
std::function<bool(const char *data, size_t data_length)>; std::function<bool(const char *data, size_t data_length)>;
using MultipartContentHeader = using MultipartContentHeader =
std::function<bool(const std::string &name, const MultipartFile &file)>; std::function<bool(const std::string &name, const MultipartFormData &file)>;
using MultipartContentReceiver = using MultipartContentReceiver =
std::function<bool(const std::string& name, const char *data, size_t data_length)>; std::function<bool(const std::string& name, const char *data, size_t data_length)>;
@ -268,7 +262,7 @@ struct Request {
std::string version; std::string version;
std::string target; std::string target;
Params params; Params params;
MultipartFiles files; MultipartFormDataMap files;
Ranges ranges; Ranges ranges;
Match matches; Match matches;
@ -295,7 +289,7 @@ struct Request {
bool is_multipart_form_data() const; bool is_multipart_form_data() const;
bool has_file(const char *key) const; bool has_file(const char *key) const;
MultipartFile get_file_value(const char *key) const; MultipartFormData get_file_value(const char *key) const;
// private members... // private members...
size_t content_length; size_t content_length;
@ -2015,6 +2009,7 @@ public:
case 2: { // Headers case 2: { // Headers
auto pos = buf_.find(crlf_); auto pos = buf_.find(crlf_);
while (pos != std::string::npos) { while (pos != std::string::npos) {
// Empty line
if (pos == 0) { if (pos == 0) {
if (!header_callback(name_, file_)) { if (!header_callback(name_, file_)) {
is_valid_ = false; is_valid_ = false;
@ -2034,6 +2029,7 @@ public:
file_.content_type = m[1]; file_.content_type = m[1];
} else if (std::regex_match(header, m, re_content_disposition)) { } else if (std::regex_match(header, m, re_content_disposition)) {
name_ = m[1]; name_ = m[1];
file_.name = name_;
file_.filename = m[2]; file_.filename = m[2];
} }
} }
@ -2137,7 +2133,7 @@ private:
size_t is_done_ = false; size_t is_done_ = false;
size_t off_ = 0; size_t off_ = 0;
std::string name_; std::string name_;
MultipartFile file_; MultipartFormData file_;
}; };
inline std::string to_lower(const char *beg, const char *end) { inline std::string to_lower(const char *beg, const char *end) {
@ -2512,10 +2508,10 @@ inline bool Request::has_file(const char *key) const {
return files.find(key) != files.end(); return files.find(key) != files.end();
} }
inline MultipartFile Request::get_file_value(const char *key) const { inline MultipartFormData Request::get_file_value(const char *key) const {
auto it = files.find(key); auto it = files.find(key);
if (it != files.end()) { return it->second; } if (it != files.end()) { return it->second; }
return MultipartFile(); return MultipartFormData();
} }
// Response implementation // Response implementation
@ -2985,7 +2981,7 @@ inline bool Server::read_content(Stream &strm, bool last_connection,
return true; return true;
}, },
// Multipart // Multipart
[&](const std::string &name, const MultipartFile &file) { [&](const std::string &name, const MultipartFormData &file) {
req.files.emplace(name, file); req.files.emplace(name, file);
return true; return true;
}, },

View File

@ -30,7 +30,7 @@ const std::string JSON_DATA = "{\"hello\":\"world\"}";
const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
MultipartFile& get_file_value(MultipartFiles &files, const char *key) { MultipartFormData& get_file_value(MultipartFormDataMap &files, const char *key) {
auto it = files.find(key); auto it = files.find(key);
if (it != files.end()) { return it->second; } if (it != files.end()) { return it->second; }
throw std::runtime_error("invalid mulitpart form data name error"); throw std::runtime_error("invalid mulitpart form data name error");
@ -801,9 +801,9 @@ protected:
.Post("/content_receiver", .Post("/content_receiver",
[&](const Request & req, Response &res, const ContentReader &content_reader) { [&](const Request & req, Response &res, const ContentReader &content_reader) {
if (req.is_multipart_form_data()) { if (req.is_multipart_form_data()) {
MultipartFiles files; MultipartFormDataMap files;
content_reader( content_reader(
[&](const std::string &name, const MultipartFile &file) { [&](const std::string &name, const MultipartFormData &file) {
files.emplace(name, file); files.emplace(name, file);
return true; return true;
}, },