Skip to content

Commit 151ccba

Browse files
committed
Code cleanup
1 parent 69a28d5 commit 151ccba

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

httplib.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,20 @@ using Progress = std::function<bool(uint64_t current, uint64_t total)>;
211211
struct Response;
212212
using ResponseHandler = std::function<bool(const Response &response)>;
213213

214-
struct MultipartFile {
215-
std::string filename;
216-
std::string content_type;
217-
std::string content;
218-
};
219-
using MultipartFiles = std::multimap<std::string, MultipartFile>;
220-
221214
struct MultipartFormData {
222215
std::string name;
223216
std::string content;
224217
std::string filename;
225218
std::string content_type;
226219
};
227220
using MultipartFormDataItems = std::vector<MultipartFormData>;
221+
using MultipartFormDataMap = std::multimap<std::string, MultipartFormData>;
228222

229223
using ContentReceiver =
230224
std::function<bool(const char *data, size_t data_length)>;
231225

232226
using MultipartContentHeader =
233-
std::function<bool(const std::string &name, const MultipartFile &file)>;
227+
std::function<bool(const std::string &name, const MultipartFormData &file)>;
234228

235229
using MultipartContentReceiver =
236230
std::function<bool(const std::string& name, const char *data, size_t data_length)>;
@@ -268,7 +262,7 @@ struct Request {
268262
std::string version;
269263
std::string target;
270264
Params params;
271-
MultipartFiles files;
265+
MultipartFormDataMap files;
272266
Ranges ranges;
273267
Match matches;
274268

@@ -295,7 +289,7 @@ struct Request {
295289
bool is_multipart_form_data() const;
296290

297291
bool has_file(const char *key) const;
298-
MultipartFile get_file_value(const char *key) const;
292+
MultipartFormData get_file_value(const char *key) const;
299293

300294
// private members...
301295
size_t content_length;
@@ -2015,6 +2009,7 @@ class MultipartFormDataParser {
20152009
case 2: { // Headers
20162010
auto pos = buf_.find(crlf_);
20172011
while (pos != std::string::npos) {
2012+
// Empty line
20182013
if (pos == 0) {
20192014
if (!header_callback(name_, file_)) {
20202015
is_valid_ = false;
@@ -2034,6 +2029,7 @@ class MultipartFormDataParser {
20342029
file_.content_type = m[1];
20352030
} else if (std::regex_match(header, m, re_content_disposition)) {
20362031
name_ = m[1];
2032+
file_.name = name_;
20372033
file_.filename = m[2];
20382034
}
20392035
}
@@ -2137,7 +2133,7 @@ class MultipartFormDataParser {
21372133
size_t is_done_ = false;
21382134
size_t off_ = 0;
21392135
std::string name_;
2140-
MultipartFile file_;
2136+
MultipartFormData file_;
21412137
};
21422138

21432139
inline std::string to_lower(const char *beg, const char *end) {
@@ -2512,10 +2508,10 @@ inline bool Request::has_file(const char *key) const {
25122508
return files.find(key) != files.end();
25132509
}
25142510

2515-
inline MultipartFile Request::get_file_value(const char *key) const {
2511+
inline MultipartFormData Request::get_file_value(const char *key) const {
25162512
auto it = files.find(key);
25172513
if (it != files.end()) { return it->second; }
2518-
return MultipartFile();
2514+
return MultipartFormData();
25192515
}
25202516

25212517
// Response implementation
@@ -2985,7 +2981,7 @@ inline bool Server::read_content(Stream &strm, bool last_connection,
29852981
return true;
29862982
},
29872983
// Multipart
2988-
[&](const std::string &name, const MultipartFile &file) {
2984+
[&](const std::string &name, const MultipartFormData &file) {
29892985
req.files.emplace(name, file);
29902986
return true;
29912987
},

test/test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const std::string JSON_DATA = "{\"hello\":\"world\"}";
3030

3131
const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
3232

33-
MultipartFile& get_file_value(MultipartFiles &files, const char *key) {
33+
MultipartFormData& get_file_value(MultipartFormDataMap &files, const char *key) {
3434
auto it = files.find(key);
3535
if (it != files.end()) { return it->second; }
3636
throw std::runtime_error("invalid mulitpart form data name error");
@@ -801,9 +801,9 @@ class ServerTest : public ::testing::Test {
801801
.Post("/content_receiver",
802802
[&](const Request & req, Response &res, const ContentReader &content_reader) {
803803
if (req.is_multipart_form_data()) {
804-
MultipartFiles files;
804+
MultipartFormDataMap files;
805805
content_reader(
806-
[&](const std::string &name, const MultipartFile &file) {
806+
[&](const std::string &name, const MultipartFormData &file) {
807807
files.emplace(name, file);
808808
return true;
809809
},

0 commit comments

Comments
 (0)