Limit the HTTP body content to dump/log.
This commit is contained in:
@@ -5,6 +5,6 @@ if(WIN32)
|
|||||||
set(SSL_LIBS ${SSL_LIBS} crypt32)
|
set(SSL_LIBS ${SSL_LIBS} crypt32)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
target_link_libraries(rest_github_client webcc ${Boost_LIBRARIES})
|
target_link_libraries(rest_github_client webcc jsoncpp ${Boost_LIBRARIES})
|
||||||
target_link_libraries(rest_github_client "${CMAKE_THREAD_LIBS_INIT}")
|
target_link_libraries(rest_github_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||||
target_link_libraries(rest_github_client ${SSL_LIBS})
|
target_link_libraries(rest_github_client ${SSL_LIBS})
|
||||||
|
|||||||
@@ -1,13 +1,38 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "json/json.h"
|
||||||
|
|
||||||
#include "webcc/rest_ssl_client.h"
|
#include "webcc/rest_ssl_client.h"
|
||||||
#include "webcc/logger.h"
|
#include "webcc/logger.h"
|
||||||
|
|
||||||
|
static Json::Value StringToJson(const std::string& str) {
|
||||||
|
Json::Value json;
|
||||||
|
|
||||||
|
Json::CharReaderBuilder builder;
|
||||||
|
std::stringstream stream(str);
|
||||||
|
std::string errs;
|
||||||
|
if (!Json::parseFromStream(builder, stream, &json, &errs)) {
|
||||||
|
std::cerr << errs << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
|
||||||
void Test() {
|
void Test() {
|
||||||
webcc::RestSslClient client("api.github.com");
|
webcc::RestSslClient client("api.github.com");
|
||||||
|
|
||||||
if (client.Get("/events")) {
|
if (client.Get("/events")) {
|
||||||
std::cout << client.response()->content() << std::endl;
|
Json::Value json = StringToJson(client.response()->content());
|
||||||
|
|
||||||
|
// Pretty print the JSON.
|
||||||
|
|
||||||
|
Json::StreamWriterBuilder builder;
|
||||||
|
builder["indentation"] = " ";
|
||||||
|
|
||||||
|
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
|
||||||
|
writer->write(json, &std::cout);
|
||||||
|
|
||||||
|
std::cout << std::endl;
|
||||||
} else {
|
} else {
|
||||||
std::cout << webcc::DescribeError(client.error());
|
std::cout << webcc::DescribeError(client.error());
|
||||||
if (client.timed_out()) {
|
if (client.timed_out()) {
|
||||||
|
|||||||
@@ -40,6 +40,11 @@ const std::size_t kInvalidLength = std::string::npos;
|
|||||||
// Default timeout for reading response.
|
// Default timeout for reading response.
|
||||||
const int kMaxReadSeconds = 30;
|
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.
|
// HTTP headers.
|
||||||
extern const std::string kHost;
|
extern const std::string kHost;
|
||||||
extern const std::string kContentType;
|
extern const std::string kContentType;
|
||||||
|
|||||||
+22
-2
@@ -77,14 +77,34 @@ void HttpMessage::Dump(std::ostream& os, std::size_t indent,
|
|||||||
|
|
||||||
os << indent_str << std::endl;
|
os << indent_str << std::endl;
|
||||||
|
|
||||||
|
// NOTE: The content will be truncated if it's too large to display.
|
||||||
|
|
||||||
if (!content_.empty()) {
|
if (!content_.empty()) {
|
||||||
if (indent == 0) {
|
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 {
|
} else {
|
||||||
|
// Split by EOL to achieve more readability.
|
||||||
std::vector<std::string> splitted;
|
std::vector<std::string> splitted;
|
||||||
boost::split(splitted, content_, boost::is_any_of(CRLF));
|
boost::split(splitted, content_, boost::is_any_of(CRLF));
|
||||||
|
|
||||||
|
std::size_t size = 0;
|
||||||
|
|
||||||
for (const std::string& line : splitted) {
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user