From fd88e0c648e6632f3f92ed119b1a93aefd66ed64 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Thu, 9 Jan 2020 22:43:08 +0100 Subject: [PATCH] wasm: add url function to wasm response (#777) Adds the url function to wasm::Response --- src/wasm/client.rs | 5 ++++- src/wasm/response.rs | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/wasm/client.rs b/src/wasm/client.rs index d0cbff64a..998370dd1 100644 --- a/src/wasm/client.rs +++ b/src/wasm/client.rs @@ -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; @@ -147,6 +148,8 @@ async fn fetch(req: Request) -> crate::Result { 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") @@ -162,7 +165,7 @@ async fn fetch(req: Request) -> crate::Result { } resp.body(js_resp) - .map(Response::new) + .map(|resp| Response::new(resp, url)) .map_err(crate::error::request) } diff --git a/src/wasm/response.rs b/src/wasm/response.rs index 93c97a23b..3653d11f0 100644 --- a/src/wasm/response.rs +++ b/src/wasm/response.rs @@ -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, + // Boxed to save space (11 words to 1 word), and it's not accessed + // frequently internally. + url: Box, } impl Response { pub(super) fn new( res: http::Response, - //url: Url, + url: Url, ) -> Response { Response { http: res, + url: Box::new(url), } } @@ -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]