Remove HttpFile to FormPart; refine payload prepare.
This commit is contained in:
@@ -1,13 +1,25 @@
|
||||
#include "webcc/common.h"
|
||||
|
||||
#include "boost/algorithm/string.hpp"
|
||||
#include "boost/filesystem/fstream.hpp"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
|
||||
namespace bfs = boost::filesystem;
|
||||
|
||||
namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace misc_strings {
|
||||
|
||||
const char HEADER_SEPARATOR[] = { ':', ' ' };
|
||||
const char CRLF[] = { '\r', '\n' };
|
||||
|
||||
} // misc_strings
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
bool Split2(const std::string& str, char token, std::string* part1,
|
||||
std::string* part2) {
|
||||
std::size_t pos = str.find(token);
|
||||
@@ -24,6 +36,20 @@ bool Split2(const std::string& str, char token, std::string* part1,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadFile(const Path& path, std::string* output) {
|
||||
bfs::ifstream ifs{ path, std::ios::binary | std::ios::ate };
|
||||
if (!ifs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto size = ifs.tellg();
|
||||
output->resize(static_cast<std::size_t>(size), '\0');
|
||||
ifs.seekg(0);
|
||||
ifs.read(&(*output)[0], size); // TODO: Error handling
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void HttpHeaders::Set(const std::string& key, const std::string& value) {
|
||||
@@ -169,4 +195,76 @@ bool ContentDisposition::Init(const std::string& str) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
FormPart::FormPart(const std::string& name, const Path& path,
|
||||
const std::string& mime_type)
|
||||
: name_(name), mime_type_(mime_type) {
|
||||
if (!ReadFile(path, &data_)) {
|
||||
throw Exception(kFileIOError, "Cannot read the file.");
|
||||
}
|
||||
|
||||
// Determine file name from file path.
|
||||
// TODO: Encoding
|
||||
file_name_ = path.filename().string();
|
||||
|
||||
// Determine content type from file extension.
|
||||
if (mime_type_.empty()) {
|
||||
std::string extension = path.extension().string();
|
||||
mime_type_ = http::media_types::FromExtension(extension, false);
|
||||
}
|
||||
}
|
||||
|
||||
FormPart::FormPart(std::string&& data, const std::string& file_name,
|
||||
const std::string& mime_type) {
|
||||
data_ = std::move(data);
|
||||
|
||||
file_name_ = file_name;
|
||||
|
||||
mime_type_ = mime_type;
|
||||
|
||||
// Determine content type from file extension.
|
||||
if (mime_type_.empty()) {
|
||||
std::size_t pos = file_name_.find_last_of('.');
|
||||
if (pos != std::string::npos) {
|
||||
std::string extension = file_name_.substr(pos + 1);
|
||||
mime_type_ = http::media_types::FromExtension(extension, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FormPart::Prepare(std::vector<boost::asio::const_buffer>& payload) {
|
||||
if (headers_.empty()) {
|
||||
std::string value = "form-data";
|
||||
if (!name_.empty()) {
|
||||
value.append("; name=\"" + name_ + "\"");
|
||||
}
|
||||
if (!file_name_.empty()) {
|
||||
value.append("; filename=\"" + file_name_ + "\"");
|
||||
}
|
||||
headers_.Set(http::headers::kContentDisposition, value);
|
||||
|
||||
if (!mime_type_.empty()) {
|
||||
headers_.Set(http::headers::kContentType, mime_type_);
|
||||
}
|
||||
}
|
||||
|
||||
using boost::asio::buffer;
|
||||
|
||||
for (const HttpHeader& h : headers_.data()) {
|
||||
payload.push_back(buffer(h.first));
|
||||
payload.push_back(buffer(misc_strings::HEADER_SEPARATOR));
|
||||
payload.push_back(buffer(h.second));
|
||||
payload.push_back(buffer(misc_strings::CRLF));
|
||||
}
|
||||
|
||||
payload.push_back(buffer(misc_strings::CRLF));
|
||||
|
||||
if (!data_.empty()) {
|
||||
payload.push_back(buffer(data_));
|
||||
}
|
||||
|
||||
payload.push_back(buffer(misc_strings::CRLF));
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
Reference in New Issue
Block a user