@@ -18,21 +18,24 @@ using namespace std;
18
18
using namespace json_spirit ;
19
19
20
20
enum RetFormat {
21
+ RF_UNDEF,
21
22
RF_BINARY,
22
23
RF_HEX,
23
24
RF_JSON,
24
25
};
25
26
26
27
static const struct {
27
28
enum RetFormat rf;
28
- const char * name;
29
+ const char * name;
29
30
} rf_names[] = {
30
- { RF_BINARY, " binary" }, // default, if match not found
31
- { RF_HEX, " hex" },
32
- { RF_JSON, " json" },
31
+ {RF_UNDEF, " " },
32
+ {RF_BINARY, " bin" },
33
+ {RF_HEX, " hex" },
34
+ {RF_JSON, " json" },
33
35
};
34
36
35
- class RestErr {
37
+ class RestErr
38
+ {
36
39
public:
37
40
enum HTTPStatusCode status;
38
41
string message;
@@ -49,15 +52,34 @@ static RestErr RESTERR(enum HTTPStatusCode status, string message)
49
52
return re;
50
53
}
51
54
52
- static enum RetFormat ParseDataFormat (const string& format )
55
+ static enum RetFormat ParseDataFormat (vector<string>& params, const string strReq )
53
56
{
54
- for (unsigned int i = 0 ; i < ARRAYLEN (rf_names); i++)
55
- if (format == rf_names[i].name )
56
- return rf_names[i].rf ;
57
+ boost::split (params, strReq, boost::is_any_of (" ." ));
58
+ if (params.size () > 1 ) {
59
+ for (unsigned int i = 0 ; i < ARRAYLEN (rf_names); i++)
60
+ if (params[1 ] == rf_names[i].name )
61
+ return rf_names[i].rf ;
62
+ }
57
63
58
64
return rf_names[0 ].rf ;
59
65
}
60
66
67
+ static string AvailableDataFormatsString ()
68
+ {
69
+ string formats = " " ;
70
+ for (unsigned int i = 0 ; i < ARRAYLEN (rf_names); i++)
71
+ if (strlen (rf_names[i].name ) > 0 ) {
72
+ formats.append (" ." );
73
+ formats.append (rf_names[i].name );
74
+ formats.append (" , " );
75
+ }
76
+
77
+ if (formats.length () > 0 )
78
+ return formats.substr (0 , formats.length () - 2 );
79
+
80
+ return formats;
81
+ }
82
+
61
83
static bool ParseHashStr (const string& strReq, uint256& v)
62
84
{
63
85
if (!IsHex (strReq) || (strReq.size () != 64 ))
@@ -67,15 +89,13 @@ static bool ParseHashStr(const string& strReq, uint256& v)
67
89
return true ;
68
90
}
69
91
70
- static bool rest_block (AcceptedConnection * conn,
92
+ static bool rest_block (AcceptedConnection* conn,
71
93
string& strReq,
72
94
map<string, string>& mapHeaders,
73
95
bool fRun )
74
96
{
75
97
vector<string> params;
76
- boost::split (params, strReq, boost::is_any_of (" /" ));
77
-
78
- enum RetFormat rf = ParseDataFormat (params.size () > 1 ? params[1 ] : string (" " ));
98
+ enum RetFormat rf = ParseDataFormat (params, strReq);
79
99
80
100
string hashStr = params[0 ];
81
101
uint256 hash;
@@ -105,7 +125,7 @@ static bool rest_block(AcceptedConnection *conn,
105
125
}
106
126
107
127
case RF_HEX: {
108
- string strHex = HexStr (ssBlock.begin (), ssBlock.end ()) + " \n " ;;
128
+ string strHex = HexStr (ssBlock.begin (), ssBlock.end ()) + " \n " ;
109
129
conn->stream () << HTTPReply (HTTP_OK, strHex, fRun , false , " text/plain" ) << std::flush;
110
130
return true ;
111
131
}
@@ -115,22 +135,24 @@ static bool rest_block(AcceptedConnection *conn,
115
135
string strJSON = write_string (Value (objBlock), false ) + " \n " ;
116
136
conn->stream () << HTTPReply (HTTP_OK, strJSON, fRun ) << std::flush;
117
137
return true ;
118
- }
138
+ }
139
+
140
+ default : {
141
+ throw RESTERR (HTTP_NOT_FOUND, " output format not found (available: " + AvailableDataFormatsString () + " )" );
142
+ }
119
143
}
120
144
121
145
// not reached
122
- return true ; // continue to process further HTTP reqs on this cxn
146
+ return true ; // continue to process further HTTP reqs on this cxn
123
147
}
124
148
125
- static bool rest_tx (AcceptedConnection * conn,
149
+ static bool rest_tx (AcceptedConnection* conn,
126
150
string& strReq,
127
151
map<string, string>& mapHeaders,
128
152
bool fRun )
129
153
{
130
154
vector<string> params;
131
- boost::split (params, strReq, boost::is_any_of (" /" ));
132
-
133
- enum RetFormat rf = ParseDataFormat (params.size () > 1 ? params[1 ] : string (" " ));
155
+ enum RetFormat rf = ParseDataFormat (params, strReq);
134
156
135
157
string hashStr = params[0 ];
136
158
uint256 hash;
@@ -153,7 +175,7 @@ static bool rest_tx(AcceptedConnection *conn,
153
175
}
154
176
155
177
case RF_HEX: {
156
- string strHex = HexStr (ssTx.begin (), ssTx.end ()) + " \n " ;;
178
+ string strHex = HexStr (ssTx.begin (), ssTx.end ()) + " \n " ;
157
179
conn->stream () << HTTPReply (HTTP_OK, strHex, fRun , false , " text/plain" ) << std::flush;
158
180
return true ;
159
181
}
@@ -165,42 +187,45 @@ static bool rest_tx(AcceptedConnection *conn,
165
187
conn->stream () << HTTPReply (HTTP_OK, strJSON, fRun ) << std::flush;
166
188
return true ;
167
189
}
190
+
191
+ default : {
192
+ throw RESTERR (HTTP_NOT_FOUND, " output format not found (available: " + AvailableDataFormatsString () + " )" );
193
+ }
168
194
}
169
195
170
196
// not reached
171
- return true ; // continue to process further HTTP reqs on this cxn
197
+ return true ; // continue to process further HTTP reqs on this cxn
172
198
}
173
199
174
200
static const struct {
175
- const char * prefix;
176
- bool (*handler)(AcceptedConnection * conn,
201
+ const char * prefix;
202
+ bool (*handler)(AcceptedConnection* conn,
177
203
string& strURI,
178
204
map<string, string>& mapHeaders,
179
205
bool fRun );
180
206
} uri_prefixes[] = {
181
- { " /rest/tx/" , rest_tx },
182
- { " /rest/block/" , rest_block },
207
+ { " /rest/tx/" , rest_tx},
208
+ { " /rest/block/" , rest_block},
183
209
};
184
210
185
- bool HTTPReq_REST (AcceptedConnection * conn,
211
+ bool HTTPReq_REST (AcceptedConnection* conn,
186
212
string& strURI,
187
213
map<string, string>& mapHeaders,
188
214
bool fRun )
189
215
{
190
216
try {
191
217
std::string statusmessage;
192
- if (RPCIsInWarmup (&statusmessage))
193
- throw RESTERR (HTTP_SERVICE_UNAVAILABLE, " Service temporarily unavailable: " + statusmessage);
194
-
218
+ if (RPCIsInWarmup (&statusmessage))
219
+ throw RESTERR (HTTP_SERVICE_UNAVAILABLE, " Service temporarily unavailable: " + statusmessage);
220
+
195
221
for (unsigned int i = 0 ; i < ARRAYLEN (uri_prefixes); i++) {
196
222
unsigned int plen = strlen (uri_prefixes[i].prefix );
197
223
if (strURI.substr (0 , plen) == uri_prefixes[i].prefix ) {
198
224
string strReq = strURI.substr (plen);
199
225
return uri_prefixes[i].handler (conn, strReq, mapHeaders, fRun );
200
226
}
201
227
}
202
- }
203
- catch (RestErr& re) {
228
+ } catch (RestErr& re) {
204
229
conn->stream () << HTTPReply (re.status , re.message + " \r\n " , false , false , " text/plain" ) << std::flush;
205
230
return false ;
206
231
}
0 commit comments