Skip to content

Commit cfce37b

Browse files
Apply review feedback.
1 parent 51abe37 commit cfce37b

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

src/web/manager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,9 @@ bool manager::handle_websocket(connection_ptr connection)
878878
const auto header_length = frame.header_length();
879879

880880
LOG_VERBOSE(LOG_PROTOCOL_HTTP)
881-
<< "Websocket data_frame flags: " << flags
882-
<< ", final_fragment: " << (final ? "true" : "false")
883-
<< ", read length: " << read_length;
881+
<< "Websocket data_frame flags: 0x" << std::hex
882+
<< static_cast<uint32_t>(flags) << std::dec << ", final_fragment: "
883+
<< (final ? "true" : "false") << ", read length: " << read_length;
884884

885885
// If full frame payload isn't buffered, initiate state to track transfer.
886886
if (data_length > read_length && !transfer.in_progress)

src/web/socket.cpp

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class task_sender
5252
auto narrow = [](int32_t& out, size_t in)
5353
{
5454
out = static_cast<int32_t>(in);
55-
return (in > std::numeric_limits<int32_t>::max() ||
56-
(static_cast<size_t>(out) != in)) ? false : true;
55+
return out >= 0 && out <= std::numeric_limits<int32_t>::max() &&
56+
static_cast<size_t>(out) == in;
5757
};
5858

5959
int32_t data_size;
@@ -163,33 +163,39 @@ class query_response_task_sender
163163
BITCOIN_ASSERT(work.connection == connection);
164164
BITCOIN_ASSERT(work.correlation_id == sequence_);
165165

166+
auto write_error = [&work](system::code ec, uint32_t id, bool rpc)
167+
{
168+
const auto reply = rpc ? rpc::to_json(ec, id) : to_json(ec, id);
169+
work.connection->write(reply);
170+
return true;
171+
};
172+
166173
data_source istream(data_);
167174
istream_reader source(istream);
168175
const auto ec = source.read_error_code();
176+
169177
if (ec)
178+
return write_error(ec, id, connection->json_rpc());
179+
180+
socket::handler_map::const_iterator handler;
181+
182+
if (connection->json_rpc())
170183
{
171-
const auto reply = (connection->json_rpc() ? rpc::to_json(ec, id) :
172-
to_json(ec, id));
173-
work.connection->write(reply);
174-
return true;
184+
handler = rpc_handlers_.find(work.command);
185+
if (handler == rpc_handlers_.end())
186+
return write_error(system::error::not_implemented, id, true);
175187
}
176-
177-
const auto handler = connection->json_rpc() ? rpc_handlers_.find(
178-
work.command) : handlers_.find(work.command);
179-
if (handler == handlers_.end() || handler == rpc_handlers_.end())
188+
else
180189
{
181-
static constexpr auto ec = system::error::not_implemented;
182-
const auto reply = (connection->json_rpc() ? rpc::to_json(ec, id) :
183-
to_json(ec, id));
184-
work.connection->write(reply);
185-
return true;
190+
handler = handlers_.find(work.command);
191+
if (handler == handlers_.end())
192+
return write_error(system::error::not_implemented, id, false);
186193
}
187194

188195
// Decode response and send query output to websocket client.
189196
// The websocket write is performed directly (running on the
190197
// websocket thread).
191-
const auto payload = source.read_bytes();
192-
handler->second.decode(payload, id, work.connection);
198+
handler->second.decode(source.read_bytes(), id, work.connection);
193199
return true;
194200
}
195201

@@ -578,21 +584,32 @@ void socket::notify_query_work(connection_ptr connection,
578584
{
579585
LOG_VERBOSE(LOG_PROTOCOL)
580586
<< "No handlers for methods. Likely incorrect endpoint addressed.";
581-
send_error_reply(protocol_status::service_unavailable,
587+
return send_error_reply(protocol_status::service_unavailable,
582588
system::error::http_invalid_request);
583-
return;
584589
}
585590

586-
const auto handler = (rpc ? rpc_handlers_.find(method) :
587-
handlers_.find(method));
588-
if (handler == handlers_.end() || handler == rpc_handlers_.end())
591+
handler_map::const_iterator handler;
592+
593+
auto handler_not_found = [=](const std::string& method, bool rpc)
589594
{
590595
LOG_VERBOSE(LOG_PROTOCOL)
591-
<< (rpc ? "JSON-RPC method" : "Websocket method ") << method
596+
<< (rpc ? "JSON-RPC" : "Websocket") << " method" << method
592597
<< " not found";
593-
send_error_reply(protocol_status::not_found,
598+
return send_error_reply(protocol_status::not_found,
594599
system::error::http_method_not_found);
595-
return;
600+
};
601+
602+
if (rpc)
603+
{
604+
handler = rpc_handlers_.find(method);
605+
if (handler == rpc_handlers_.end())
606+
return handler_not_found(method, rpc);
607+
}
608+
else
609+
{
610+
handler = handlers_.find(method);
611+
if (handler == handlers_.end())
612+
return handler_not_found(method, rpc);
596613
}
597614

598615
auto it = work_.find(connection);

0 commit comments

Comments
 (0)