Unpack `request` and convert it into an [`http::request::Parts`], and a [`Duration`] from its `timeout` if supplied. It's very important that the error return from this function not contain any potentially sensitive data from `request`, such as the query parameters or header values. See comment on [`InstanceEnv::http_request`].
(request: st_http::Request)
| 1198 | /// such as the query parameters or header values. |
| 1199 | /// See comment on [`InstanceEnv::http_request`]. |
| 1200 | fn convert_http_request(request: st_http::Request) -> http::Result<(http::request::Parts, Option<Duration>)> { |
| 1201 | let st_http::Request { |
| 1202 | method, |
| 1203 | headers, |
| 1204 | timeout, |
| 1205 | uri, |
| 1206 | version, |
| 1207 | } = request; |
| 1208 | |
| 1209 | let (mut request, ()) = http::Request::new(()).into_parts(); |
| 1210 | request.method = match method { |
| 1211 | st_http::Method::Get => http::Method::GET, |
| 1212 | st_http::Method::Head => http::Method::HEAD, |
| 1213 | st_http::Method::Post => http::Method::POST, |
| 1214 | st_http::Method::Put => http::Method::PUT, |
| 1215 | st_http::Method::Delete => http::Method::DELETE, |
| 1216 | st_http::Method::Connect => http::Method::CONNECT, |
| 1217 | st_http::Method::Options => http::Method::OPTIONS, |
| 1218 | st_http::Method::Trace => http::Method::TRACE, |
| 1219 | st_http::Method::Patch => http::Method::PATCH, |
| 1220 | st_http::Method::Extension(method) => http::Method::from_bytes(method.as_bytes()).expect("Invalid HTTP method"), |
| 1221 | }; |
| 1222 | // The error type here, `http::uri::InvalidUri`, doesn't contain the URI itself, |
| 1223 | // so it's safe to return and to log. |
| 1224 | // See https://docs.rs/http/1.3.1/src/http/uri/mod.rs.html#120-141 . |
| 1225 | request.uri = uri.try_into()?; |
| 1226 | request.version = match version { |
| 1227 | st_http::Version::Http09 => http::Version::HTTP_09, |
| 1228 | st_http::Version::Http10 => http::Version::HTTP_10, |
| 1229 | st_http::Version::Http11 => http::Version::HTTP_11, |
| 1230 | st_http::Version::Http2 => http::Version::HTTP_2, |
| 1231 | st_http::Version::Http3 => http::Version::HTTP_3, |
| 1232 | }; |
| 1233 | request.headers = headers |
| 1234 | .into_iter() |
| 1235 | .map(|(k, v)| { |
| 1236 | Ok(( |
| 1237 | // The error type here, `http::header::InvalidHeaderName`, doesn't contain the header name itself, |
| 1238 | // so it's safe to return and to log. |
| 1239 | // See https://docs.rs/http/1.3.1/src/http/header/name.rs.html#60-63 . |
| 1240 | k.into_string().try_into()?, |
| 1241 | // The error type here, `http::header::InvalidHeaderValue`, doesn't contain the header value itself, |
| 1242 | // so it's safe to return and to log. |
| 1243 | // See https://docs.rs/http/1.3.1/src/http/header/value.rs.html#27-31 . |
| 1244 | v.into_vec().try_into()?, |
| 1245 | )) |
| 1246 | }) |
| 1247 | // Collecting into a `HeaderMap` doesn't add any new possible errors, |
| 1248 | // the `?` here is just to propogate the errors from converting the individual header names and values. |
| 1249 | // We know those are free from sensitive info, so this result is clean. |
| 1250 | .collect::<http::Result<_>>()?; |
| 1251 | |
| 1252 | let timeout = timeout.map(|d| d.to_duration_saturating()); |
| 1253 | |
| 1254 | Ok((request, timeout)) |
| 1255 | } |
| 1256 | |
| 1257 | fn convert_http_response(response: http::response::Parts) -> st_http::Response { |
no test coverage detected
searching dependent graphs…