Skip to content

Commit 31aea76

Browse files
authored
Merge pull request #617 from evoskuil/master
LOGP request/response messages.
2 parents ff0021d + 6716be8 commit 31aea76

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

include/bitcoin/network/channels/channel_http.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ class BCT_API channel_http
6767
shared_from_base<channel_http>(), _1, _2, ptr, std::move(handler));
6868

6969
if (!ptr)
70+
{
7071
complete(error::bad_alloc, {});
71-
else
72-
write(*ptr, std::move(complete));
72+
return;
73+
}
74+
75+
log_message(*ptr);
76+
write(*ptr, std::move(complete));
7377
}
7478

7579
/// response_buffer_ is initialized to default size, see set_buffer().
@@ -119,6 +123,9 @@ class BCT_API channel_http
119123
void handle_read_request(const code& ec, size_t bytes_read,
120124
const http::request_cptr& request) NOEXCEPT;
121125

126+
void log_message(const http::request& request) const NOEXCEPT;
127+
void log_message(const http::response& response) const NOEXCEPT;
128+
122129
// These are protected by strand.
123130
http::flat_buffer_ptr response_buffer_;
124131
http::flat_buffer request_buffer_;

include/bitcoin/network/messages/http/enums/mime_type.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ BCT_API mime_type content_mime_type(const std::string_view& content_type,
6666
mime_type default_=mime_type::unknown) NOEXCEPT;
6767
BCT_API mime_type content_mime_type(const http::fields& fields,
6868
mime_type default_ = mime_type::unknown) NOEXCEPT;
69+
6970
BCT_API mime_type extension_mime_type(const std::string_view& extension,
7071
mime_type default_=mime_type::unknown) NOEXCEPT;
7172
BCT_API mime_type file_mime_type(const std::filesystem::path& path,

include/bitcoin/network/messages/http/enums/status.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ namespace http {
2828
/// Status is aliased to beast boost define.
2929
using status = boost::beast::http::status;
3030

31+
inline std::string status_string(status code) NOEXCEPT
32+
{
33+
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
34+
return boost::beast::http::obsolete_reason(code);
35+
BC_POP_WARNING()
36+
}
37+
3138
} // namespace http
3239
} // namespace network
3340
} // namespace libbitcoin

src/channels/channel_http.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
#include <bitcoin/network/channels/channel_http.hpp>
2020

2121
#include <bitcoin/network/async/async.hpp>
22+
#include <bitcoin/network/channels/channel.hpp>
2223
#include <bitcoin/network/define.hpp>
2324
#include <bitcoin/network/log/log.hpp>
24-
#include <bitcoin/network/channels/channel.hpp>
25+
#include <bitcoin/network/messages/http/http.hpp>
2526
#include <bitcoin/network/settings.hpp>
2627

2728
namespace libbitcoin {
@@ -107,9 +108,44 @@ void channel_http::handle_read_request(const code& ec, size_t,
107108
return;
108109
}
109110

111+
log_message(*request);
110112
distributor_.notify(request);
111113
}
112114

115+
// log helpers
116+
// ----------------------------------------------------------------------------
117+
118+
void channel_http::log_message(const http::request& request) const NOEXCEPT
119+
{
120+
LOG_ONLY(const auto size = serialize(request.payload_size()
121+
.has_value() ? request.payload_size().value() : zero);)
122+
123+
LOG_ONLY(const auto version = "http/" + serialize(request.version() / 10) +
124+
"." + serialize(request.version() % 10);)
125+
126+
LOGP("Request [" << request.method_string()
127+
<< "] " << version << " (" << (request.chunked() ? "c" : size)
128+
<< ") " << (request.keep_alive() ? "keep" : "drop")
129+
<< " [" << authority() << "]"
130+
<< " {" << (split(request[http::field::accept], ",").front()) << "...}"
131+
<< " " << request.target());
132+
}
133+
134+
void channel_http::log_message(const http::response& response) const NOEXCEPT
135+
{
136+
LOG_ONLY(const auto size = serialize(response.payload_size()
137+
.has_value() ? response.payload_size().value() : zero);)
138+
139+
LOG_ONLY(const auto version = "http/" + serialize(response.version() / 10)
140+
+ "." + serialize(response.version() % 10);)
141+
142+
LOGP("Response [" << http::status_string(response.result())
143+
<< "] " << version << " (" << (response.chunked() ? "c" : size)
144+
<< ") " << (response.keep_alive() ? "keep" : "drop")
145+
<< " [" << authority() << "]"
146+
<< " {" << (response[http::field::content_type]) << "}");
147+
}
148+
113149
BC_POP_WARNING()
114150
BC_POP_WARNING()
115151
BC_POP_WARNING()

test/channels/channel_http.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ BOOST_AUTO_TEST_CASE(channel_http__send__not_connected__expected)
200200
BOOST_REQUIRE(!channel_ptr->stopped());
201201
boost::asio::post(channel_ptr->strand(), [&]() NOEXCEPT
202202
{
203-
channel_ptr->send<http::string_response>({}, handler);
203+
channel_ptr->send<http::response>({}, handler);
204204
});
205205

206206
// 10009 (WSAEBADF, invalid file handle) gets mapped to bad_stream.
@@ -227,7 +227,7 @@ BOOST_AUTO_TEST_CASE(channel_http__send__not_connected_move__expected)
227227
BOOST_REQUIRE(!channel_ptr->stopped());
228228
boost::asio::post(channel_ptr->strand(), [&]() NOEXCEPT
229229
{
230-
channel_ptr->send(http::string_response{}, [&](code ec)
230+
channel_ptr->send(http::response{}, [&](code ec)
231231
{
232232
result &= channel_ptr->stopped();
233233
promise.set_value(ec);

0 commit comments

Comments
 (0)