Skip to content

Commit

Permalink
Support HEAD requests
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 17, 2024
1 parent b463dc5 commit 4579dd1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
14 changes: 9 additions & 5 deletions ehttp/src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn fetch_blocking(request: &Request) -> crate::Result<Response> {
let (ok, resp) = match resp {
Ok(resp) => (true, resp),
Err(ureq::Error::Status(_, resp)) => (false, resp), // Still read the body on e.g. 404
Err(ureq::Error::Transport(error)) => return Err(error.to_string()),
Err(ureq::Error::Transport(err)) => return Err(err.to_string()),
};

let url = resp.get_url().to_owned();
Expand All @@ -56,10 +56,14 @@ pub fn fetch_blocking(request: &Request) -> crate::Result<Response> {

let mut reader = resp.into_reader();
let mut bytes = vec![];
use std::io::Read;
reader
.read_to_end(&mut bytes)
.map_err(|err| err.to_string())?;
use std::io::Read as _;
if let Err(err) = reader.read_to_end(&mut bytes) {
if request.method == "HEAD" && err.kind() == std::io::ErrorKind::UnexpectedEof {
// We don't really expect a body for HEAD requests, so this is fine.
} else {
return Err(format!("Failed to read response body: {err}"));
}
}

let response = Response {
url,
Expand Down
12 changes: 9 additions & 3 deletions ehttp/src/streaming/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ pub fn fetch_streaming_blocking(
on_data(Ok(Part::Chunk(vec![])));
break;
}
Err(error) => {
on_data(Err(error.to_string()));
return;
Err(err) => {
if request.method == "HEAD" && err.kind() == std::io::ErrorKind::UnexpectedEof {
// We don't really expect a body for HEAD requests, so this is fine.
on_data(Ok(Part::Chunk(vec![])));
break;
} else {
on_data(Err(format!("Failed to read response body: {err}")));
return;
}
}
};
}
Expand Down
11 changes: 11 additions & 0 deletions ehttp/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ impl Request {
}
}

/// Create a `HEAD` request with the given url.
#[allow(clippy::needless_pass_by_value)]
pub fn head(url: impl ToString) -> Self {
Self {
method: "HEAD".to_owned(),
url: url.to_string(),
body: vec![],
headers: crate::headers(&[("Accept", "*/*")]),
}
}

/// Create a `POST` request with the given url and body.
#[allow(clippy::needless_pass_by_value)]
pub fn post(url: impl ToString, body: Vec<u8>) -> Self {
Expand Down
4 changes: 4 additions & 0 deletions example_eframe/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use eframe::egui;
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum Method {
Get,
Head,
Post,
}

Expand Down Expand Up @@ -50,6 +51,7 @@ impl eframe::App for DemoApp {
if trigger_fetch {
let request = match self.method {
Method::Get => ehttp::Request::get(&self.url),
Method::Head => ehttp::Request::head(&self.url),
Method::Post => {
ehttp::Request::post(&self.url, self.request_body.as_bytes().to_vec())
}
Expand Down Expand Up @@ -161,6 +163,8 @@ impl DemoApp {
ui.horizontal(|ui| {
ui.radio_value(&mut self.method, Method::Get, "GET")
.clicked();
ui.radio_value(&mut self.method, Method::Head, "HEAD")
.clicked();
ui.radio_value(&mut self.method, Method::Post, "POST")
.clicked();
});
Expand Down

0 comments on commit 4579dd1

Please sign in to comment.