Skip to content

Commit f012a85

Browse files
committed
rest.cpp: change HTTP_INTERNAL_SERVER_ERROR to HTTP_BAD_REQUEST
1 parent 84decb5 commit f012a85

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

doc/release-notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ git merge commit are mentioned.
5959

6060
### RPC and REST
6161

62+
UTXO set query (`GET /rest/getutxos/<checkmempool>/<txid>-<n>/<txid>-<n>/.../<txid>-<n>.<bin|hex|json>`) responses
63+
were changed to return status code HTTP_BAD_REQUEST (400) instead of HTTP_INTERNAL_SERVER_ERROR (500) when requests
64+
contain invalid parameters.
65+
6266
### Configuration and command-line options
6367

6468
### Block and transaction handling

qa/rpc-tests/rest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,29 @@ def run_test(self):
179179
#do some invalid requests
180180
json_request = '{"checkmempool'
181181
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'json', json_request, True)
182-
assert_equal(response.status, 500) #must be a 500 because we send a invalid json request
182+
assert_equal(response.status, 400) #must be a 400 because we send a invalid json request
183183

184184
json_request = '{"checkmempool'
185185
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+self.FORMAT_SEPARATOR+'bin', json_request, True)
186-
assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request
186+
assert_equal(response.status, 400) #must be a 400 because we send a invalid bin request
187187

188188
response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool'+self.FORMAT_SEPARATOR+'bin', '', True)
189-
assert_equal(response.status, 500) #must be a 500 because we send a invalid bin request
189+
assert_equal(response.status, 400) #must be a 400 because we send a invalid bin request
190190

191191
#test limits
192192
json_request = '/checkmempool/'
193193
for x in range(0, 20):
194194
json_request += txid+'-'+str(n)+'/'
195195
json_request = json_request.rstrip("/")
196196
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
197-
assert_equal(response.status, 500) #must be a 500 because we exceeding the limits
197+
assert_equal(response.status, 400) #must be a 400 because we exceeding the limits
198198

199199
json_request = '/checkmempool/'
200200
for x in range(0, 15):
201201
json_request += txid+'-'+str(n)+'/'
202202
json_request = json_request.rstrip("/")
203203
response = http_post_call(url.hostname, url.port, '/rest/getutxos'+json_request+self.FORMAT_SEPARATOR+'json', '', True)
204-
assert_equal(response.status, 200) #must be a 500 because we exceeding the limits
204+
assert_equal(response.status, 200) #must be a 200 because we are within the limits
205205

206206
self.nodes[0].generate(1) #generate block to not affect upcoming tests
207207
self.sync_all()

src/rest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
420420
// throw exception in case of a empty request
421421
std::string strRequestMutable = req->ReadBody();
422422
if (strRequestMutable.length() == 0 && uriParts.size() == 0)
423-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
423+
return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
424424

425425
bool fInputParsed = false;
426426
bool fCheckMemPool = false;
@@ -444,7 +444,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
444444
std::string strOutput = uriParts[i].substr(uriParts[i].find("-")+1);
445445

446446
if (!ParseInt32(strOutput, &nOutput) || !IsHex(strTxid))
447-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Parse error");
447+
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
448448

449449
txid.SetHex(strTxid);
450450
vOutPoints.push_back(COutPoint(txid, (uint32_t)nOutput));
@@ -453,7 +453,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
453453
if (vOutPoints.size() > 0)
454454
fInputParsed = true;
455455
else
456-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
456+
return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
457457
}
458458

459459
switch (rf) {
@@ -469,7 +469,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
469469
if (strRequestMutable.size() > 0)
470470
{
471471
if (fInputParsed) //don't allow sending input over URI and HTTP RAW DATA
472-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Combination of URI scheme inputs and raw post data is not allowed");
472+
return RESTERR(req, HTTP_BAD_REQUEST, "Combination of URI scheme inputs and raw post data is not allowed");
473473

474474
CDataStream oss(SER_NETWORK, PROTOCOL_VERSION);
475475
oss << strRequestMutable;
@@ -478,14 +478,14 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
478478
}
479479
} catch (const std::ios_base::failure& e) {
480480
// abort in case of unreadable binary data
481-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Parse error");
481+
return RESTERR(req, HTTP_BAD_REQUEST, "Parse error");
482482
}
483483
break;
484484
}
485485

486486
case RF_JSON: {
487487
if (!fInputParsed)
488-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, "Error: empty request");
488+
return RESTERR(req, HTTP_BAD_REQUEST, "Error: empty request");
489489
break;
490490
}
491491
default: {
@@ -495,7 +495,7 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
495495

496496
// limit max outpoints
497497
if (vOutPoints.size() > MAX_GETUTXOS_OUTPOINTS)
498-
return RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
498+
return RESTERR(req, HTTP_BAD_REQUEST, strprintf("Error: max outpoints exceeded (max: %d, tried: %d)", MAX_GETUTXOS_OUTPOINTS, vOutPoints.size()));
499499

500500
// check spentness and form a bitmap (as well as a JSON capable human-readable string representation)
501501
vector<unsigned char> bitmap;

0 commit comments

Comments
 (0)