Fix the ambiguity of the contructors of FormPart.
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
TEST(FormBodyTest, Payload) {
|
TEST(FormBodyTest, Payload) {
|
||||||
std::vector<webcc::FormPartPtr> parts{
|
std::vector<webcc::FormPartPtr> parts{
|
||||||
std::make_shared<webcc::FormPart>("json", "{}", "application/json")
|
webcc::FormPart::New("json", "{}", "application/json")
|
||||||
};
|
};
|
||||||
|
|
||||||
webcc::FormBody form_body{ parts, "123456" };
|
webcc::FormBody form_body{ parts, "123456" };
|
||||||
|
|||||||
+24
-11
@@ -168,24 +168,37 @@ bool ContentDisposition::Init(const std::string& str) {
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
FormPart::FormPart(const std::string& name, const Path& path,
|
FormPartPtr FormPart::New(const std::string& name, std::string&& data,
|
||||||
const std::string& media_type)
|
const std::string& media_type) {
|
||||||
: name_(name), path_(path), media_type_(media_type) {
|
auto form_part = std::make_shared<FormPart>();
|
||||||
|
|
||||||
|
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<FormPart>();
|
||||||
|
|
||||||
|
form_part->name_ = name;
|
||||||
|
form_part->path_ = path;
|
||||||
|
form_part->media_type_ = media_type;
|
||||||
|
|
||||||
// Determine file name from file path.
|
// Determine file name from file path.
|
||||||
// TODO: encoding
|
// TODO: encoding
|
||||||
file_name_ = path.filename().string(std::codecvt_utf8<wchar_t>());
|
form_part->file_name_ = path.filename().string(std::codecvt_utf8<wchar_t>());
|
||||||
|
|
||||||
// Determine media type from file extension.
|
// Determine media type from file extension.
|
||||||
if (media_type_.empty()) {
|
|
||||||
std::string extension = path.extension().string();
|
|
||||||
// TODO: Default to "application/text"?
|
// TODO: Default to "application/text"?
|
||||||
media_type_ = media_types::FromExtension(extension);
|
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,
|
return form_part;
|
||||||
const std::string& media_type)
|
|
||||||
: name_(name), data_(std::move(data)), media_type_(media_type) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormPart::Prepare(Payload* payload) {
|
void FormPart::Prepare(Payload* payload) {
|
||||||
|
|||||||
+11
-10
@@ -135,26 +135,29 @@ private:
|
|||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class FormPart;
|
||||||
|
using FormPartPtr = std::shared_ptr<FormPart>;
|
||||||
|
|
||||||
// A part of the multipart form data.
|
// A part of the multipart form data.
|
||||||
class FormPart {
|
class FormPart {
|
||||||
public:
|
public:
|
||||||
FormPart() = default;
|
FormPart() = default;
|
||||||
|
|
||||||
// Construct a file part.
|
FormPart(const FormPart&) = delete;
|
||||||
// The file name will be extracted from path.
|
FormPart& operator=(const FormPart&) = delete;
|
||||||
// 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 = "");
|
|
||||||
|
|
||||||
// Construct a non-file part.
|
// Construct a non-file part.
|
||||||
// The data will be moved, no file name is needed.
|
// 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
|
// The media type is optional. If the data is a JSON string, you can specify
|
||||||
// media type as "application/json".
|
// media type as "application/json".
|
||||||
FormPart(const std::string& name, std::string&& data,
|
static FormPartPtr New(const std::string& name, std::string&& data,
|
||||||
const std::string& media_type = "");
|
const std::string& media_type = "");
|
||||||
|
|
||||||
FormPart(const FormPart&) = delete;
|
// Construct a file part.
|
||||||
FormPart& operator=(const FormPart&) = delete;
|
// 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
|
// API: SERVER
|
||||||
const std::string& name() const {
|
const std::string& name() const {
|
||||||
@@ -243,8 +246,6 @@ private:
|
|||||||
std::string data_;
|
std::string data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using FormPartPtr = std::shared_ptr<FormPart>;
|
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|
||||||
#endif // WEBCC_COMMON_H_
|
#endif // WEBCC_COMMON_H_
|
||||||
|
|||||||
@@ -55,8 +55,7 @@ RequestBuilder& RequestBuilder::File(const std::string& name,
|
|||||||
const Path& path,
|
const Path& path,
|
||||||
const std::string& media_type) {
|
const std::string& media_type) {
|
||||||
assert(!name.empty());
|
assert(!name.empty());
|
||||||
auto part = std::make_shared<FormPart>(name, path, media_type);
|
form_parts_.push_back(FormPart::NewFile(name, path, media_type));
|
||||||
form_parts_.push_back(part);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,8 +63,7 @@ RequestBuilder& RequestBuilder::Form(const std::string& name,
|
|||||||
std::string&& data,
|
std::string&& data,
|
||||||
const std::string& media_type) {
|
const std::string& media_type) {
|
||||||
assert(!name.empty());
|
assert(!name.empty());
|
||||||
auto part = std::make_shared<FormPart>(name, std::move(data), media_type);
|
form_parts_.push_back(FormPart::New(name, std::move(data), media_type));
|
||||||
form_parts_.push_back(part);
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user