From 2a20030f7e1c1bc30e5e5f032aab1477942300ac Mon Sep 17 00:00:00 2001 From: Adam Gu Date: Sun, 14 Jul 2019 09:01:48 +0800 Subject: [PATCH] Fix the ambiguity of the contructors of FormPart. --- unittest/body_unittest.cc | 2 +- webcc/common.cc | 37 +++++++++++++++++++++++++------------ webcc/common.h | 23 ++++++++++++----------- webcc/request_builder.cc | 6 ++---- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/unittest/body_unittest.cc b/unittest/body_unittest.cc index 979da6d..d744708 100644 --- a/unittest/body_unittest.cc +++ b/unittest/body_unittest.cc @@ -4,7 +4,7 @@ TEST(FormBodyTest, Payload) { std::vector parts{ - std::make_shared("json", "{}", "application/json") + webcc::FormPart::New("json", "{}", "application/json") }; webcc::FormBody form_body{ parts, "123456" }; diff --git a/webcc/common.cc b/webcc/common.cc index 7e7ef8b..d50169a 100644 --- a/webcc/common.cc +++ b/webcc/common.cc @@ -168,24 +168,37 @@ bool ContentDisposition::Init(const std::string& str) { // ----------------------------------------------------------------------------- -FormPart::FormPart(const std::string& name, const Path& path, - const std::string& media_type) - : name_(name), path_(path), media_type_(media_type) { +FormPartPtr FormPart::New(const std::string& name, std::string&& data, + const std::string& media_type) { + auto form_part = std::make_shared(); + + form_part->name_ = name; + form_part->data_ = std::move(data); + form_part->media_type_ = media_type; + + return form_part; +} + +FormPartPtr FormPart::NewFile(const std::string& name, const Path& path, + const std::string& media_type) { + auto form_part = std::make_shared(); + + form_part->name_ = name; + form_part->path_ = path; + form_part->media_type_ = media_type; + // Determine file name from file path. // TODO: encoding - file_name_ = path.filename().string(std::codecvt_utf8()); + form_part->file_name_ = path.filename().string(std::codecvt_utf8()); // Determine media type from file extension. - if (media_type_.empty()) { - std::string extension = path.extension().string(); - // TODO: Default to "application/text"? - media_type_ = media_types::FromExtension(extension); + // TODO: Default to "application/text"? + if (form_part->media_type_.empty()) { + auto ext = path.extension().string(); + form_part->media_type_ = media_types::FromExtension(ext); } -} -FormPart::FormPart(const std::string& name, std::string&& data, - const std::string& media_type) - : name_(name), data_(std::move(data)), media_type_(media_type) { + return form_part; } void FormPart::Prepare(Payload* payload) { diff --git a/webcc/common.h b/webcc/common.h index 2cd3e55..7afb6bd 100644 --- a/webcc/common.h +++ b/webcc/common.h @@ -135,26 +135,29 @@ private: // ----------------------------------------------------------------------------- +class FormPart; +using FormPartPtr = std::shared_ptr; + // A part of the multipart form data. class FormPart { public: FormPart() = default; - // Construct a file part. - // The file name will be extracted from path. - // The media type, if not provided, will be inferred from file extension. - FormPart(const std::string& name, const Path& path, - const std::string& media_type = ""); + FormPart(const FormPart&) = delete; + FormPart& operator=(const FormPart&) = delete; // Construct a non-file part. // The data will be moved, no file name is needed. // The media type is optional. If the data is a JSON string, you can specify // media type as "application/json". - FormPart(const std::string& name, std::string&& data, - const std::string& media_type = ""); + static FormPartPtr New(const std::string& name, std::string&& data, + const std::string& media_type = ""); - FormPart(const FormPart&) = delete; - FormPart& operator=(const FormPart&) = delete; + // Construct a file part. + // The file name will be extracted from path. + // The media type, if not provided, will be inferred from file extension. + static FormPartPtr NewFile(const std::string& name, const Path& path, + const std::string& media_type = ""); // API: SERVER const std::string& name() const { @@ -243,8 +246,6 @@ private: std::string data_; }; -using FormPartPtr = std::shared_ptr; - } // namespace webcc #endif // WEBCC_COMMON_H_ diff --git a/webcc/request_builder.cc b/webcc/request_builder.cc index 493cb8e..ea5eda0 100644 --- a/webcc/request_builder.cc +++ b/webcc/request_builder.cc @@ -55,8 +55,7 @@ RequestBuilder& RequestBuilder::File(const std::string& name, const Path& path, const std::string& media_type) { assert(!name.empty()); - auto part = std::make_shared(name, path, media_type); - form_parts_.push_back(part); + form_parts_.push_back(FormPart::NewFile(name, path, media_type)); return *this; } @@ -64,8 +63,7 @@ RequestBuilder& RequestBuilder::Form(const std::string& name, std::string&& data, const std::string& media_type) { assert(!name.empty()); - auto part = std::make_shared(name, std::move(data), media_type); - form_parts_.push_back(part); + form_parts_.push_back(FormPart::New(name, std::move(data), media_type)); return *this; }