Skip to content

Commit 639c55d

Browse files
committed
fix query param parsing
1 parent 6b100ef commit 639c55d

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

stackslib/src/net/api/gethealth.rs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
use std::fmt;
18+
use std::str::FromStr;
19+
1720
use regex::{Captures, Regex};
1821
use stacks_common::types::net::PeerHost;
1922
use stacks_common::types::StacksEpochId;
@@ -45,13 +48,40 @@ pub struct RPCGetHealthResponse {
4548

4649
const NEIGHBORS_SCOPE_PARAM_NAME: &str = "neighbors";
4750

48-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
49-
#[serde(rename_all = "lowercase")]
51+
#[derive(Clone, Debug, PartialEq)]
5052
pub enum NeighborsScope {
5153
Initial,
5254
All,
5355
}
5456

57+
impl FromStr for NeighborsScope {
58+
type Err = crate::net::http::Error;
59+
60+
fn from_str(s: &str) -> Result<Self, Self::Err> {
61+
match s {
62+
"initial" => Ok(NeighborsScope::Initial),
63+
"all" => Ok(NeighborsScope::All),
64+
_ => Err(crate::net::http::Error::Http(
65+
400,
66+
format!(
67+
"Invalid `neighbors` query parameter: `{}`, allowed values are `initial` or `all`",
68+
s
69+
),
70+
)),
71+
}
72+
}
73+
}
74+
75+
impl fmt::Display for NeighborsScope {
76+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77+
let s = match self {
78+
NeighborsScope::Initial => "initial",
79+
NeighborsScope::All => "all",
80+
};
81+
write!(f, "{s}")
82+
}
83+
}
84+
5585
#[derive(Clone)]
5686
/// Empty request handler for the GET /v3/health endpoint
5787
pub struct RPCGetHealthRequestHandler {
@@ -97,9 +127,7 @@ impl HttpRequest for RPCGetHealthRequestHandler {
97127

98128
let req_contents = HttpRequestContents::new().query_string(query);
99129
if let Some(scope) = req_contents.get_query_arg(NEIGHBORS_SCOPE_PARAM_NAME) {
100-
self.neighbors_scope = Some(serde_json::from_str(scope.as_str()).map_err(|_e| {
101-
Error::Http(400, format!("Invalid `neighbors` query parameter: `{}`, allowed values are `initial` or `all`", scope))
102-
})?);
130+
self.neighbors_scope = Some(scope.parse()?);
103131
}
104132

105133
Ok(req_contents)
@@ -243,7 +271,7 @@ impl StacksHttpRequest {
243271
format!("/v3/health"),
244272
HttpRequestContents::new().query_arg(
245273
NEIGHBORS_SCOPE_PARAM_NAME.into(),
246-
serde_json::to_string(&neighbors_scope).unwrap(),
274+
neighbors_scope.to_string(),
247275
),
248276
)
249277
.expect("FATAL: failed to construct request from infallible data")

0 commit comments

Comments
 (0)