Rework the body of request and response

This commit is contained in:
Chunting Gu
2019-07-03 17:37:44 +08:00
parent b9f2ba6a41
commit 98aeeae012
39 changed files with 1122 additions and 677 deletions
+32 -5
View File
@@ -113,11 +113,14 @@ ContentType::ContentType(const std::string& str) {
}
void ContentType::Parse(const std::string& str) {
Reset();
Init(str);
}
void ContentType::Reset() {
media_type_.clear();
additional_.clear();
multipart_ = false;
Init(str);
}
bool ContentType::Valid() const {
@@ -224,15 +227,17 @@ FormPart::FormPart(const std::string& name, std::string&& data,
}
void FormPart::Prepare(Payload* payload) {
using boost::asio::buffer;
// NOTE:
// The payload buffers don't own the memory.
// It depends on some existing variables/objects to keep the memory.
// That's why we need |headers_|.
// That's why we need save headers to member variable.
if (headers_.empty()) {
SetHeaders();
}
using boost::asio::buffer;
for (const Header& h : headers_.data()) {
payload->push_back(buffer(h.first));
payload->push_back(buffer(misc_strings::HEADER_SEPARATOR));
@@ -249,6 +254,28 @@ void FormPart::Prepare(Payload* payload) {
payload->push_back(buffer(misc_strings::CRLF));
}
std::size_t FormPart::GetSize() {
std::size_t size = 0;
if (headers_.empty()) {
SetHeaders();
}
for (const Header& h : headers_.data()) {
size += h.first.size();
size += sizeof(misc_strings::HEADER_SEPARATOR);
size += h.second.size();
size += sizeof(misc_strings::CRLF);
}
size += sizeof(misc_strings::CRLF);
size += data_.size();
size += sizeof(misc_strings::CRLF);
return size;
}
void FormPart::SetHeaders() {
// Header: Content-Disposition