Skip to content

Commit

Permalink
wasm: add url function to wasm response (#777)
Browse files Browse the repository at this point in the history
Adds the url function to wasm::Response
  • Loading branch information
paolobarbolini authored and seanmonstar committed Jan 9, 2020
1 parent 50c33a9 commit fd88e0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/wasm/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use http::Method;
use js_sys::Uint8Array;
use std::future::Future;
use wasm_bindgen::UnwrapThrowExt as _;
use url::Url;

use super::{Request, RequestBuilder, Response};
use crate::IntoUrl;
Expand Down Expand Up @@ -147,6 +148,8 @@ async fn fetch(req: Request) -> crate::Result<Response> {
let mut resp = http::Response::builder()
.status(js_resp.status());

let url = Url::parse(&js_resp.url()).expect_throw("url parse");

let js_headers = js_resp.headers();
let js_iter = js_sys::try_iter(&js_headers)
.expect_throw("headers try_iter")
Expand All @@ -162,7 +165,7 @@ async fn fetch(req: Request) -> crate::Result<Response> {
}

resp.body(js_resp)
.map(Response::new)
.map(|resp| Response::new(resp, url))
.map_err(crate::error::request)
}

Expand Down
13 changes: 12 additions & 1 deletion src/wasm/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ use std::fmt;
use bytes::Bytes;
use js_sys::Uint8Array;
use http::{HeaderMap, StatusCode};
use url::Url;

/// A Response to a submitted `Request`.
pub struct Response {
http: http::Response<web_sys::Response>,
// Boxed to save space (11 words to 1 word), and it's not accessed
// frequently internally.
url: Box<Url>,
}

impl Response {
pub(super) fn new(
res: http::Response<web_sys::Response>,
//url: Url,
url: Url,
) -> Response {
Response {
http: res,
url: Box::new(url),
}
}

Expand All @@ -37,6 +42,12 @@ impl Response {
self.http.headers_mut()
}

/// Get the final `Url` of this `Response`.
#[inline]
pub fn url(&self) -> &Url {
&self.url
}

/* It might not be possible to detect this in JS?
/// Get the HTTP `Version` of this `Response`.
#[inline]
Expand Down

0 comments on commit fd88e0c

Please sign in to comment.