Skip to content

Commit

Permalink
Merge pull request #178 from jbr/update-http-types
Browse files Browse the repository at this point in the history
update http-types and http-client
  • Loading branch information
yoshuawuyts committed May 29, 2020
2 parents b81ad3d + 42cd13a commit ebc44c4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Expand Up @@ -25,16 +25,16 @@ encoding = ["encoding_rs", "web-sys"]

[dependencies]
futures = { version = "0.3.1", features = ["compat", "io-compat"] }
http-types = "1.0.1"
log = { version = "0.4.7", features = ["kv_unstable"] }
mime = "0.3.13"
mime_guess = "2.0.3"
serde = "1.0.97"
serde_json = "1.0.40"
serde_urlencoded = "0.6.1"
url = "2.0.0"
http-client = "2.0.0"
async-std = { version = "1.4.0", default-features = false, features = ["std"] }
http-client = "3.0.0"
http-types = "2.0.0"
async-std = { version = "1.6.0", default-features = false, features = ["std"] }
pin-project-lite = "0.1.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand All @@ -50,7 +50,7 @@ features = [
]

[dev-dependencies]
async-std = { version = "1.0", features = ["attributes"] }
async-std = { version = "1.6.0", features = ["attributes"] }
femme = "1.1.0"
serde = { version = "1.0.97", features = ["derive"] }
mockito = "0.23.3"
Expand Down
2 changes: 1 addition & 1 deletion examples/next_reuse.rs
Expand Up @@ -17,7 +17,7 @@ impl<C: HttpClient> Middleware<C> for Doubler {
let mut new_req = Request::new(req.method().clone(), req.url().clone());
new_req.set_version(req.version().clone());
for (name, value) in &req {
new_req.insert_header(name.clone(), &value[..])?;
new_req.insert_header(name, value);
}

let mut buf = Vec::new();
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -62,6 +62,7 @@
//!
//! # Features
//! The following features are available.
//! - __`h1-client`:__ use `async-h1` on the server and `window.fetch` in the browser.
//! - __`native-client` (default):__ use `curl` on the server and `window.fetch` in the browser.
//! - __`middleware-logger` (default):__ enables logging requests and responses using a middleware.
//! - __`curl-client`:__ use `curl` (through `isahc`) as the HTTP backend.
Expand Down
24 changes: 11 additions & 13 deletions src/request.rs
Expand Up @@ -3,7 +3,7 @@ use crate::Response;
use async_std::io::BufRead;
use futures::future::BoxFuture;
use http_client::{self, HttpClient};
use http_types::headers::{HeaderName, HeaderValue, CONTENT_TYPE};
use http_types::headers::{HeaderName, HeaderValues, ToHeaderValues, CONTENT_TYPE};
use http_types::{Body, Error, Method};
use mime::Mime;
use serde::Serialize;
Expand Down Expand Up @@ -176,12 +176,11 @@ impl<C: HttpClient> Request<C> {
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let req = surf::get("https://httpbin.org/get")
/// .set_header("X-Requested-With".parse().unwrap(), "surf");
/// assert_eq!(req.header(&"X-Requested-With".parse().unwrap()), Some(&vec!["surf".parse().unwrap()]));
/// let req = surf::get("https://httpbin.org/get").set_header("X-Requested-With", "surf");
/// assert_eq!(req.header("X-Requested-With").unwrap(), "surf");
/// # Ok(()) }
/// ```
pub fn header(&self, key: &HeaderName) -> Option<&'_ Vec<HeaderValue>> {
pub fn header(&self, key: impl Into<HeaderName>) -> Option<&HeaderValues> {
let req = self.req.as_ref().unwrap();
req.header(key)
}
Expand All @@ -193,16 +192,15 @@ impl<C: HttpClient> Request<C> {
/// ```no_run
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let req = surf::get("https://httpbin.org/get")
/// .set_header("X-Requested-With".parse().unwrap(), "surf");
/// assert_eq!(req.header(&"X-Requested-With".parse().unwrap()), Some(&vec!["surf".parse().unwrap()]));
/// let req = surf::get("https://httpbin.org/get").set_header("X-Requested-With", "surf");
/// assert_eq!(req.header("X-Requested-With").unwrap(), "surf");
/// # Ok(()) }
/// ```
pub fn set_header(mut self, key: HeaderName, value: impl AsRef<str>) -> Self {
pub fn set_header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self {
self.req
.as_mut()
.unwrap()
.insert_header(key, &[value.as_ref().parse().unwrap()][..])
.insert_header(key, value)
.unwrap();
self
}
Expand Down Expand Up @@ -614,7 +612,7 @@ impl<C: HttpClient> fmt::Debug for Request<C> {

#[cfg(any(feature = "native-client", feature = "h1-client"))]
impl IntoIterator for Request<Client> {
type Item = (HeaderName, Vec<HeaderValue>);
type Item = (HeaderName, HeaderValues);
type IntoIter = http_types::headers::IntoIter;

/// Returns a iterator of references over the remaining items.
Expand All @@ -626,7 +624,7 @@ impl IntoIterator for Request<Client> {

#[cfg(any(feature = "native-client", feature = "h1-client"))]
impl<'a> IntoIterator for &'a Request<Client> {
type Item = (&'a HeaderName, &'a Vec<HeaderValue>);
type Item = (&'a HeaderName, &'a HeaderValues);
type IntoIter = http_types::headers::Iter<'a>;

#[inline]
Expand All @@ -637,7 +635,7 @@ impl<'a> IntoIterator for &'a Request<Client> {

#[cfg(any(feature = "native-client", feature = "h1-client"))]
impl<'a> IntoIterator for &'a mut Request<Client> {
type Item = (&'a HeaderName, &'a mut Vec<HeaderValue>);
type Item = (&'a HeaderName, &'a mut HeaderValues);
type IntoIter = http_types::headers::IterMut<'a>;

#[inline]
Expand Down
10 changes: 5 additions & 5 deletions src/response.rs
Expand Up @@ -3,7 +3,7 @@ use futures::io::AsyncReadExt;
use futures::prelude::*;
use http_client;
use http_types::{
headers::{HeaderName, HeaderValue, CONTENT_TYPE},
headers::{HeaderName, HeaderValues, CONTENT_TYPE},
Error, StatusCode, Version,
};
use mime::Mime;
Expand Down Expand Up @@ -68,10 +68,10 @@ impl Response {
/// # #[async_std::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// let res = surf::get("https://httpbin.org/get").await?;
/// assert!(res.header(&"Content-Length".parse().unwrap()).is_some());
/// assert!(res.header("Content-Length").is_some());
/// # Ok(()) }
/// ```
pub fn header(&self, key: &HeaderName) -> Option<&'_ Vec<HeaderValue>> {
pub fn header(&self, key: impl Into<HeaderName>) -> Option<&HeaderValues> {
self.response.header(key)
}

Expand All @@ -96,8 +96,8 @@ impl Response {
/// # Ok(()) }
/// ```
pub fn mime(&self) -> Option<Mime> {
let header = self.header(&CONTENT_TYPE)?;
header.last().and_then(|s| s.as_str().parse().ok())
self.header(&CONTENT_TYPE)
.and_then(|header| header.last().as_str().parse().ok())
}

/// Reads the entire request body into a byte buffer.
Expand Down

0 comments on commit ebc44c4

Please sign in to comment.