Sends an HTTP request to the supervisor. # Arguments `method` - The HTTP method to use. `uri` - The URI to send the request to. Supports Unix sockets: `unix:/path/to/socket` or HTTP: `http://host:port`. `body` - The body of the request.
(
method: &str,
base: &str,
path: &str,
body: &[u8],
)
| 30 | /// * `uri` - The URI to send the request to. Supports Unix sockets: `unix:/path/to/socket` or HTTP: `http://host:port`. |
| 31 | /// * `body` - The body of the request. |
| 32 | pub async fn http_request( |
| 33 | method: &str, |
| 34 | base: &str, |
| 35 | path: &str, |
| 36 | body: &[u8], |
| 37 | ) -> Result<(u16, Vec<u8>)> { |
| 38 | debug!("Sending HTTP request to {base}, path={path}"); |
| 39 | let mut response = if let Some(uds) = base.strip_prefix("unix:") { |
| 40 | let path = if path.starts_with("/") { |
| 41 | path.to_string() |
| 42 | } else { |
| 43 | format!("/{path}") |
| 44 | }; |
| 45 | let client: Client<UnixConnector, Full<Bytes>> = Client::unix(); |
| 46 | let unix_uri: hyper::Uri = Uri::new(uds, &path).into(); |
| 47 | let req = Request::builder() |
| 48 | .method(method) |
| 49 | .uri(unix_uri) |
| 50 | .body(Full::new(Bytes::copy_from_slice(body)))?; |
| 51 | client.request(req).await? |
| 52 | } else if base.starts_with("vsock:") { |
| 53 | let client = Client::vsock(); |
| 54 | let uri = mk_url(base, path).parse::<hyper::Uri>()?; |
| 55 | let req = Request::builder() |
| 56 | .method(method) |
| 57 | .uri(uri) |
| 58 | .body(Full::new(Bytes::copy_from_slice(body)))?; |
| 59 | client.request(req).await? |
| 60 | } else { |
| 61 | let uri = mk_url(base, path); |
| 62 | let client = reqwest::Client::builder().build()?; |
| 63 | let response = client.post(uri).body(body.to_vec()).send().await?; |
| 64 | return Ok(( |
| 65 | response.status().as_u16(), |
| 66 | response.text().await?.into_bytes(), |
| 67 | )); |
| 68 | }; |
| 69 | debug!("Response: {:?}", response); |
| 70 | let mut body = Vec::new(); |
| 71 | while let Some(frame_result) = response.frame().await { |
| 72 | let frame = frame_result?; |
| 73 | if let Some(segment) = frame.data_ref() { |
| 74 | body.extend_from_slice(segment.iter().as_slice()); |
| 75 | } |
| 76 | } |
| 77 | Ok((response.status().as_u16(), body)) |
| 78 | } |
| 79 | |
| 80 | #[cfg(test)] |
| 81 | mod tests { |