@@ -52,8 +52,8 @@ class task_sender
52
52
auto narrow = [](int32_t & out, size_t in)
53
53
{
54
54
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;
57
57
};
58
58
59
59
int32_t data_size;
@@ -163,33 +163,39 @@ class query_response_task_sender
163
163
BITCOIN_ASSERT (work.connection == connection);
164
164
BITCOIN_ASSERT (work.correlation_id == sequence_);
165
165
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
+
166
173
data_source istream (data_);
167
174
istream_reader source (istream);
168
175
const auto ec = source.read_error_code ();
176
+
169
177
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 ())
170
183
{
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 );
175
187
}
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
180
189
{
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 );
186
193
}
187
194
188
195
// Decode response and send query output to websocket client.
189
196
// The websocket write is performed directly (running on the
190
197
// 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 );
193
199
return true ;
194
200
}
195
201
@@ -578,21 +584,32 @@ void socket::notify_query_work(connection_ptr connection,
578
584
{
579
585
LOG_VERBOSE (LOG_PROTOCOL)
580
586
<< " 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,
582
588
system::error::http_invalid_request);
583
- return ;
584
589
}
585
590
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 )
589
594
{
590
595
LOG_VERBOSE (LOG_PROTOCOL)
591
- << (rpc ? " JSON-RPC method " : " Websocket method " ) << method
596
+ << (rpc ? " JSON-RPC" : " Websocket" ) << " method " << method
592
597
<< " not found" ;
593
- send_error_reply (protocol_status::not_found,
598
+ return send_error_reply (protocol_status::not_found,
594
599
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);
596
613
}
597
614
598
615
auto it = work_.find (connection);
0 commit comments