Skip to content

Commit 204268a

Browse files
authored
Merge pull request #520 from jbr/update-http-types
http-types and async-h1 2.0.0
2 parents d241f21 + 1e0be8b commit 204268a

16 files changed

+198
-318
lines changed

Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ unstable = []
3232
__internal__bench = []
3333

3434
[dependencies]
35-
async-h1 = { version = "1.1.2", optional = true }
35+
async-h1 = { version = "2.0.0", optional = true }
3636
async-sse = "2.1.0"
37-
async-std = { version = "1.4.0", features = ["unstable"] }
38-
cookie = { version = "0.12.0", features = ["percent-encode"]}
37+
async-std = { version = "1.6.0", features = ["unstable"] }
3938
femme = "2.0.1"
40-
http-types = "1.0.1"
39+
http-types = "2.0.0"
4140
kv-log-macro = "1.0.4"
4241
mime = "0.3.14"
4342
mime_guess = "2.0.3"
@@ -47,12 +46,12 @@ serde_json = "1.0.41"
4746
serde_qs = "0.5.0"
4847

4948
[dev-dependencies]
50-
async-std = { version = "1.4.0", features = ["unstable", "attributes"] }
49+
async-std = { version = "1.6.0", features = ["unstable", "attributes"] }
5150
juniper = "0.14.1"
5251
portpicker = "0.1.0"
5352
serde = { version = "1.0.102", features = ["derive"] }
54-
surf = "2.0.0-alpha.1"
5553
criterion = "0.3.1"
54+
surf = { version = "2.0.0-alpha.2", default-features = false, features = ["h1-client"] }
5655

5756
[[test]]
5857
name = "nested"

examples/graphql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ async fn handle_graphql(mut cx: Request<State>) -> tide::Result {
9595
async fn handle_graphiql(_: Request<State>) -> tide::Result {
9696
let res = Response::new(StatusCode::Ok)
9797
.body_string(juniper::http::graphiql::graphiql_source("/graphql"))
98-
.set_header("content-type".parse().unwrap(), "text/html;charset=utf-8");
98+
.set_mime(mime::TEXT_HTML_UTF_8);
9999
Ok(res)
100100
}
101101

src/cookies/middleware.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use std::sync::{Arc, RwLock};
1313
///
1414
/// ```
1515
/// # use tide::{Request, Response, StatusCode};
16+
/// # use tide::http::cookies::Cookie;
1617
/// let mut app = tide::Server::new();
1718
/// app.at("/get").get(|cx: Request<()>| async move { Ok(cx.cookie("testCookie").unwrap().value().to_string()) });
1819
/// app.at("/set").get(|_| async {
1920
/// let mut res = Response::new(StatusCode::Ok);
20-
/// res.set_cookie(cookie::Cookie::new("testCookie", "NewCookieValue"));
21+
/// res.set_cookie(Cookie::new("testCookie", "NewCookieValue"));
2122
/// Ok(res)
2223
/// });
2324
/// ```
@@ -38,13 +39,13 @@ impl<State: Send + Sync + 'static> Middleware<State> for CookiesMiddleware {
3839
next: Next<'a, State>,
3940
) -> BoxFuture<'a, crate::Result> {
4041
Box::pin(async move {
41-
let cookie_jar = if let Some(cookie_data) = ctx.local::<CookieData>() {
42+
let cookie_jar = if let Some(cookie_data) = ctx.ext::<CookieData>() {
4243
cookie_data.content.clone()
4344
} else {
4445
let cookie_data = CookieData::from_request(&ctx);
45-
// no cookie data in local context, so we try to create it
46+
// no cookie data in ext context, so we try to create it
4647
let content = cookie_data.content.clone();
47-
ctx = ctx.set_local(cookie_data);
48+
ctx = ctx.set_ext(cookie_data);
4849
content
4950
};
5051

src/redirect.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//! # Ok(()) }) }
1717
//! ```
1818
19+
use crate::http::headers::LOCATION;
1920
use crate::utils::BoxFuture;
2021
use crate::StatusCode;
2122
use crate::{Endpoint, Request, Response};
@@ -103,6 +104,6 @@ impl<T: AsRef<str>> Into<Response> for Redirect<T> {
103104

104105
impl<T: AsRef<str>> Into<Response> for &Redirect<T> {
105106
fn into(self) -> Response {
106-
Response::new(self.status).set_header("location".parse().unwrap(), &self.location)
107+
Response::new(self.status).set_header(LOCATION, self.location.as_ref())
107108
}
108109
}

src/request.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{str::FromStr, sync::Arc};
99

1010
use crate::cookies::CookieData;
1111
use crate::http::cookies::Cookie;
12-
use crate::http::headers::{HeaderName, HeaderValue};
12+
use crate::http::headers::{HeaderName, HeaderValues};
1313
use crate::http::{self, Method, StatusCode, Url, Version};
1414
use crate::Response;
1515

@@ -124,7 +124,7 @@ impl<State> Request<State> {
124124
///
125125
/// let mut app = tide::new();
126126
/// app.at("/").get(|req: Request<()>| async move {
127-
/// assert_eq!(req.header(&"X-Forwarded-For".parse().unwrap()), Some(&vec!["127.0.0.1".parse().unwrap()]));
127+
/// assert_eq!(req.header("X-Forwarded-For").unwrap(), "127.0.0.1");
128128
/// Ok("")
129129
/// });
130130
/// app.listen("127.0.0.1:8080").await?;
@@ -134,20 +134,20 @@ impl<State> Request<State> {
134134
#[must_use]
135135
pub fn header(
136136
&self,
137-
key: &http_types::headers::HeaderName,
138-
) -> Option<&Vec<http_types::headers::HeaderValue>> {
137+
key: impl Into<http_types::headers::HeaderName>,
138+
) -> Option<&http_types::headers::HeaderValues> {
139139
self.request.header(key)
140140
}
141141

142-
/// Get a local value.
142+
/// Get a request extension value.
143143
#[must_use]
144-
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
145-
self.request.local().get()
144+
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T> {
145+
self.request.ext().get()
146146
}
147147

148-
/// Set a local value.
149-
pub fn set_local<T: Send + Sync + 'static>(mut self, val: T) -> Self {
150-
self.request.local_mut().insert(val);
148+
/// Set a request extension value.
149+
pub fn set_ext<T: Send + Sync + 'static>(mut self, val: T) -> Self {
150+
self.request.ext_mut().insert(val);
151151
self
152152
}
153153

@@ -294,10 +294,8 @@ impl<State> Request<State> {
294294
/// returns a `Cookie` by name of the cookie.
295295
#[must_use]
296296
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
297-
if let Some(cookie_data) = self.local::<CookieData>() {
298-
return cookie_data.content.read().unwrap().get(name).cloned();
299-
}
300-
None
297+
self.ext::<CookieData>()
298+
.and_then(|cookie_data| cookie_data.content.read().unwrap().get(name).cloned())
301299
}
302300

303301
/// Get the length of the body.
@@ -349,7 +347,7 @@ impl<State: Send + Sync + 'static> Into<Response> for Request<State> {
349347
}
350348

351349
impl<State> IntoIterator for Request<State> {
352-
type Item = (HeaderName, Vec<HeaderValue>);
350+
type Item = (HeaderName, HeaderValues);
353351
type IntoIter = http_types::headers::IntoIter;
354352

355353
/// Returns a iterator of references over the remaining items.
@@ -360,7 +358,7 @@ impl<State> IntoIterator for Request<State> {
360358
}
361359

362360
impl<'a, State> IntoIterator for &'a Request<State> {
363-
type Item = (&'a HeaderName, &'a Vec<HeaderValue>);
361+
type Item = (&'a HeaderName, &'a HeaderValues);
364362
type IntoIter = http_types::headers::Iter<'a>;
365363

366364
#[inline]
@@ -370,7 +368,7 @@ impl<'a, State> IntoIterator for &'a Request<State> {
370368
}
371369

372370
impl<'a, State> IntoIterator for &'a mut Request<State> {
373-
type Item = (&'a HeaderName, &'a mut Vec<HeaderValue>);
371+
type Item = (&'a HeaderName, &'a mut HeaderValues);
374372
type IntoIter = http_types::headers::IterMut<'a>;
375373

376374
#[inline]

src/response.rs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use mime::Mime;
55
use serde::Serialize;
66

77
use crate::http::cookies::Cookie;
8-
use crate::http::headers::{HeaderName, HeaderValue};
8+
use crate::http::headers::{HeaderName, HeaderValues, ToHeaderValues, CONTENT_TYPE};
99
use crate::http::{self, Body, StatusCode};
10-
use crate::Redirect;
10+
use crate::redirect::Redirect;
1111

1212
#[derive(Debug)]
1313
pub(crate) enum CookieEvent {
@@ -99,38 +99,24 @@ impl Response {
9999

100100
/// Get an HTTP header.
101101
#[must_use]
102-
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
102+
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues> {
103103
self.res.header(name)
104104
}
105105

106106
/// Remove a header.
107-
pub fn remove_header(&mut self, name: &HeaderName) -> Option<Vec<HeaderValue>> {
107+
pub fn remove_header(&mut self, name: impl Into<HeaderName>) -> Option<HeaderValues> {
108108
self.res.remove_header(name)
109109
}
110110

111111
/// Insert an HTTP header.
112-
pub fn set_header(
113-
mut self,
114-
key: http_types::headers::HeaderName,
115-
value: impl AsRef<str>,
116-
) -> Self {
117-
let value = value.as_ref().to_owned();
118-
self.res
119-
.insert_header(key, &[value.parse().unwrap()][..])
120-
.expect("invalid header");
112+
pub fn set_header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self {
113+
self.res.insert_header(key, value);
121114
self
122115
}
123116

124117
/// Append an HTTP header.
125-
pub fn append_header(
126-
mut self,
127-
key: http_types::headers::HeaderName,
128-
value: impl AsRef<str>,
129-
) -> Self {
130-
let value = value.as_ref().to_owned();
131-
self.res
132-
.append_header(key, &[value.parse().unwrap()][..])
133-
.expect("invalid header");
118+
pub fn append_header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self {
119+
self.res.append_header(key, value);
134120
self
135121
}
136122

@@ -139,7 +125,7 @@ impl Response {
139125
/// [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)
140126
#[must_use]
141127
pub fn set_mime(self, mime: Mime) -> Self {
142-
self.set_header(http_types::headers::CONTENT_TYPE, format!("{}", mime))
128+
self.set_header(CONTENT_TYPE, mime.to_string())
143129
}
144130

145131
/// Pass a string as the request body.
@@ -233,15 +219,15 @@ impl Response {
233219
self.cookie_events.push(CookieEvent::Removed(cookie));
234220
}
235221

236-
/// Get a local value.
222+
/// Get a response extension value.
237223
#[must_use]
238-
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
239-
self.res.local().get()
224+
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T> {
225+
self.res.ext().get()
240226
}
241227

242228
/// Set a local value.
243-
pub fn set_local<T: Send + Sync + 'static>(mut self, val: T) -> Self {
244-
self.res.local_mut().insert(val);
229+
pub fn set_ext<T: Send + Sync + 'static>(mut self, val: T) -> Self {
230+
self.res.ext_mut().insert(val);
245231
self
246232
}
247233

@@ -309,7 +295,7 @@ impl<'a> From<&'a str> for Response {
309295
}
310296

311297
impl IntoIterator for Response {
312-
type Item = (HeaderName, Vec<HeaderValue>);
298+
type Item = (HeaderName, HeaderValues);
313299
type IntoIter = http_types::headers::IntoIter;
314300

315301
/// Returns a iterator of references over the remaining items.
@@ -320,7 +306,7 @@ impl IntoIterator for Response {
320306
}
321307

322308
impl<'a> IntoIterator for &'a Response {
323-
type Item = (&'a HeaderName, &'a Vec<HeaderValue>);
309+
type Item = (&'a HeaderName, &'a HeaderValues);
324310
type IntoIter = http_types::headers::Iter<'a>;
325311

326312
#[inline]
@@ -330,7 +316,7 @@ impl<'a> IntoIterator for &'a Response {
330316
}
331317

332318
impl<'a> IntoIterator for &'a mut Response {
333-
type Item = (&'a HeaderName, &'a mut Vec<HeaderValue>);
319+
type Item = (&'a HeaderName, &'a mut HeaderValues);
334320
type IntoIter = http_types::headers::IterMut<'a>;
335321

336322
#[inline]

src/route.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl<'a, State: 'static> Route<'a, State> {
153153
));
154154
(ep.clone(), ep)
155155
};
156-
self.router.add(&self.path, method.clone(), ep1);
156+
self.router.add(&self.path, method, ep1);
157157
let wildcard = self.at("*--tide-path-rest");
158158
wildcard.router.add(&wildcard.path, method, ep2);
159159
} else {

0 commit comments

Comments
 (0)