Skip to content

Commit 91b6d27

Browse files
committed
Add function to deserialize query strings
1 parent bdac6bd commit 91b6d27

File tree

1 file changed

+46
-36
lines changed

1 file changed

+46
-36
lines changed

site/src/server.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
284284
return Ok(response);
285285
}
286286

287+
macro_rules! check {
288+
($e:expr) => {
289+
match $e {
290+
Ok(v) => v,
291+
Err(e) => return Ok(e),
292+
}
293+
};
294+
}
295+
287296
match path {
288297
"/perf/info" => return server.handle_get(&req, request_handlers::handle_info),
289298
"/perf/dashboard" => {
@@ -303,25 +312,7 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
303312
}
304313
"/perf/triage" => {
305314
let input: triage::Request = if *req.method() == http::Method::GET {
306-
let url = url::Url::parse(&format!("http://example.com{}", req.uri())).unwrap();
307-
let parts = url
308-
.query_pairs()
309-
.into_owned()
310-
.collect::<HashMap<String, String>>();
311-
match serde_json::from_str(&serde_json::to_string(&parts).unwrap()) {
312-
Ok(v) => v,
313-
Err(e) => {
314-
return Ok(http::Response::builder()
315-
.header_typed(ContentType::text_utf8())
316-
.status(StatusCode::BAD_REQUEST)
317-
.body(hyper::Body::from(format!(
318-
"failed to deserialize request {}: {:?}",
319-
req.uri(),
320-
e,
321-
)))
322-
.unwrap());
323-
}
324-
}
315+
check!(parse_query_string(req.uri()))
325316
} else if *req.method() == http::Method::POST {
326317
let mut body = Vec::new();
327318
let (_req, mut body_stream) = req.into_parts();
@@ -406,21 +397,12 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
406397
}
407398
}
408399

409-
macro_rules! body {
410-
($e:expr) => {
411-
match $e {
412-
Ok(v) => v,
413-
Err(e) => return Ok(e),
414-
}
415-
};
416-
}
417-
418400
match path {
419401
"/perf/graph" => Ok(to_response(
420-
request_handlers::handle_graph(body!(parse_body(&body)), &ctxt).await,
402+
request_handlers::handle_graph(check!(parse_body(&body)), &ctxt).await,
421403
)),
422404
"/perf/get" => Ok(to_response(
423-
crate::comparison::handle_compare(body!(parse_body(&body)), &ctxt)
405+
crate::comparison::handle_compare(check!(parse_body(&body)), &ctxt)
424406
.await
425407
.map_err(|e| e.to_string()),
426408
)),
@@ -453,7 +435,7 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
453435
};
454436
match event.as_str() {
455437
"issue_comment" => Ok(to_response(
456-
request_handlers::handle_github(body!(parse_body(&body)), ctxt.clone()).await,
438+
request_handlers::handle_github(check!(parse_body(&body)), ctxt.clone()).await,
457439
)),
458440
_ => Ok(http::Response::builder()
459441
.status(StatusCode::OK)
@@ -462,13 +444,13 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
462444
}
463445
}
464446
"/perf/self-profile" => Ok(to_response(
465-
request_handlers::handle_self_profile(body!(parse_body(&body)), &ctxt).await,
447+
request_handlers::handle_self_profile(check!(parse_body(&body)), &ctxt).await,
466448
)),
467449
"/perf/self-profile-raw" => Ok(to_response(
468-
request_handlers::handle_self_profile_raw(body!(parse_body(&body)), &ctxt).await,
450+
request_handlers::handle_self_profile_raw(check!(parse_body(&body)), &ctxt).await,
469451
)),
470452
"/perf/graph-new" => Ok(
471-
match request_handlers::handle_graph_new(body!(parse_body(&body)), &ctxt).await {
453+
match request_handlers::handle_graph_new(check!(parse_body(&body)), &ctxt).await {
472454
Ok(result) => {
473455
let mut response = http::Response::builder()
474456
.header_typed(ContentType::json())
@@ -489,7 +471,7 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
489471
},
490472
),
491473
"/perf/bootstrap" => Ok(
492-
match request_handlers::handle_bootstrap(body!(parse_body(&body)), &ctxt).await {
474+
match request_handlers::handle_bootstrap(check!(parse_body(&body)), &ctxt).await {
493475
Ok(result) => {
494476
let mut response = http::Response::builder()
495477
.header_typed(ContentType::json())
@@ -533,14 +515,42 @@ where
533515
.header_typed(ContentType::text_utf8())
534516
.status(StatusCode::BAD_REQUEST)
535517
.body(hyper::Body::from(format!(
536-
"Failed to deserialize request; {:?}",
518+
"Failed to deserialize request: {:?}",
537519
err
538520
)))
539521
.unwrap());
540522
}
541523
}
542524
}
543525

526+
fn parse_query_string<D>(uri: &http::Uri) -> Result<D, Response>
527+
where
528+
D: DeserializeOwned,
529+
{
530+
let params: HashMap<String, String> = uri
531+
.query()
532+
.map(|v| {
533+
url::form_urlencoded::parse(v.as_bytes())
534+
.into_owned()
535+
.collect()
536+
})
537+
.unwrap_or_else(HashMap::new);
538+
539+
match serde_json::from_str(&serde_json::to_string(&params).unwrap()) {
540+
Ok(d) => Ok(d),
541+
Err(err) => {
542+
return Err(http::Response::builder()
543+
.header_typed(ContentType::text_utf8())
544+
.status(StatusCode::BAD_REQUEST)
545+
.body(hyper::Body::from(format!(
546+
"Failed to deserialize request {}: {:?}",
547+
uri, err,
548+
)))
549+
.unwrap());
550+
}
551+
}
552+
}
553+
544554
/// Handle the case where the path is to a static file
545555
fn handle_fs_path(path: &str) -> Option<http::Response<hyper::Body>> {
546556
let fs_path = format!(

0 commit comments

Comments
 (0)