1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::ascii::Ascii;
use crate::solicit::header::HeaderError;
use bytes::Bytes;
use std::fmt;
#[derive(Eq, PartialEq, Hash, Clone)]
pub struct HeaderValue(Ascii);
impl HeaderValue {
pub fn from_bytes(bs: Bytes) -> Result<HeaderValue, (HeaderError, Bytes)> {
for &b in &bs {
if !b.is_ascii() {
return Err((HeaderError::HeaderValueNotAscii, bs));
}
if (b < b' ' || b > b'~') && b != b'\t' {
return Err((HeaderError::IncorrectCharInValue, bs));
}
}
Ok(HeaderValue(unsafe { Ascii::from_bytes_unchecked(bs) }))
}
pub fn into_inner(self) -> Bytes {
self.0.into_bytes()
}
pub fn as_slice(&self) -> &[u8] {
self.0.as_bytes()
}
pub const unsafe fn from_bytes_unchecked(bytes: Bytes) -> HeaderValue {
HeaderValue(Ascii::from_bytes_unchecked(bytes))
}
}
impl fmt::Debug for HeaderValue {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, fmt)
}
}
impl From<Vec<u8>> for HeaderValue {
fn from(vec: Vec<u8>) -> HeaderValue {
HeaderValue::from(Bytes::from(vec))
}
}
impl From<Bytes> for HeaderValue {
fn from(bytes: Bytes) -> HeaderValue {
HeaderValue::from_bytes(bytes).map_err(|(e, _)| e).unwrap()
}
}
impl<'a> From<&'a [u8]> for HeaderValue {
fn from(buf: &'a [u8]) -> HeaderValue {
HeaderValue::from(Bytes::copy_from_slice(buf))
}
}
impl From<String> for HeaderValue {
fn from(s: String) -> HeaderValue {
HeaderValue::from(s.into_bytes())
}
}
impl<'a> From<&'a str> for HeaderValue {
fn from(s: &'a str) -> HeaderValue {
HeaderValue::from(s.as_bytes())
}
}
impl Into<Bytes> for HeaderValue {
fn into(self) -> Bytes {
self.0.into()
}
}
impl AsRef<[u8]> for HeaderValue {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl AsRef<str> for HeaderValue {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}