|
| 1 | +extern crate http; |
| 2 | + |
| 3 | +pub use http::header::{self, HeaderName, HeaderValue}; |
| 4 | + |
| 5 | +mod error; |
| 6 | +pub mod parsing; |
| 7 | + |
| 8 | +pub use self::error::{Error, Result}; |
| 9 | + |
| 10 | +/// A trait for any object that will represent a header field and value. |
| 11 | +/// |
| 12 | +/// This trait represents the construction and identification of headers, |
| 13 | +/// and contains trait-object unsafe methods. |
| 14 | +pub trait Header { |
| 15 | + /// The name of this header. |
| 16 | + const NAME: &'static HeaderName; |
| 17 | + |
| 18 | + /// Decode this type from a `HeaderValue`. |
| 19 | + fn decode(values: &mut Values) -> Result<Self> |
| 20 | + where |
| 21 | + Self: Sized; |
| 22 | + |
| 23 | + /// Encode this type to a `HeaderMap`. |
| 24 | + /// |
| 25 | + /// This function should be infallible. Any errors converting to a |
| 26 | + /// `HeaderValue` should have been caught when parsing or constructing |
| 27 | + /// this value. |
| 28 | + fn encode(&self, values: &mut ToValues); |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug)] |
| 32 | +pub struct Values<'a> { |
| 33 | + inner: http::header::ValueIter<'a, http::header::HeaderValue>, |
| 34 | +} |
| 35 | + |
| 36 | +impl<'a> Iterator for Values<'a> { |
| 37 | + type Item = &'a HeaderValue; |
| 38 | + |
| 39 | + #[inline] |
| 40 | + fn next(&mut self) -> Option<Self::Item> { |
| 41 | + self.inner.next() |
| 42 | + } |
| 43 | + |
| 44 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 45 | + self.inner.size_hint() |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub struct ToValues<'a> { |
| 50 | + state: State<'a>, |
| 51 | +} |
| 52 | + |
| 53 | +enum State<'a> { |
| 54 | + First(http::header::Entry<'a, HeaderValue>), |
| 55 | + Latter(http::header::OccupiedEntry<'a, HeaderValue>), |
| 56 | + Tmp, |
| 57 | +} |
| 58 | + |
| 59 | +impl<'a> ToValues<'a> { |
| 60 | + pub fn append(&mut self, value: HeaderValue) { |
| 61 | + let entry = match ::std::mem::replace(&mut self.state, State::Tmp) { |
| 62 | + State::First(http::header::Entry::Occupied(mut e)) => { |
| 63 | + e.insert(value); |
| 64 | + e |
| 65 | + }, |
| 66 | + State::First(http::header::Entry::Vacant(e)) => e.insert_entry(value), |
| 67 | + State::Latter(mut e) => { |
| 68 | + e.append(value); |
| 69 | + e |
| 70 | + }, |
| 71 | + State::Tmp => unreachable!("ToValues State::Tmp"), |
| 72 | + }; |
| 73 | + self.state = State::Latter(entry); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +pub trait HeaderMapExt: self::sealed::Sealed { |
| 78 | + fn typed_insert<H>(&mut self, header: H) |
| 79 | + where |
| 80 | + H: Header; |
| 81 | +} |
| 82 | + |
| 83 | +impl HeaderMapExt for http::HeaderMap { |
| 84 | + fn typed_insert<H>(&mut self, header: H) |
| 85 | + where |
| 86 | + H: Header, |
| 87 | + { |
| 88 | + let entry = self |
| 89 | + .entry(H::NAME) |
| 90 | + .expect("HeaderName is always valid"); |
| 91 | + let mut values = ToValues { |
| 92 | + state: State::First(entry), |
| 93 | + }; |
| 94 | + header.encode(&mut values); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +mod sealed { |
| 99 | + pub trait Sealed {} |
| 100 | + impl Sealed for ::http::HeaderMap {} |
| 101 | +} |
0 commit comments