Skip to content

Commit 87cb9e9

Browse files
committed
update http-types
1 parent f169f9c commit 87cb9e9

15 files changed

+173
-273
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ docs = ["unstable"]
3030
unstable = []
3131

3232
[dependencies]
33-
async-h1 = { version = "1.1.2", optional = true }
33+
async-h1 = { git = "https://github.com/jbr/async-h1.git", branch = "update-http-types", optional = true }
3434
async-sse = "2.1.0"
3535
async-std = { version = "1.4.0", features = ["unstable"] }
36-
cookie = { version = "0.12.0", features = ["percent-encode"]}
3736
femme = "2.0.1"
38-
http-types = "1.0.1"
37+
http-types = { git = "https://github.com/http-rs/http-types.git", branch = "master" }
3938
kv-log-macro = "1.0.4"
4039
mime = "0.3.14"
4140
mime_guess = "2.0.3"
@@ -49,7 +48,7 @@ async-std = { version = "1.4.0", features = ["unstable", "attributes"] }
4948
juniper = "0.14.1"
5049
portpicker = "0.1.0"
5150
serde = { version = "1.0.102", features = ["derive"] }
52-
surf = "2.0.0-alpha.1"
51+
surf = { version = "2.0.0-alpha.2", default-features = false, features = ["h1-client"] }
5352

5453
[[test]]
5554
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 {
44-
// no cookie data in local context, so we need to create it
45+
// no cookie data in ext context, so we need to create it
4546
let cookie_data = CookieData::from_request(&ctx);
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: 14 additions & 14 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

@@ -121,7 +121,7 @@ impl<State> Request<State> {
121121
///
122122
/// let mut app = tide::new();
123123
/// app.at("/").get(|req: Request<()>| async move {
124-
/// assert_eq!(req.header(&"X-Forwarded-For".parse().unwrap()), Some(&vec!["127.0.0.1".parse().unwrap()]));
124+
/// assert_eq!(req.header("X-Forwarded-For").unwrap().last().as_str(), "127.0.0.1");
125125
/// Ok("")
126126
/// });
127127
/// app.listen("127.0.0.1:8080").await?;
@@ -130,19 +130,19 @@ impl<State> Request<State> {
130130
/// ```
131131
pub fn header(
132132
&self,
133-
key: &http_types::headers::HeaderName,
134-
) -> Option<&Vec<http_types::headers::HeaderValue>> {
133+
key: impl Into<http_types::headers::HeaderName>,
134+
) -> Option<&http_types::headers::HeaderValues> {
135135
self.request.header(key)
136136
}
137137

138-
/// Get a local value.
139-
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
140-
self.request.local().get()
138+
/// Get a extension value.
139+
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T> {
140+
self.request.ext().get()
141141
}
142142

143-
/// Set a local value.
144-
pub fn set_local<T: Send + Sync + 'static>(mut self, val: T) -> Self {
145-
self.request.local_mut().insert(val);
143+
/// Set a extension value.
144+
pub fn set_ext<T: Send + Sync + 'static>(mut self, val: T) -> Self {
145+
self.request.ext_mut().insert(val);
146146
self
147147
}
148148

@@ -295,7 +295,7 @@ impl<State> Request<State> {
295295
/// returns a `Cookie` by name of the cookie.
296296
pub fn cookie(&self, name: &str) -> Option<Cookie<'static>> {
297297
let cookie_data = self
298-
.local::<CookieData>()
298+
.ext::<CookieData>()
299299
.expect("should always be set by the cookies middleware");
300300

301301
let locked_jar = cookie_data.content.read().unwrap();
@@ -345,7 +345,7 @@ impl<State: Send + Sync + 'static> Into<Response> for Request<State> {
345345
}
346346

347347
impl<State> IntoIterator for Request<State> {
348-
type Item = (HeaderName, Vec<HeaderValue>);
348+
type Item = (HeaderName, HeaderValues);
349349
type IntoIter = http_types::headers::IntoIter;
350350

351351
/// Returns a iterator of references over the remaining items.
@@ -356,7 +356,7 @@ impl<State> IntoIterator for Request<State> {
356356
}
357357

358358
impl<'a, State> IntoIterator for &'a Request<State> {
359-
type Item = (&'a HeaderName, &'a Vec<HeaderValue>);
359+
type Item = (&'a HeaderName, &'a HeaderValues);
360360
type IntoIter = http_types::headers::Iter<'a>;
361361

362362
#[inline]
@@ -366,7 +366,7 @@ impl<'a, State> IntoIterator for &'a Request<State> {
366366
}
367367

368368
impl<'a, State> IntoIterator for &'a mut Request<State> {
369-
type Item = (&'a HeaderName, &'a mut Vec<HeaderValue>);
369+
type Item = (&'a HeaderName, &'a mut HeaderValues);
370370
type IntoIter = http_types::headers::IterMut<'a>;
371371

372372
#[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 {
@@ -88,46 +88,32 @@ impl Response {
8888
}
8989

9090
/// Get an HTTP header.
91-
pub fn header(&self, name: &HeaderName) -> Option<&Vec<HeaderValue>> {
91+
pub fn header(&self, name: impl Into<HeaderName>) -> Option<&HeaderValues> {
9292
self.res.header(name)
9393
}
9494

9595
/// Remove a header.
96-
pub fn remove_header(&mut self, name: &HeaderName) -> Option<Vec<HeaderValue>> {
96+
pub fn remove_header(&mut self, name: impl Into<HeaderName>) -> Option<HeaderValues> {
9797
self.res.remove_header(name)
9898
}
9999

100100
/// Insert an HTTP header.
101-
pub fn set_header(
102-
mut self,
103-
key: http_types::headers::HeaderName,
104-
value: impl AsRef<str>,
105-
) -> Self {
106-
let value = value.as_ref().to_owned();
107-
self.res
108-
.insert_header(key, &[value.parse().unwrap()][..])
109-
.expect("invalid header");
101+
pub fn set_header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self {
102+
self.res.insert_header(key, value);
110103
self
111104
}
112105

113106
/// Append an HTTP header.
114-
pub fn append_header(
115-
mut self,
116-
key: http_types::headers::HeaderName,
117-
value: impl AsRef<str>,
118-
) -> Self {
119-
let value = value.as_ref().to_owned();
120-
self.res
121-
.append_header(key, &[value.parse().unwrap()][..])
122-
.expect("invalid header");
107+
pub fn append_header(mut self, key: impl Into<HeaderName>, value: impl ToHeaderValues) -> Self {
108+
self.res.append_header(key, value).expect("invalid header");
123109
self
124110
}
125111

126112
/// Set the request MIME.
127113
///
128114
/// [Read more on MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)
129115
pub fn set_mime(self, mime: Mime) -> Self {
130-
self.set_header(http_types::headers::CONTENT_TYPE, format!("{}", mime))
116+
self.set_header(CONTENT_TYPE, mime.to_string())
131117
}
132118

133119
/// Pass a string as the request body.
@@ -220,14 +206,14 @@ impl Response {
220206
self.cookie_events.push(CookieEvent::Removed(cookie));
221207
}
222208

223-
/// Get a local value.
224-
pub fn local<T: Send + Sync + 'static>(&self) -> Option<&T> {
225-
self.res.local().get()
209+
/// Get a extension value.
210+
pub fn ext<T: Send + Sync + 'static>(&self) -> Option<&T> {
211+
self.res.ext().get()
226212
}
227213

228214
/// Set a local value.
229-
pub fn set_local<T: Send + Sync + 'static>(mut self, val: T) -> Self {
230-
self.res.local_mut().insert(val);
215+
pub fn set_ext<T: Send + Sync + 'static>(mut self, val: T) -> Self {
216+
self.res.ext_mut().insert(val);
231217
self
232218
}
233219

@@ -297,7 +283,7 @@ impl<'a> From<&'a str> for Response {
297283
}
298284

299285
impl IntoIterator for Response {
300-
type Item = (HeaderName, Vec<HeaderValue>);
286+
type Item = (HeaderName, HeaderValues);
301287
type IntoIter = http_types::headers::IntoIter;
302288

303289
/// Returns a iterator of references over the remaining items.
@@ -308,7 +294,7 @@ impl IntoIterator for Response {
308294
}
309295

310296
impl<'a> IntoIterator for &'a Response {
311-
type Item = (&'a HeaderName, &'a Vec<HeaderValue>);
297+
type Item = (&'a HeaderName, &'a HeaderValues);
312298
type IntoIter = http_types::headers::Iter<'a>;
313299

314300
#[inline]
@@ -318,7 +304,7 @@ impl<'a> IntoIterator for &'a Response {
318304
}
319305

320306
impl<'a> IntoIterator for &'a mut Response {
321-
type Item = (&'a HeaderName, &'a mut Vec<HeaderValue>);
307+
type Item = (&'a HeaderName, &'a mut HeaderValues);
322308
type IntoIter = http_types::headers::IterMut<'a>;
323309

324310
#[inline]

0 commit comments

Comments
 (0)