Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Http conversions #26

Merged
merged 2 commits into from
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use body::Body;
use http::Request as HttpRequest;
use reqwest::{Method, Url};
use reqwest::header::HeaderMap;
use serde::ser::{Serialize, SerializeStruct, Serializer};
Expand Down Expand Up @@ -33,6 +34,21 @@ impl Request {
}
}

impl<T> From<HttpRequest<T>> for Request where T: Into<Body> {
fn from(r: HttpRequest<T>) -> Self {
let header = RequestHeader {
url: Url::parse(&format!("{}", r.uri())).unwrap(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge your PR but I'll add a note here that this should be better handled in the future. (Invalid urls could be used as an exploit to make a software panic, but it's not like the crate is security oriented to begin with.)

Also see: seanmonstar/reqwest#668

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That issue is about the other direction, but I'd not be surprised if this way also had parsing disagreements. Unfortunately, there doesn't seem any work going on to make a unified url crate that http is willing to use (builder pattern in use, HTTP2 breakdown and relative path support), so we're stuck with these conversion warts.

hyperium/http#396 hyperium/http#277 (points to servo/rust-url#468) hyperium/http#137 hyperium/http#73

method: r.method().clone(),
headers: r.headers().clone(),
};

Request {
header,
body: Some(r.into_body().into()),
}
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct RequestMem {
pub header: RequestHeader,
Expand Down
17 changes: 17 additions & 0 deletions src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use base64;
use error::Error;
use http::Response as HttpResponse;
use reqwest::header::HeaderMap;
use reqwest::{StatusCode, Url};
use serde::de::Error as DeError;
Expand Down Expand Up @@ -124,6 +125,22 @@ impl<'de> Visitor<'de> for ResponseVisitor {
}
}

impl From<Response> for HttpResponse<Vec<u8>> {
fn from(r: Response) -> HttpResponse<Vec<u8>> {
let mut http_rsp = HttpResponse::builder()
.status(r.status);
let headers = http_rsp.headers_mut().unwrap();
let mut last_header = None;
for (key, value) in r.headers {
if key.is_some() {
last_header = key.clone();
}
headers.insert(last_header.clone().unwrap(), value);
}
http_rsp.body(r.body).unwrap()
}
}

impl<'de> Deserialize<'de> for Response {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down