Limit the HTTP body content to dump/log.

This commit is contained in:
Chunting Gu
2018-10-11 14:32:34 +08:00
parent d159ce46bf
commit e3e9a22181
4 changed files with 54 additions and 4 deletions
+5
View File
@@ -40,6 +40,11 @@ const std::size_t kInvalidLength = std::string::npos;
// Default timeout for reading response.
const int kMaxReadSeconds = 30;
// Max size of the HTTP body to dump/log.
// If the HTTP, e.g., response, has a very large content, it will be truncated
// when dumped/logged.
const std::size_t kMaxDumpSize = 2048;
// HTTP headers.
extern const std::string kHost;
extern const std::string kContentType;
+22 -2
View File
@@ -77,14 +77,34 @@ void HttpMessage::Dump(std::ostream& os, std::size_t indent,
os << indent_str << std::endl;
// NOTE: The content will be truncated if it's too large to display.
if (!content_.empty()) {
if (indent == 0) {
os << content_ << std::endl;
if (content_.size() > kMaxDumpSize) {
os.write(content_.c_str(), kMaxDumpSize);
os << "..." << std::endl;
} else {
os << content_ << std::endl;
}
} else {
// Split by EOL to achieve more readability.
std::vector<std::string> splitted;
boost::split(splitted, content_, boost::is_any_of(CRLF));
std::size_t size = 0;
for (const std::string& line : splitted) {
os << indent_str << line << std::endl;
os << indent_str;
if (line.size() + size > kMaxDumpSize) {
os.write(line.c_str(), kMaxDumpSize - size);
os << "..." << std::endl;
break;
} else {
os << line << std::endl;
size += line.size();
}
}
}
}