Skip to content

Commit

Permalink
wip: wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Sep 12, 2019
1 parent 5a337ba commit 5599bad
Show file tree
Hide file tree
Showing 12 changed files with 529 additions and 84 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ matrix:
#- rust: nightly
# env: FEATURES="--features socks"

# trust-dns
#- rust: stable
#- rust: nightly
# env: FEATURES="--features trust-dns"

# wasm
- name: "WASM"
env: TARGET=wasm32-unknown-unknown
rust: nightly
install: rustup target add "$TARGET"
script: cargo check --target "$TARGET"

# android
#- rust: stable
- rust: nightly
Expand Down
48 changes: 35 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,42 @@ publish = false
[package.metadata.docs.rs]
all-features = true

[features]
default = ["default-tls"]

tls = []

default-tls = ["hyper-tls", "native-tls", "tls", "tokio-tls"]
default-tls-vendored = ["default-tls", "native-tls/vendored"]

rustls-tls = ["hyper-rustls", "tokio-rustls", "webpki-roots", "rustls", "tls"]

#trust-dns = ["trust-dns-resolver"]

[dependencies]
http = "0.1.15"
url = "2.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
base64 = "0.10"
bytes = "0.4"
encoding_rs = "0.8"
futures-preview = { version = "=0.3.0-alpha.18" }
http = "0.1.15"
hyper = "=0.13.0-alpha.1"
log = "0.4"
mime = "0.3.7"
mime_guess = "2.0"
percent-encoding = "2.1"
tokio = { version = "=0.2.0-alpha.4", default-features = false, features = ["rt-full", "tcp"] }
tokio-executor = "=0.2.0-alpha.4"
url = "2.1"
uuid = { version = "0.7", features = ["v4"] }
time = "0.1.42"

# TODO: candidates for optional features

async-compression = { version = "0.1.0-alpha.4", default-features = false, features = ["gzip", "stream"] }
cookie_store = "0.9.0"
cookie = "0.12.0"
cookie_crate = { version = "0.12.0", package = "cookie" }
serde = "1.0"
serde_json = "1.0"
serde_urlencoded = "0.6.1"
Expand All @@ -61,25 +75,33 @@ webpki-roots = { version = "0.17", optional = true }
## trust-dns
#trust-dns-resolver = { version = "0.11", optional = true }

[dev-dependencies]

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
env_logger = "0.6"
serde = { version = "1.0", features = ["derive"] }
libflate = "0.1"
doc-comment = "0.3"
bytes = "0.4"
tokio-fs = { version = "=0.2.0-alpha.4" }

[features]
default = ["default-tls"]
[target.'cfg(windows)'.dependencies]
winreg = "0.6"

tls = []
# wasm

default-tls = ["hyper-tls", "native-tls", "tls", "tokio-tls"]
default-tls-vendored = ["default-tls", "native-tls/vendored"]
[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3.25"
wasm-bindgen = "0.2.48"
wasm-bindgen-futures = { version = "", features = ["futures_0_3"] }

rustls-tls = ["hyper-rustls", "tokio-rustls", "webpki-roots", "rustls", "tls"]
[target.'cfg(target_arch = "wasm32")'.dependencies.web-sys]
version = "0.3.25"
features = [
"Headers",
"Request",
"RequestInit",
"Response",
"Window",
]

#trust-dns = ["trust-dns-resolver"]

[target.'cfg(windows)'.dependencies]
winreg = "0.6"
13 changes: 6 additions & 7 deletions src/cookie.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! The cookies module contains types for working with request and response cookies.
use crate::cookie_crate;
use crate::header;
use std::borrow::Cow;
use std::fmt;
Expand All @@ -18,7 +17,7 @@ fn tm_to_systemtime(tm: time::Tm) -> SystemTime {
}

/// Error representing a parse failure of a 'Set-Cookie' header.
pub struct CookieParseError(cookie::ParseError);
pub struct CookieParseError(cookie_crate::ParseError);

impl<'a> fmt::Debug for CookieParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -35,7 +34,7 @@ impl<'a> fmt::Display for CookieParseError {
impl std::error::Error for CookieParseError {}

/// A single HTTP cookie.
pub struct Cookie<'a>(cookie::Cookie<'a>);
pub struct Cookie<'a>(cookie_crate::Cookie<'a>);

impl<'a> fmt::Debug for Cookie<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -50,20 +49,20 @@ impl Cookie<'static> {
N: Into<Cow<'static, str>>,
V: Into<Cow<'static, str>>,
{
Cookie(cookie::Cookie::new(name, value))
Cookie(cookie_crate::Cookie::new(name, value))
}
}

impl<'a> Cookie<'a> {
fn parse(value: &'a crate::header::HeaderValue) -> Result<Cookie<'a>, CookieParseError> {
std::str::from_utf8(value.as_bytes())
.map_err(cookie::ParseError::from)
.and_then(cookie::Cookie::parse)
.map_err(cookie_crate::ParseError::from)
.and_then(cookie_crate::Cookie::parse)
.map_err(CookieParseError)
.map(Cookie)
}

pub(crate) fn into_inner(self) -> cookie::Cookie<'a> {
pub(crate) fn into_inner(self) -> cookie_crate::Cookie<'a> {
self.0
}

Expand Down
3 changes: 0 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ struct Inner {
url: Option<Url>,
}

/// A `Result` alias where the `Err` case is `reqwest::Error`.
pub type Result<T> = std::result::Result<T, Error>;

impl Error {
fn new(kind: Kind, url: Option<Url>) -> Error {
Error {
Expand Down
42 changes: 29 additions & 13 deletions src/into_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,26 @@ pub trait PolyfillTryInto {
fn into_url(self) -> crate::Result<Url>;
}

impl PolyfillTryInto for Url {
fn into_url(self) -> crate::Result<Url> {
if self.has_host() {
Ok(self)
} else {
Err(crate::error::url_bad_scheme(self))
if_hyper! {
impl PolyfillTryInto for Url {
fn into_url(self) -> crate::Result<Url> {
if self.has_host() {
Ok(self)
} else {
Err(crate::error::url_bad_scheme(self))
}
}
}
}

if_wasm! {
impl PolyfillTryInto for Url {
fn into_url(self) -> crate::Result<Url> {
if self.has_host() {
Ok(self)
} else {
unimplemented!("url_bad_scheme");
}
}
}
}
Expand All @@ -37,14 +51,16 @@ impl<'a> PolyfillTryInto for &'a String {
}
}

pub(crate) fn expect_uri(url: &Url) -> hyper::Uri {
url.as_str()
.parse()
.expect("a parsed Url should always be a valid Uri")
}
if_hyper! {
pub(crate) fn expect_uri(url: &Url) -> http::Uri {
url.as_str()
.parse()
.expect("a parsed Url should always be a valid Uri")
}

pub(crate) fn try_uri(url: &Url) -> Option<hyper::Uri> {
url.as_str().parse().ok()
pub(crate) fn try_uri(url: &Url) -> Option<http::Uri> {
url.as_str().parse().ok()
}
}

#[cfg(test)]
Expand Down
118 changes: 70 additions & 48 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,60 +172,32 @@
////! - **trust-dns**: Enables a trust-dns async resolver instead of default
////! threadpool using `getaddrinfo`.

extern crate cookie as cookie_crate;

#[cfg(test)]
#[macro_use]
extern crate doc_comment;
macro_rules! if_wasm {
($($item:item)*) => {$(
#[cfg(target_arch = "wasm32")]
$item
)*}
}

#[cfg(test)]
doctest!("../README.md");
macro_rules! if_hyper {
($($item:item)*) => {$(
#[cfg(not(target_arch = "wasm32"))]
$item
)*}
}

pub use hyper::header;
pub use hyper::Method;
pub use hyper::{StatusCode, Version};
pub use url::ParseError as UrlError;
pub use http::header;
pub use http::Method;
pub use http::{StatusCode, Version};
pub use url::Url;

pub use self::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
//pub use self::body::Body;
//pub use self::client::{Client, ClientBuilder};
pub use self::error::{Error, Result};
pub use self::into_url::IntoUrl;
pub use self::proxy::Proxy;
pub use self::redirect::{RedirectAction, RedirectAttempt, RedirectPolicy};
//pub use self::request::{Request, RequestBuilder};
//pub use self::response::Response;
#[cfg(feature = "tls")]
pub use self::tls::{Certificate, Identity};

// this module must be first because of the `try_` macro
#[macro_use]
mod error;
/// A `Result` alias where the `Err` case is `reqwest::Error`.
pub type Result<T> = std::result::Result<T, Error>;

mod async_impl;
pub mod blocking;
mod connect;
pub mod cookie;
//#[cfg(feature = "trust-dns")]
//mod dns;
// universal mods
mod into_url;
mod proxy;
mod redirect;
#[cfg(feature = "tls")]
mod tls;

//pub mod multipart;

#[doc(hidden)]
#[deprecated(note = "types moved to top of crate")]
pub mod r#async {
pub use crate::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
}
pub use self::into_url::IntoUrl;

/// Shortcut method to quickly make a `GET` request.
///
Expand Down Expand Up @@ -271,8 +243,58 @@ fn _assert_impls() {
assert_send::<Request>();
assert_send::<RequestBuilder>();

assert_send::<Response>();
#[cfg(not(target_arch = "wasm32"))]
{
assert_send::<Response>();
}

assert_send::<Error>();
assert_sync::<Error>();
}

if_hyper! {
#[cfg(test)]
#[macro_use]
extern crate doc_comment;

#[cfg(test)]
doctest!("../README.md");

pub use self::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
pub use self::error::Error;
pub use self::proxy::Proxy;
pub use self::redirect::{RedirectAction, RedirectAttempt, RedirectPolicy};
#[cfg(feature = "tls")]
pub use self::tls::{Certificate, Identity};

#[macro_use]
mod error;

mod async_impl;
pub mod blocking;
mod connect;
pub mod cookie;
//#[cfg(feature = "trust-dns")]
//mod dns;
mod proxy;
mod redirect;
#[cfg(feature = "tls")]
mod tls;

#[doc(hidden)]
#[deprecated(note = "types moved to top of crate")]
pub mod r#async {
pub use crate::async_impl::{
multipart, Body, Client, ClientBuilder, Request, RequestBuilder, Response,
};
}
}

if_wasm! {
mod wasm;

use self::wasm::error;
pub use self::wasm::{Body, Client, ClientBuilder, error::Error, Request, RequestBuilder, Response};
}
3 changes: 3 additions & 0 deletions src/wasm/body.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// dox
#[derive(Debug)]
pub struct Body(());
Loading

0 comments on commit 5599bad

Please sign in to comment.