@@ -185,7 +185,7 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
185
185
// Set the URI
186
186
jreq.URI = req->GetURI ();
187
187
188
- std::string strReply ;
188
+ UniValue reply ;
189
189
bool user_has_whitelist = g_rpc_whitelist.count (jreq.authUser );
190
190
if (!user_has_whitelist && g_rpc_whitelist_default) {
191
191
LogPrintf (" RPC User %s not allowed to call any methods\n " , jreq.authUser );
@@ -200,13 +200,12 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
200
200
req->WriteReply (HTTP_FORBIDDEN);
201
201
return false ;
202
202
}
203
- UniValue result = tableRPC.execute (jreq);
204
203
205
- // Send reply
206
- strReply = JSONRPCReplyObj (std::move (result), NullUniValue, jreq.id ).write () + " \n " ;
204
+ reply = JSONRPCExec (jreq);
207
205
208
206
// array of requests
209
207
} else if (valRequest.isArray ()) {
208
+ // Check authorization for each request's method
210
209
if (user_has_whitelist) {
211
210
for (unsigned int reqIdx = 0 ; reqIdx < valRequest.size (); reqIdx++) {
212
211
if (!valRequest[reqIdx].isObject ()) {
@@ -223,13 +222,26 @@ static bool HTTPReq_JSONRPC(const std::any& context, HTTPRequest* req)
223
222
}
224
223
}
225
224
}
226
- strReply = JSONRPCExecBatch (jreq, valRequest.get_array ());
225
+
226
+ // Execute each request
227
+ reply = UniValue::VARR;
228
+ for (size_t i{0 }; i < valRequest.size (); ++i) {
229
+ // Batches include errors in the batch response, they do not throw
230
+ try {
231
+ jreq.parse (valRequest[i]);
232
+ reply.push_back (JSONRPCExec (jreq));
233
+ } catch (UniValue& e) {
234
+ reply.push_back (JSONRPCReplyObj (NullUniValue, std::move (e), jreq.id ));
235
+ } catch (const std::exception& e) {
236
+ reply.push_back (JSONRPCReplyObj (NullUniValue, JSONRPCError (RPC_PARSE_ERROR, e.what ()), jreq.id ));
237
+ }
238
+ }
227
239
}
228
240
else
229
241
throw JSONRPCError (RPC_PARSE_ERROR, " Top-level object parse error" );
230
242
231
243
req->WriteHeader (" Content-Type" , " application/json" );
232
- req->WriteReply (HTTP_OK, strReply );
244
+ req->WriteReply (HTTP_OK, reply. write () + " \n " );
233
245
} catch (UniValue& e) {
234
246
JSONErrorReply (req, std::move (e), jreq.id );
235
247
return false ;
0 commit comments