Skip to content

Commit

Permalink
custom implementation of post_empty()
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Aug 27, 2024
1 parent f364f1f commit 0a62b26
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ pub async fn read_url_blob(context: &Context, url: &str) -> Result<Response> {
.body(http_body_util::Empty::<bytes::Bytes>::new())?;
let response = sender.send_request(req).await?;

let headers = response.headers();
if response.status().is_redirection() {
let header = headers
let header = response
.headers()
.get_all("location")
.iter()
.last()
Expand Down Expand Up @@ -226,27 +226,47 @@ pub(crate) async fn get_client(context: &Context, load_cache: bool) -> Result<re
///
/// Returns response text and whether request was successful or not.
pub(crate) async fn post_empty(context: &Context, url: &str) -> Result<(String, bool)> {
let parsed_url = url
.parse::<hyper::Uri>()
.with_context(|| format!("Failed to parse URL {url:?}"))?;
let scheme = parsed_url.scheme_str().context("URL has no scheme")?;
if scheme != "https" {
bail!("POST requests to non-HTTPS URLs are not allowed");
let mut url = url.to_string();

for _i in 0..10 {
let parsed_url = url
.parse::<hyper::Uri>()
.with_context(|| format!("Failed to parse URL {url:?}"))?;
let scheme = parsed_url.scheme_str().context("URL has no scheme")?;
if scheme != "https" {
bail!("POST requests to non-HTTPS URLs are not allowed");
}

let mut sender = get_http_sender(context, parsed_url.clone()).await?;
let authority = parsed_url
.authority()
.context("URL has no authority")?
.clone();
let req = hyper::Request::post(parsed_url.path())
.header(hyper::header::HOST, authority.as_str())
.body(http_body_util::Empty::<bytes::Bytes>::new())?;

let response = sender.send_request(req).await?;
if response.status().is_redirection() {
let header = response
.headers()
.get_all("location")
.iter()
.last()
.ok_or_else(|| anyhow!("Redirection doesn't have a target location"))?
.to_str()?;
info!(context, "Following redirect to {}", header);
url = header.to_string();
continue;
}

let response_status = response.status();
let body = response.collect().await?.to_bytes();
let text = String::from_utf8_lossy(&body);
let response_text = text.to_string();

return Ok((response_text, response_status.is_success()));
}

// As only HTTPS is used, it is safe to load DNS cache.
let load_cache = true;

let response = crate::net::http::get_client(context, load_cache)
.await?
.post(url)
.send()
.await?;
let response_status = response.status();
let response_text = response
.text()
.await
.context("Request failed: empty response")?;

Ok((response_text, response_status.is_success()))
Err(anyhow!("Followed 10 redirections"))
}

0 comments on commit 0a62b26

Please sign in to comment.