diff --git a/ehttp/src/lib.rs b/ehttp/src/lib.rs index 6c1d8c2..ea43f11 100644 --- a/ehttp/src/lib.rs +++ b/ehttp/src/lib.rs @@ -99,7 +99,7 @@ pub mod streaming; /// ..Request::get("https://www.example.com") /// }; /// ``` -pub fn headers(headers: &[(&str, &str)]) -> std::collections::BTreeMap { +pub fn headers(headers: &[(&str, &str)]) -> Vec<(String, String)> { headers .iter() .map(|e| (e.0.to_owned(), e.1.to_owned())) diff --git a/ehttp/src/native.rs b/ehttp/src/native.rs index 8d49d4f..e373526 100644 --- a/ehttp/src/native.rs +++ b/ehttp/src/native.rs @@ -1,5 +1,3 @@ -use std::collections::BTreeMap; - use crate::{Request, Response}; #[cfg(feature = "native-async")] @@ -28,7 +26,7 @@ pub fn fetch_blocking(request: &Request) -> crate::Result { let mut req = ureq::request(&request.method, &request.url); for header in &request.headers { - req = req.set(header.0, header.1); + req = req.set(header.0.as_str(), header.1.as_str()); } let resp = if request.body.is_empty() { @@ -46,11 +44,11 @@ pub fn fetch_blocking(request: &Request) -> crate::Result { let url = resp.get_url().to_owned(); let status = resp.status(); let status_text = resp.status_text().to_owned(); - let mut headers = BTreeMap::new(); + let mut headers = vec![]; for key in &resp.headers_names() { if let Some(value) = resp.header(key) { // lowercase for easy lookup - headers.insert(key.to_ascii_lowercase(), value.to_owned()); + headers.push((key.to_ascii_lowercase(), value.to_owned())); } } diff --git a/ehttp/src/streaming/native.rs b/ehttp/src/streaming/native.rs index acc20d2..415468f 100644 --- a/ehttp/src/streaming/native.rs +++ b/ehttp/src/streaming/native.rs @@ -1,5 +1,6 @@ use std::collections::BTreeMap; use std::ops::ControlFlow; +use std::vec; use crate::Request; @@ -13,7 +14,7 @@ pub fn fetch_streaming_blocking( let mut req = ureq::request(&request.method, &request.url); for header in &request.headers { - req = req.set(header.0, header.1); + req = req.set(header.0.as_str(), header.1.as_str()); } let resp = if request.body.is_empty() { @@ -34,11 +35,11 @@ pub fn fetch_streaming_blocking( let url = resp.get_url().to_owned(); let status = resp.status(); let status_text = resp.status_text().to_owned(); - let mut headers = BTreeMap::new(); + let mut headers = vec!(); for key in &resp.headers_names() { if let Some(value) = resp.header(key) { // lowercase for easy lookup - headers.insert(key.to_ascii_lowercase(), value.to_owned()); + headers.push((key.to_ascii_lowercase(), value.to_owned())); } } diff --git a/ehttp/src/types.rs b/ehttp/src/types.rs index b5a3df5..780df3c 100644 --- a/ehttp/src/types.rs +++ b/ehttp/src/types.rs @@ -13,7 +13,7 @@ pub struct Request { pub body: Vec, /// ("Accept", "*/*"), … - pub headers: BTreeMap, + pub headers: Vec<(String, String)>, } impl Request { @@ -28,6 +28,10 @@ impl Request { } } + pub fn get_header(&self ,key:String)->Vec{ + self.headers.iter().filter(|h|h.0==key).map(|h|h.1.clone()).collect() + } + /// Create a `POST` request with the given url and body. #[allow(clippy::needless_pass_by_value)] pub fn post(url: impl ToString, body: Vec) -> Self { @@ -59,7 +63,7 @@ pub struct Response { pub status_text: String, /// The returned headers. All header names are lower-case. - pub headers: BTreeMap, + pub headers: Vec<(String, String)>, /// The raw bytes of the response body. pub bytes: Vec, @@ -71,7 +75,13 @@ impl Response { } pub fn content_type(&self) -> Option<&str> { - self.headers.get("content-type").map(|s| s.as_str()) + self.headers + .iter() + .find(|s| s.0 == "content-type") + .map(|s| s.1.as_str()) + } + pub fn get_header(&self ,key:String)->Vec{ + self.headers.iter().filter(|h|h.0==key).map(|h|h.1.clone()).collect() } } @@ -104,7 +114,7 @@ pub struct PartialResponse { pub status_text: String, /// The returned headers. All header names are lower-case. - pub headers: BTreeMap, + pub headers: Vec<(String, String)>, } impl PartialResponse { diff --git a/example_eframe/src/app.rs b/example_eframe/src/app.rs index a2292f6..041e7ec 100644 --- a/example_eframe/src/app.rs +++ b/example_eframe/src/app.rs @@ -265,8 +265,8 @@ fn response_ui(ui: &mut egui::Ui, response: &ehttp::Response) { .spacing(egui::vec2(ui.spacing().item_spacing.x * 2.0, 0.0)) .show(ui, |ui| { for header in &response.headers { - ui.label(header.0); - ui.label(header.1); + ui.label(header.0.clone()); + ui.label(header.1.clone()); ui.end_row(); } })