From 470c8fd8589097e0a4e25a3f5bc1d4fcaf4c93c1 Mon Sep 17 00:00:00 2001 From: Henry Fontanier Date: Mon, 3 Jun 2024 10:38:51 +0200 Subject: [PATCH 1/2] fix: don't expose hyper HeaderMap --- eventsource-client/src/error.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/eventsource-client/src/error.rs b/eventsource-client/src/error.rs index 2585e47..1b6f101 100644 --- a/eventsource-client/src/error.rs +++ b/eventsource-client/src/error.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + use hyper::{body::Buf, Body, Response}; pub struct ResponseWrapper { @@ -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> { + 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::InvalidParameter(Box::new(err))), + }; + map.insert(key, value); + } + Ok(map) } pub async fn body_bytes(self) -> Result> { From b0a09838ebd8b43218800d70dd89eab98ab84f3f Mon Sep 17 00:00:00 2001 From: Henry Fontanier Date: Mon, 3 Jun 2024 10:53:49 +0200 Subject: [PATCH 2/2] add error type --- eventsource-client/src/error.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/eventsource-client/src/error.rs b/eventsource-client/src/error.rs index 1b6f101..ede60b6 100644 --- a/eventsource-client/src/error.rs +++ b/eventsource-client/src/error.rs @@ -20,7 +20,7 @@ impl ResponseWrapper { let key = key.as_str(); let value = match value.to_str() { Ok(value) => value, - Err(err) => return Err(Error::InvalidParameter(Box::new(err))), + Err(err) => return Err(Error::InvalidResponseHeader(Box::new(err))), }; map.insert(key, value); } @@ -70,6 +70,8 @@ pub enum Error { MalformedLocationHeader(Box), /// Reached maximum redirect limit after encountering Location headers. MaxRedirectLimitReached(u32), + // Invalid response header. + InvalidResponseHeader(Box), } impl std::fmt::Display for Error { @@ -90,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}"), } } }