|
| 1 | +pub use http; |
| 2 | + |
| 3 | +use core::fmt::Display; |
| 4 | + |
| 5 | +impl From<http::Method> for crate::http::types::Method { |
| 6 | + fn from(method: http::Method) -> Self { |
| 7 | + use std::string::ToString; |
| 8 | + |
| 9 | + match method.as_str() { |
| 10 | + "GET" => Self::Get, |
| 11 | + "HEAD" => Self::Head, |
| 12 | + "POST" => Self::Post, |
| 13 | + "PUT" => Self::Put, |
| 14 | + "DELETE" => Self::Delete, |
| 15 | + "CONNECT" => Self::Connect, |
| 16 | + "OPTIONS" => Self::Options, |
| 17 | + "TRACE" => Self::Trace, |
| 18 | + "PATCH" => Self::Patch, |
| 19 | + _ => Self::Other(method.to_string()), |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl TryFrom<crate::http::types::Method> for http::Method { |
| 25 | + type Error = http::method::InvalidMethod; |
| 26 | + |
| 27 | + fn try_from(method: crate::http::types::Method) -> Result<Self, Self::Error> { |
| 28 | + match method { |
| 29 | + crate::http::types::Method::Get => Ok(Self::GET), |
| 30 | + crate::http::types::Method::Head => Ok(Self::HEAD), |
| 31 | + crate::http::types::Method::Post => Ok(Self::POST), |
| 32 | + crate::http::types::Method::Put => Ok(Self::PUT), |
| 33 | + crate::http::types::Method::Delete => Ok(Self::DELETE), |
| 34 | + crate::http::types::Method::Connect => Ok(Self::CONNECT), |
| 35 | + crate::http::types::Method::Options => Ok(Self::OPTIONS), |
| 36 | + crate::http::types::Method::Trace => Ok(Self::TRACE), |
| 37 | + crate::http::types::Method::Patch => Ok(Self::PATCH), |
| 38 | + crate::http::types::Method::Other(method) => method.parse(), |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl From<http::uri::Scheme> for crate::http::types::Scheme { |
| 44 | + fn from(scheme: http::uri::Scheme) -> Self { |
| 45 | + use std::string::ToString; |
| 46 | + |
| 47 | + match scheme.as_str() { |
| 48 | + "http" => Self::Http, |
| 49 | + "https" => Self::Https, |
| 50 | + _ => Self::Other(scheme.to_string()), |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl TryFrom<crate::http::types::Scheme> for http::uri::Scheme { |
| 56 | + type Error = http::uri::InvalidUri; |
| 57 | + |
| 58 | + fn try_from(scheme: crate::http::types::Scheme) -> Result<Self, Self::Error> { |
| 59 | + match scheme { |
| 60 | + crate::http::types::Scheme::Http => Ok(Self::HTTP), |
| 61 | + crate::http::types::Scheme::Https => Ok(Self::HTTPS), |
| 62 | + crate::http::types::Scheme::Other(scheme) => scheme.parse(), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +#[derive(Debug)] |
| 68 | +pub enum FieldsToHeaderMapError { |
| 69 | + InvalidHeaderName(http::header::InvalidHeaderName), |
| 70 | + InvalidHeaderValue(http::header::InvalidHeaderValue), |
| 71 | +} |
| 72 | + |
| 73 | +impl Display for FieldsToHeaderMapError { |
| 74 | + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 75 | + match self { |
| 76 | + FieldsToHeaderMapError::InvalidHeaderName(e) => write!(f, "invalid header name: {e}"), |
| 77 | + FieldsToHeaderMapError::InvalidHeaderValue(e) => write!(f, "invalid header value: {e}"), |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl std::error::Error for FieldsToHeaderMapError {} |
| 83 | + |
| 84 | +impl TryFrom<crate::http::types::Fields> for http::HeaderMap { |
| 85 | + type Error = FieldsToHeaderMapError; |
| 86 | + |
| 87 | + fn try_from(fields: crate::http::types::Fields) -> Result<Self, Self::Error> { |
| 88 | + let mut headers = http::HeaderMap::new(); |
| 89 | + for (name, value) in fields.entries() { |
| 90 | + let name = http::HeaderName::try_from(name) |
| 91 | + .map_err(FieldsToHeaderMapError::InvalidHeaderName)?; |
| 92 | + let value = http::HeaderValue::try_from(value) |
| 93 | + .map_err(FieldsToHeaderMapError::InvalidHeaderValue)?; |
| 94 | + match headers.entry(name) { |
| 95 | + http::header::Entry::Vacant(entry) => { |
| 96 | + entry.insert(value); |
| 97 | + } |
| 98 | + http::header::Entry::Occupied(mut entry) => { |
| 99 | + entry.append(value); |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + Ok(headers) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl From<http::HeaderMap> for crate::http::types::Fields { |
| 108 | + fn from(headers: http::HeaderMap) -> Self { |
| 109 | + use std::string::ToString; |
| 110 | + |
| 111 | + let fields = crate::http::types::Fields::new(); |
| 112 | + for (name, value) in headers.iter() { |
| 113 | + fields |
| 114 | + .append(&name.to_string(), &value.as_bytes().to_vec()) |
| 115 | + .expect("failed to append header") |
| 116 | + } |
| 117 | + fields |
| 118 | + } |
| 119 | +} |
0 commit comments