Skip to content

Commit

Permalink
Merge pull request #2 from dust-tt/fix/dont-expose-hyper-headermap
Browse files Browse the repository at this point in the history
fix: don't expose hyper HeaderMap
  • Loading branch information
fontanierh authored Jun 3, 2024
2 parents f532589 + b0a0983 commit df8e425
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions eventsource-client/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use hyper::{body::Buf, Body, Response};

pub struct ResponseWrapper {
Expand All @@ -11,8 +13,18 @@ impl ResponseWrapper {
pub fn status(&self) -> u16 {
self.response.status().as_u16()
}
pub fn headers(&self) -> &hyper::header::HeaderMap {
self.response.headers()
pub fn headers(&self) -> Result<HashMap<&str, &str>> {
let headers = self.response.headers();
let mut map = HashMap::new();
for (key, value) in headers.iter() {
let key = key.as_str();
let value = match value.to_str() {
Ok(value) => value,
Err(err) => return Err(Error::InvalidResponseHeader(Box::new(err))),
};
map.insert(key, value);
}
Ok(map)
}

pub async fn body_bytes(self) -> Result<Vec<u8>> {
Expand Down Expand Up @@ -58,6 +70,8 @@ pub enum Error {
MalformedLocationHeader(Box<dyn std::error::Error + Send + Sync + 'static>),
/// Reached maximum redirect limit after encountering Location headers.
MaxRedirectLimitReached(u32),
// Invalid response header.
InvalidResponseHeader(Box<dyn std::error::Error + Send + Sync + 'static>),
}

impl std::fmt::Display for Error {
Expand All @@ -78,6 +92,7 @@ impl std::fmt::Display for Error {
InvalidEvent => write!(f, "invalid event"),
MalformedLocationHeader(err) => write!(f, "malformed header: {err}"),
MaxRedirectLimitReached(limit) => write!(f, "maximum redirect limit reached: {limit}"),
InvalidResponseHeader(err) => write!(f, "invalid response header: {err}"),
}
}
}
Expand Down

0 comments on commit df8e425

Please sign in to comment.