wasm: add url function to wasm response (#777)

Adds the url function to wasm::Response
This commit is contained in:
Paolo Barbolini
2020-01-09 22:43:08 +01:00
committed by Sean McArthur
parent 50c33a932e
commit fd88e0c648
2 changed files with 16 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ use http::Method;
use js_sys::Uint8Array; use js_sys::Uint8Array;
use std::future::Future; use std::future::Future;
use wasm_bindgen::UnwrapThrowExt as _; use wasm_bindgen::UnwrapThrowExt as _;
use url::Url;
use super::{Request, RequestBuilder, Response}; use super::{Request, RequestBuilder, Response};
use crate::IntoUrl; use crate::IntoUrl;
@@ -147,6 +148,8 @@ async fn fetch(req: Request) -> crate::Result<Response> {
let mut resp = http::Response::builder() let mut resp = http::Response::builder()
.status(js_resp.status()); .status(js_resp.status());
let url = Url::parse(&js_resp.url()).expect_throw("url parse");
let js_headers = js_resp.headers(); let js_headers = js_resp.headers();
let js_iter = js_sys::try_iter(&js_headers) let js_iter = js_sys::try_iter(&js_headers)
.expect_throw("headers try_iter") .expect_throw("headers try_iter")
@@ -162,7 +165,7 @@ async fn fetch(req: Request) -> crate::Result<Response> {
} }
resp.body(js_resp) resp.body(js_resp)
.map(Response::new) .map(|resp| Response::new(resp, url))
.map_err(crate::error::request) .map_err(crate::error::request)
} }

View File

@@ -3,19 +3,24 @@ use std::fmt;
use bytes::Bytes; use bytes::Bytes;
use js_sys::Uint8Array; use js_sys::Uint8Array;
use http::{HeaderMap, StatusCode}; use http::{HeaderMap, StatusCode};
use url::Url;
/// A Response to a submitted `Request`. /// A Response to a submitted `Request`.
pub struct Response { pub struct Response {
http: http::Response<web_sys::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 { impl Response {
pub(super) fn new( pub(super) fn new(
res: http::Response<web_sys::Response>, res: http::Response<web_sys::Response>,
//url: Url, url: Url,
) -> Response { ) -> Response {
Response { Response {
http: res, http: res,
url: Box::new(url),
} }
} }
@@ -37,6 +42,12 @@ impl Response {
self.http.headers_mut() 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? /* It might not be possible to detect this in JS?
/// Get the HTTP `Version` of this `Response`. /// Get the HTTP `Version` of this `Response`.
#[inline] #[inline]