Skip to content

Commit 9cdc335

Browse files
committed
Simplify logic of REST request suffix parsing.
This patch changes the way the suffix (giving the requested data format) is parsed for REST requests. Before, the string was split at '.' characters and it was assumed that the second part was the suffix. Now, we look for the last dot and use that to determine the suffix. This allows for strings that contain dots (not used now, though), and seems, in general, to be clearer and more intuitive.
1 parent 536207f commit 9cdc335

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/rest.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,24 @@ static bool RESTERR(HTTPRequest* req, enum HTTPStatusCode status, string message
7171
return false;
7272
}
7373

74-
static enum RetFormat ParseDataFormat(vector<string>& params, const string& strReq)
74+
static enum RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
7575
{
76-
boost::split(params, strReq, boost::is_any_of("."));
77-
if (params.size() > 1) {
78-
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
79-
if (params[1] == rf_names[i].name)
80-
return rf_names[i].rf;
76+
const std::string::size_type pos = strReq.rfind('.');
77+
if (pos == std::string::npos)
78+
{
79+
param = strReq;
80+
return rf_names[0].rf;
8181
}
8282

83+
param = strReq.substr(0, pos);
84+
const std::string suff(strReq, pos + 1);
85+
86+
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
87+
if (suff == rf_names[i].name)
88+
return rf_names[i].rf;
89+
90+
/* If no suffix is found, return original string. */
91+
param = strReq;
8392
return rf_names[0].rf;
8493
}
8594

@@ -121,10 +130,10 @@ static bool rest_headers(HTTPRequest* req,
121130
{
122131
if (!CheckWarmup(req))
123132
return false;
124-
vector<string> params;
125-
const RetFormat rf = ParseDataFormat(params, strURIPart);
133+
std::string param;
134+
const RetFormat rf = ParseDataFormat(param, strURIPart);
126135
vector<string> path;
127-
boost::split(path, params[0], boost::is_any_of("/"));
136+
boost::split(path, param, boost::is_any_of("/"));
128137

129138
if (path.size() != 2)
130139
return RESTERR(req, HTTP_BAD_REQUEST, "No header count specified. Use /rest/headers/<count>/<hash>.<ext>.");
@@ -196,10 +205,9 @@ static bool rest_block(HTTPRequest* req,
196205
{
197206
if (!CheckWarmup(req))
198207
return false;
199-
vector<string> params;
200-
const RetFormat rf = ParseDataFormat(params, strURIPart);
208+
std::string hashStr;
209+
const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
201210

202-
string hashStr = params[0];
203211
uint256 hash;
204212
if (!ParseHashStr(hashStr, hash))
205213
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
@@ -268,8 +276,8 @@ static bool rest_chaininfo(HTTPRequest* req, const std::string& strURIPart)
268276
{
269277
if (!CheckWarmup(req))
270278
return false;
271-
vector<string> params;
272-
const RetFormat rf = ParseDataFormat(params, strURIPart);
279+
std::string param;
280+
const RetFormat rf = ParseDataFormat(param, strURIPart);
273281

274282
switch (rf) {
275283
case RF_JSON: {
@@ -293,8 +301,8 @@ static bool rest_mempool_info(HTTPRequest* req, const std::string& strURIPart)
293301
{
294302
if (!CheckWarmup(req))
295303
return false;
296-
vector<string> params;
297-
const RetFormat rf = ParseDataFormat(params, strURIPart);
304+
std::string param;
305+
const RetFormat rf = ParseDataFormat(param, strURIPart);
298306

299307
switch (rf) {
300308
case RF_JSON: {
@@ -318,8 +326,8 @@ static bool rest_mempool_contents(HTTPRequest* req, const std::string& strURIPar
318326
{
319327
if (!CheckWarmup(req))
320328
return false;
321-
vector<string> params;
322-
const RetFormat rf = ParseDataFormat(params, strURIPart);
329+
std::string param;
330+
const RetFormat rf = ParseDataFormat(param, strURIPart);
323331

324332
switch (rf) {
325333
case RF_JSON: {
@@ -343,10 +351,9 @@ static bool rest_tx(HTTPRequest* req, const std::string& strURIPart)
343351
{
344352
if (!CheckWarmup(req))
345353
return false;
346-
vector<string> params;
347-
const RetFormat rf = ParseDataFormat(params, strURIPart);
354+
std::string hashStr;
355+
const RetFormat rf = ParseDataFormat(hashStr, strURIPart);
348356

349-
string hashStr = params[0];
350357
uint256 hash;
351358
if (!ParseHashStr(hashStr, hash))
352359
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
@@ -396,13 +403,13 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
396403
{
397404
if (!CheckWarmup(req))
398405
return false;
399-
vector<string> params;
400-
enum RetFormat rf = ParseDataFormat(params, strURIPart);
406+
std::string param;
407+
const RetFormat rf = ParseDataFormat(param, strURIPart);
401408

402409
vector<string> uriParts;
403-
if (params.size() > 0 && params[0].length() > 1)
410+
if (param.length() > 1)
404411
{
405-
std::string strUriParams = params[0].substr(1);
412+
std::string strUriParams = param.substr(1);
406413
boost::split(uriParts, strUriParams, boost::is_any_of("/"));
407414
}
408415

0 commit comments

Comments
 (0)