Skip to content

Commit 853439c

Browse files
committed
Add protocol_bitcoind::send_json and handle_receive_options.
1 parent c846c40 commit 853439c

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

include/bitcoin/node/protocols/protocol_bitcoind.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class BCN_API protocol_bitcoind
5656
}
5757

5858
/// Dispatch.
59+
void handle_receive_options(const code& ec,
60+
const network::http::method::options::cptr& options) NOEXCEPT override;
5961
void handle_receive_post(const code& ec,
6062
const network::http::method::post::cptr& post) NOEXCEPT override;
6163

@@ -99,6 +101,9 @@ class BCN_API protocol_bitcoind
99101
interface::verify_tx_out_set, const std::string&) NOEXCEPT;
100102

101103
private:
104+
void send_json(boost::json::value&& model, size_t size_hint,
105+
const network::http::request& request={}) NOEXCEPT;
106+
102107
// This is thread safe.
103108
////const options_t& options_;
104109

src/protocols/protocol_bitcoind.cpp

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ using namespace network::http;
3434
using namespace std::placeholders;
3535
using namespace boost::json;
3636

37-
using json_t = json_body::value_type;
38-
37+
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT)
3938
BC_PUSH_WARNING(SMART_PTR_NOT_NEEDED)
4039
BC_PUSH_WARNING(NO_VALUE_OR_CONST_REF_SHARED_PTR)
4140

@@ -74,6 +73,32 @@ void protocol_bitcoind::start() NOEXCEPT
7473
// Dispatch.
7574
// ----------------------------------------------------------------------------
7675

76+
void protocol_bitcoind::handle_receive_options(const code& ec,
77+
const network::http::method::options::cptr& options) NOEXCEPT
78+
{
79+
BC_ASSERT(stranded());
80+
81+
if (stopped(ec))
82+
return;
83+
84+
// Enforce http host header (if any hosts are configured).
85+
if (!is_allowed_host(*options, options->version()))
86+
{
87+
send_bad_host(*options);
88+
return;
89+
}
90+
91+
// Enforce http origin policy (if any origins are configured).
92+
if (!is_allowed_origin(*options, options->version()))
93+
{
94+
send_forbidden(*options);
95+
return;
96+
}
97+
98+
send_ok(*options);
99+
}
100+
101+
// TODO: also handle_receive_get and dispatch based on URL parse.
77102
void protocol_bitcoind::handle_receive_post(const code& ec,
78103
const network::http::method::post::cptr& post) NOEXCEPT
79104
{
@@ -82,6 +107,21 @@ void protocol_bitcoind::handle_receive_post(const code& ec,
82107
if (stopped(ec))
83108
return;
84109

110+
// Enforce http host header (if any hosts are configured).
111+
if (!is_allowed_host(*post, post->version()))
112+
{
113+
send_bad_host(*post);
114+
return;
115+
}
116+
117+
// Enforce http origin policy (if any origins are configured).
118+
if (!is_allowed_origin(*post, post->version()))
119+
{
120+
send_forbidden(*post);
121+
return;
122+
}
123+
124+
using json_t = json_body::value_type;
85125
const auto& body = post->body();
86126
if (!body.contains<json_t>())
87127
{
@@ -228,6 +268,24 @@ bool protocol_bitcoind::handle_verify_tx_out_set(const code& ec,
228268
return !ec;
229269
}
230270

271+
// private
272+
// ----------------------------------------------------------------------------
273+
274+
// TODO: post-process response for json-rpc version.
275+
void protocol_bitcoind::send_json(boost::json::value&& model, size_t size_hint,
276+
const request& request) NOEXCEPT
277+
{
278+
BC_ASSERT(stranded());
279+
constexpr auto json = media_type::application_json;
280+
response response{ status::ok, request.version() };
281+
add_common_headers(response, request);
282+
response.set(field::content_type, from_media_type(json));
283+
response.body() = { std::move(model), size_hint };
284+
response.prepare_payload();
285+
SEND(std::move(response), handle_complete, _1, error::success);
286+
}
287+
288+
BC_POP_WARNING()
231289
BC_POP_WARNING()
232290
BC_POP_WARNING()
233291

0 commit comments

Comments
 (0)