Skip to content

Commit e9b150b

Browse files
committed
Convert HTTP method handling to match
1 parent f40cabd commit e9b150b

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

trino-lb/src/http_server/v1/statement.rs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ pub enum Error {
133133
requested_path: String,
134134
trino_endpoint: Url,
135135
},
136+
137+
#[snafu(display("Unexpected HTTP method {actual}, expected on of {expected:?}"))]
138+
UnexpectedHttpMethod {
139+
actual: http::Method,
140+
expected: Vec<http::Method>,
141+
},
136142
}
137143

138144
impl IntoResponse for Error {
@@ -236,33 +242,43 @@ pub async fn get_trino_executing_statement(
236242
Path((query_id, _, _)): Path<(TrinoQueryId, String, u64)>,
237243
uri: Uri,
238244
) -> Result<Response, Error> {
239-
if method == http::Method::HEAD {
240-
state.metrics.http_counter.add(
241-
1,
242-
&[KeyValue::new("resource", "head_trino_executing_statement")],
243-
);
245+
match method {
246+
http::Method::GET => {
247+
state.metrics.http_counter.add(
248+
1,
249+
&[KeyValue::new("resource", "get_trino_executing_statement")],
250+
);
244251

245-
let headers = handle_head_request_to_trino(&state, headers, query_id, uri.path()).await?;
252+
let (headers, body) =
253+
handle_query_running_on_trino(&state, headers, query_id, uri.path()).await?;
246254

247-
// For a HEAD request we don't need (nor can) return a body.
248-
let mut response = Response::new(Body::empty());
249-
*response.status_mut() = StatusCode::OK;
250-
*response.headers_mut() = headers;
251-
return Ok(response);
252-
}
253-
state.metrics.http_counter.add(
254-
1,
255-
&[KeyValue::new("resource", "get_trino_executing_statement")],
256-
);
255+
let mut response = body.into_response();
256+
// We can not simply replace the headers, as otherwise e.g. "Content-Type: application/json"
257+
// would be missing
258+
response.headers_mut().extend(headers);
259+
Ok(response)
260+
}
261+
http::Method::HEAD => {
262+
state.metrics.http_counter.add(
263+
1,
264+
&[KeyValue::new("resource", "head_trino_executing_statement")],
265+
);
257266

258-
let (headers, body) =
259-
handle_query_running_on_trino(&state, headers, query_id, uri.path()).await?;
267+
let headers =
268+
handle_head_request_to_trino(&state, headers, query_id, uri.path()).await?;
260269

261-
let mut response = body.into_response();
262-
// We can not simply replace the headers, as otherwise e.g. "Content-Type: application/json"
263-
// would be missing
264-
response.headers_mut().extend(headers);
265-
Ok(response)
270+
// For a HEAD request we don't need (nor can) return a body.
271+
let mut response = Response::new(Body::empty());
272+
*response.status_mut() = StatusCode::OK;
273+
*response.headers_mut() = headers;
274+
Ok(response)
275+
}
276+
_ => UnexpectedHttpMethodSnafu {
277+
actual: method,
278+
expected: vec![http::Method::GET, http::Method::HEAD],
279+
}
280+
.fail(),
281+
}
266282
}
267283

268284
#[instrument(skip(

0 commit comments

Comments
 (0)