Skip to content

Commit 71a3944

Browse files
Revert "Merge branch 'main' into move-trailers"
This reverts commit 65cd9b7, reversing changes made to cbc6faf.
1 parent 64f270c commit 71a3944

File tree

16 files changed

+261
-511
lines changed

16 files changed

+261
-511
lines changed

.travis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
language: rust
2+
rust:
3+
- stable
4+
5+
before_script: |
6+
rustup component add rustfmt-preview &&
7+
rustup component add clippy-preview
8+
script: |
9+
cargo fmt -- --check &&
10+
cargo clippy -- -D clippy &&
11+
cargo build --verbose &&
12+
cargo test --verbose
13+
cache: cargo

Cargo.toml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "http-types"
3-
version = "3.0.0"
3+
version = "2.9.0"
44
license = "MIT OR Apache-2.0"
55
repository = "https://github.com/http-rs/http-types"
66
documentation = "https://docs.rs/http-types"
@@ -21,8 +21,7 @@ docs = ["unstable"]
2121
unstable = []
2222
hyperium_http = ["http"]
2323
async_std = ["fs"]
24-
cookies = ["cookie"]
25-
cookie-secure = ["cookies", "cookie/secure"]
24+
cookie-secure = ["cookie/secure"]
2625
fs = ["async-std"]
2726

2827
[dependencies]
@@ -35,9 +34,7 @@ async-channel = "1.5.1"
3534
http = { version = "0.2.0", optional = true }
3635

3736
anyhow = "1.0.26"
38-
39-
# features: cookies
40-
cookie = { version = "0.14.0", features = ["percent-encode"], optional = true }
37+
cookie = { version = "0.14.0", features = ["percent-encode"] }
4138
infer = "0.2.3"
4239
pin-project-lite = "0.2.0"
4340
url = { version = "2.1.1", features = ["serde"] }

src/conditional/etag.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl ETag {
117117

118118
if !s
119119
.bytes()
120-
.all(|c| c == 0x21 || (0x23..=0x7E).contains(&c) || c >= 0x80)
120+
.all(|c| c == 0x21 || (c >= 0x23 && c <= 0x7E) || c >= 0x80)
121121
{
122122
return Err(Error::from_str(
123123
StatusCode::BadRequest,

src/headers/header_name.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ mod tests {
135135
use super::*;
136136

137137
#[test]
138-
#[allow(clippy::eq_op)]
139138
fn test_header_name_static_non_static() {
140139
let static_header = HeaderName::from_lowercase_str("hello");
141140
let non_static_header = HeaderName::from_str("hello").unwrap();

src/headers/header_value.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ impl From<Mime> for HeaderValue {
5656
}
5757
}
5858

59-
#[cfg(feature = "cookies")]
6059
impl From<Cookie<'_>> for HeaderValue {
6160
fn from(cookie: Cookie<'_>) -> Self {
6261
HeaderValue {

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
//! ```
99
//! # fn main() -> Result<(), http_types::url::ParseError> {
1010
//! #
11-
//! use http_types::{Method, Request, Response, StatusCode};
11+
//! use http_types::{Url, Method, Request, Response, StatusCode};
1212
//!
13-
//! let mut req = Request::new(Method::Get, "https://example.com");
13+
//! let mut req = Request::new(Method::Get, Url::parse("https://example.com")?);
1414
//! req.set_body("Hello, Nori!");
1515
//!
1616
//! let mut res = Response::new(StatusCode::Ok);
@@ -102,7 +102,6 @@
102102
#![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")]
103103

104104
/// HTTP cookies.
105-
#[cfg(feature = "cookies")]
106105
pub mod cookies {
107106
pub use cookie::*;
108107
}
@@ -164,6 +163,9 @@ pub use headers::Headers;
164163
#[doc(inline)]
165164
pub use crate::url::Url;
166165

166+
#[doc(inline)]
167+
pub use crate::cookies::Cookie;
168+
167169
pub mod security;
168170

169171
#[cfg(feature = "hyperium_http")]

src/mime/constants.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::mime::Mime;
2-
use std::borrow::Cow;
1+
use super::ParamKind;
2+
use crate::Mime;
33

44
macro_rules! utf8_mime_const {
55
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
@@ -9,34 +9,47 @@ macro_rules! utf8_mime_const {
99
$desc,
1010
$base,
1111
$sub,
12-
true,
12+
Some(ParamKind::Utf8),
1313
";charset=utf-8"
1414
);
1515
};
1616
}
1717
macro_rules! mime_const {
1818
($name:ident, $desc:expr, $base:expr, $sub:expr) => {
19-
mime_const!(with_params, $name, $desc, $base, $sub, false, "");
19+
mime_const!(with_params, $name, $desc, $base, $sub, None, "");
2020
};
2121

22-
(with_params, $name:ident, $desc:expr, $base:expr, $sub:expr, $is_utf8:expr, $doccomment:expr) => {
23-
mime_const!(doc_expanded, $name, $desc, $base, $sub, $is_utf8,
24-
concat!(
22+
(with_params, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
23+
mime_const!(
24+
doc_expanded,
25+
$name,
26+
$desc,
27+
$base,
28+
$sub,
29+
$params,
30+
concat!(
2531
"Content-Type for ",
2632
$desc,
2733
".\n\n# Mime Type\n\n```text\n",
28-
$base, "/", $sub, $doccomment, "\n```")
34+
$base,
35+
"/",
36+
$sub,
37+
$doccomment,
38+
"\n```"
39+
)
2940
);
3041
};
3142

32-
(doc_expanded, $name:ident, $desc:expr, $base:expr, $sub:expr, $is_utf8:expr, $doccomment:expr) => {
43+
(doc_expanded, $name:ident, $desc:expr, $base:expr, $sub:expr, $params:expr, $doccomment:expr) => {
3344
#[doc = $doccomment]
3445
pub const $name: Mime = Mime {
35-
essence: Cow::Borrowed(concat!($base, "/", $sub)),
36-
basetype: Cow::Borrowed($base),
37-
subtype: Cow::Borrowed($sub),
38-
is_utf8: $is_utf8,
39-
params: vec![],
46+
essence: String::new(),
47+
basetype: String::new(),
48+
subtype: String::new(),
49+
params: $params,
50+
static_essence: Some(concat!($base, "/", $sub)),
51+
static_basetype: Some($base),
52+
static_subtype: Some($sub),
4053
};
4154
};
4255
}

src/mime/mod.rs

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ use infer::Infer;
2828
/// ```
2929
// NOTE: we cannot statically initialize Strings with values yet, so we keep dedicated static
3030
// fields for the static strings.
31-
#[derive(Clone, PartialEq, Eq, Debug)]
31+
#[derive(Clone)]
3232
pub struct Mime {
33-
pub(crate) essence: Cow<'static, str>,
34-
pub(crate) basetype: Cow<'static, str>,
35-
pub(crate) subtype: Cow<'static, str>,
36-
// NOTE(yosh): this is a hack because we can't populate vecs in const yet.
37-
// This enables us to encode media types as utf-8 at compilation.
38-
pub(crate) is_utf8: bool,
39-
pub(crate) params: Vec<(ParamName, ParamValue)>,
33+
pub(crate) essence: String,
34+
pub(crate) basetype: String,
35+
pub(crate) subtype: String,
36+
pub(crate) static_essence: Option<&'static str>,
37+
pub(crate) static_basetype: Option<&'static str>,
38+
pub(crate) static_subtype: Option<&'static str>,
39+
pub(crate) params: Option<ParamKind>,
4040
}
4141

4242
impl Mime {
@@ -68,40 +68,87 @@ impl Mime {
6868
/// According to the spec this method should be named `type`, but that's a reserved keyword in
6969
/// Rust so hence prefix with `base` instead.
7070
pub fn basetype(&self) -> &str {
71-
&self.basetype
71+
if let Some(basetype) = self.static_basetype {
72+
&basetype
73+
} else {
74+
&self.basetype
75+
}
7276
}
7377

7478
/// Access the Mime's `subtype` value.
7579
pub fn subtype(&self) -> &str {
76-
&self.subtype
80+
if let Some(subtype) = self.static_subtype {
81+
&subtype
82+
} else {
83+
&self.subtype
84+
}
7785
}
7886

7987
/// Access the Mime's `essence` value.
8088
pub fn essence(&self) -> &str {
81-
&self.essence
89+
if let Some(essence) = self.static_essence {
90+
&essence
91+
} else {
92+
&self.essence
93+
}
8294
}
8395

8496
/// Get a reference to a param.
8597
pub fn param(&self, name: impl Into<ParamName>) -> Option<&ParamValue> {
8698
let name: ParamName = name.into();
87-
if name.as_str() == "charset" && self.is_utf8 {
88-
return Some(&ParamValue(Cow::Borrowed("utf-8")));
89-
}
90-
91-
self.params.iter().find(|(k, _)| k == &name).map(|(_, v)| v)
99+
self.params
100+
.as_ref()
101+
.map(|inner| match inner {
102+
ParamKind::Vec(v) => v
103+
.iter()
104+
.find_map(|(k, v)| if k == &name { Some(v) } else { None }),
105+
ParamKind::Utf8 => match name {
106+
ParamName(Cow::Borrowed("charset")) => Some(&ParamValue(Cow::Borrowed("utf8"))),
107+
_ => None,
108+
},
109+
})
110+
.flatten()
92111
}
93112

94113
/// Remove a param from the set. Returns the `ParamValue` if it was contained within the set.
95114
pub fn remove_param(&mut self, name: impl Into<ParamName>) -> Option<ParamValue> {
96115
let name: ParamName = name.into();
97-
if name.as_str() == "charset" && self.is_utf8 {
98-
self.is_utf8 = false;
99-
return Some(ParamValue(Cow::Borrowed("utf-8")));
116+
let mut unset_params = false;
117+
let ret = self
118+
.params
119+
.as_mut()
120+
.map(|inner| match inner {
121+
ParamKind::Vec(v) => match v.iter().position(|(k, _)| k == &name) {
122+
Some(index) => Some(v.remove(index).1),
123+
None => None,
124+
},
125+
ParamKind::Utf8 => match name {
126+
ParamName(Cow::Borrowed("charset")) => {
127+
unset_params = true;
128+
Some(ParamValue(Cow::Borrowed("utf8")))
129+
}
130+
_ => None,
131+
},
132+
})
133+
.flatten();
134+
if unset_params {
135+
self.params = None;
100136
}
101-
self.params
102-
.iter()
103-
.position(|(k, _)| k == &name)
104-
.map(|pos| self.params.remove(pos).1)
137+
ret
138+
}
139+
}
140+
141+
impl PartialEq<Mime> for Mime {
142+
fn eq(&self, other: &Mime) -> bool {
143+
let left = match self.static_essence {
144+
Some(essence) => essence,
145+
None => &self.essence,
146+
};
147+
let right = match other.static_essence {
148+
Some(essence) => essence,
149+
None => &other.essence,
150+
};
151+
left == right
105152
}
106153
}
107154

@@ -111,11 +158,15 @@ impl Display for Mime {
111158
}
112159
}
113160

114-
// impl Debug for Mime {
115-
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116-
// Debug::fmt(&self.essence, f)
117-
// }
118-
// }
161+
impl Debug for Mime {
162+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
163+
if let Some(essence) = self.static_essence {
164+
Debug::fmt(essence, f)
165+
} else {
166+
Debug::fmt(&self.essence, f)
167+
}
168+
}
169+
}
119170

120171
impl FromStr for Mime {
121172
type Err = crate::Error;
@@ -145,7 +196,6 @@ impl ToHeaderValues for Mime {
145196
Ok(header.to_header_values().unwrap())
146197
}
147198
}
148-
149199
/// A parameter name.
150200
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
151201
pub struct ParamName(Cow<'static, str>);
@@ -209,3 +259,11 @@ impl PartialEq<str> for ParamValue {
209259
self.0 == other
210260
}
211261
}
262+
263+
/// This is a hack that allows us to mark a trait as utf8 during compilation. We
264+
/// can remove this once we can construct HashMap during compilation.
265+
#[derive(Debug, Clone, PartialEq, Eq)]
266+
pub(crate) enum ParamKind {
267+
Utf8,
268+
Vec(Vec<(ParamName, ParamValue)>),
269+
}

0 commit comments

Comments
 (0)