@@ -28,81 +28,9 @@ pub struct Request {
2828 body : Vec < u8 > ,
2929}
3030
31- /// A header value.
32- ///
33- /// Since header values do not have to be valid utf8, this allows for
34- /// both utf8 strings and bags of bytes.
35- #[ derive( Debug , PartialEq , Eq , Clone ) ]
36- pub struct HeaderValue {
37- inner : HeaderValueRep ,
38- }
39-
40- #[ derive( Debug , PartialEq , Eq , Clone ) ]
41- enum HeaderValueRep {
42- /// Header value encoded as a utf8 string
43- String ( String ) ,
44- /// Header value as a bag of bytes
45- Bytes ( Vec < u8 > ) ,
46- }
47-
48- impl HeaderValue {
49- /// Construct a `HeaderValue` from a string
50- pub fn string ( str : String ) -> HeaderValue {
51- HeaderValue {
52- inner : HeaderValueRep :: String ( str) ,
53- }
54- }
55-
56- /// Construct a `HeaderValue` from a bag of bytes
57- pub fn bytes ( bytes : Vec < u8 > ) -> HeaderValue {
58- HeaderValue {
59- inner : HeaderValueRep :: Bytes ( bytes) ,
60- }
61- }
62-
63- /// Get the `HeaderValue` as a utf8 encoded string
64- ///
65- /// Returns `None` if the value is a non utf8 encoded header value
66- pub fn as_str ( & self ) -> Option < & str > {
67- match & self . inner {
68- HeaderValueRep :: String ( s) => Some ( s) ,
69- HeaderValueRep :: Bytes ( _) => None ,
70- }
71- }
72-
73- /// Get the `HeaderValue` as bytes
74- pub fn as_bytes ( & self ) -> & [ u8 ] {
75- self . as_ref ( )
76- }
77-
78- /// Turn the `HeaderValue` into a String (in a lossy way if the `HeaderValue` is a bag of bytes)
79- pub fn into_utf8_lossy ( self ) -> String {
80- match self . inner {
81- HeaderValueRep :: String ( s) => s,
82- HeaderValueRep :: Bytes ( b) => String :: from_utf8_lossy ( & b) . into_owned ( ) ,
83- }
84- }
85-
86- /// Turn the `HeaderValue` into bytes
87- pub fn into_bytes ( self ) -> Vec < u8 > {
88- match self . inner {
89- HeaderValueRep :: String ( s) => s. into_bytes ( ) ,
90- HeaderValueRep :: Bytes ( b) => b,
91- }
92- }
93- }
94-
95- impl AsRef < [ u8 ] > for HeaderValue {
96- fn as_ref ( & self ) -> & [ u8 ] {
97- match & self . inner {
98- HeaderValueRep :: String ( s) => s. as_bytes ( ) ,
99- HeaderValueRep :: Bytes ( b) => b,
100- }
101- }
102- }
103-
10431impl Request {
105- fn new ( method : Method , uri : impl Into < String > ) -> Self {
32+ /// Create a new request from a method and uri
33+ pub fn new ( method : Method , uri : impl Into < String > ) -> Self {
10634 Self {
10735 method,
10836 uri : Self :: parse_uri ( uri. into ( ) ) ,
@@ -242,7 +170,7 @@ pub struct Response {
242170}
243171
244172impl Response {
245- /// Create a new response from a status and optional headers and body
173+ /// Create a new response from a status and body
246174 pub fn new ( status : impl conversions:: IntoStatusCode , body : impl conversions:: IntoBody ) -> Self {
247175 Self {
248176 status : status. into_status_code ( ) ,
@@ -333,6 +261,79 @@ impl ResponseBuilder {
333261 }
334262}
335263
264+ /// A header value.
265+ ///
266+ /// Since header values do not have to be valid utf8, this allows for
267+ /// both utf8 strings and bags of bytes.
268+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
269+ pub struct HeaderValue {
270+ inner : HeaderValueRep ,
271+ }
272+
273+ #[ derive( Debug , PartialEq , Eq , Clone ) ]
274+ enum HeaderValueRep {
275+ /// Header value encoded as a utf8 string
276+ String ( String ) ,
277+ /// Header value as a bag of bytes
278+ Bytes ( Vec < u8 > ) ,
279+ }
280+
281+ impl HeaderValue {
282+ /// Construct a `HeaderValue` from a string
283+ pub fn string ( str : String ) -> HeaderValue {
284+ HeaderValue {
285+ inner : HeaderValueRep :: String ( str) ,
286+ }
287+ }
288+
289+ /// Construct a `HeaderValue` from a bag of bytes
290+ pub fn bytes ( bytes : Vec < u8 > ) -> HeaderValue {
291+ HeaderValue {
292+ inner : HeaderValueRep :: Bytes ( bytes) ,
293+ }
294+ }
295+
296+ /// Get the `HeaderValue` as a utf8 encoded string
297+ ///
298+ /// Returns `None` if the value is a non utf8 encoded header value
299+ pub fn as_str ( & self ) -> Option < & str > {
300+ match & self . inner {
301+ HeaderValueRep :: String ( s) => Some ( s) ,
302+ HeaderValueRep :: Bytes ( _) => None ,
303+ }
304+ }
305+
306+ /// Get the `HeaderValue` as bytes
307+ pub fn as_bytes ( & self ) -> & [ u8 ] {
308+ self . as_ref ( )
309+ }
310+
311+ /// Turn the `HeaderValue` into a String (in a lossy way if the `HeaderValue` is a bag of bytes)
312+ pub fn into_utf8_lossy ( self ) -> String {
313+ match self . inner {
314+ HeaderValueRep :: String ( s) => s,
315+ HeaderValueRep :: Bytes ( b) => String :: from_utf8_lossy ( & b) . into_owned ( ) ,
316+ }
317+ }
318+
319+ /// Turn the `HeaderValue` into bytes
320+ pub fn into_bytes ( self ) -> Vec < u8 > {
321+ match self . inner {
322+ HeaderValueRep :: String ( s) => s. into_bytes ( ) ,
323+ HeaderValueRep :: Bytes ( b) => b,
324+ }
325+ }
326+ }
327+
328+ impl AsRef < [ u8 ] > for HeaderValue {
329+ fn as_ref ( & self ) -> & [ u8 ] {
330+ match & self . inner {
331+ HeaderValueRep :: String ( s) => s. as_bytes ( ) ,
332+ HeaderValueRep :: Bytes ( b) => b,
333+ }
334+ }
335+ }
336+
336337fn into_header_rep ( headers : impl conversions:: IntoHeaders ) -> HashMap < String , HeaderValue > {
337338 headers
338339 . into_headers ( )
0 commit comments