|
| 1 | +//! Helper traits to convert between http 0.2 and http 1.x. |
| 2 | +
|
| 3 | +/// Convert from http 1.x to http 0.2 |
| 4 | +pub trait ToHttpOld { |
| 5 | + type Output; |
| 6 | + |
| 7 | + fn to_http_old(self) -> Self::Output; |
| 8 | +} |
| 9 | + |
| 10 | +impl<T> ToHttpOld for http::Response<T> { |
| 11 | + type Output = http_old::Response<T>; |
| 12 | + |
| 13 | + fn to_http_old(self) -> Self::Output { |
| 14 | + let (parts, body) = self.into_parts(); |
| 15 | + let mut response = http_old::Response::new(body); |
| 16 | + |
| 17 | + *response.status_mut() = parts.status.to_http_old(); |
| 18 | + *response.version_mut() = parts.version.to_http_old(); |
| 19 | + *response.headers_mut() = parts.headers.to_http_old(); |
| 20 | + // We cannot do anything about extensions because we cannot iterate over them. |
| 21 | + |
| 22 | + response |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +impl ToHttpOld for http::StatusCode { |
| 27 | + type Output = http_old::StatusCode; |
| 28 | + |
| 29 | + fn to_http_old(self) -> Self::Output { |
| 30 | + http_old::StatusCode::from_u16(self.as_u16()) |
| 31 | + .expect("status code should be valid between both versions") |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl ToHttpOld for http::Version { |
| 36 | + type Output = http_old::Version; |
| 37 | + |
| 38 | + fn to_http_old(self) -> Self::Output { |
| 39 | + if self == http::Version::HTTP_09 { |
| 40 | + http_old::Version::HTTP_09 |
| 41 | + } else if self == http::Version::HTTP_10 { |
| 42 | + http_old::Version::HTTP_10 |
| 43 | + } else if self == http::Version::HTTP_11 { |
| 44 | + http_old::Version::HTTP_11 |
| 45 | + } else if self == http::Version::HTTP_2 { |
| 46 | + http_old::Version::HTTP_2 |
| 47 | + } else if self == http::Version::HTTP_3 { |
| 48 | + http_old::Version::HTTP_3 |
| 49 | + } else { |
| 50 | + // Current http code doesn't have other variants. |
| 51 | + unreachable!() |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl ToHttpOld for http::HeaderMap<http::HeaderValue> { |
| 57 | + type Output = http_old::HeaderMap<http_old::HeaderValue>; |
| 58 | + |
| 59 | + fn to_http_old(self) -> Self::Output { |
| 60 | + let mut map = http_old::HeaderMap::new(); |
| 61 | + map.extend( |
| 62 | + self.into_iter() |
| 63 | + .map(|(name, value)| (name.map(ToHttpOld::to_http_old), value.to_http_old())), |
| 64 | + ); |
| 65 | + |
| 66 | + map |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl ToHttpOld for http::HeaderName { |
| 71 | + type Output = http_old::HeaderName; |
| 72 | + |
| 73 | + fn to_http_old(self) -> Self::Output { |
| 74 | + http_old::HeaderName::from_bytes(self.as_ref()) |
| 75 | + .expect("header name should be valid between both versions") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl ToHttpOld for http::HeaderValue { |
| 80 | + type Output = http_old::HeaderValue; |
| 81 | + |
| 82 | + fn to_http_old(self) -> Self::Output { |
| 83 | + http_old::HeaderValue::from_bytes(self.as_bytes()) |
| 84 | + .expect("header value should be valid between both versions") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// Convert from http 0.2 to http 1.x |
| 89 | +pub trait ToHttpNew { |
| 90 | + type Output; |
| 91 | + |
| 92 | + fn to_http_new(self) -> Self::Output; |
| 93 | +} |
| 94 | + |
| 95 | +impl<T> ToHttpNew for http_old::Request<T> { |
| 96 | + type Output = http::Request<T>; |
| 97 | + |
| 98 | + fn to_http_new(self) -> Self::Output { |
| 99 | + let (parts, body) = self.into_parts(); |
| 100 | + let mut request = http::Request::new(body); |
| 101 | + |
| 102 | + *request.method_mut() = parts.method.to_http_new(); |
| 103 | + *request.uri_mut() = parts.uri.to_http_new(); |
| 104 | + *request.version_mut() = parts.version.to_http_new(); |
| 105 | + *request.headers_mut() = parts.headers.to_http_new(); |
| 106 | + // We cannot do anything about extensions because we cannot iterate over them. |
| 107 | + |
| 108 | + request |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +impl ToHttpNew for http_old::Method { |
| 113 | + type Output = http::Method; |
| 114 | + |
| 115 | + fn to_http_new(self) -> Self::Output { |
| 116 | + self.as_str().parse().expect("method should be valid between both versions") |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl ToHttpNew for http_old::Uri { |
| 121 | + type Output = http::Uri; |
| 122 | + |
| 123 | + fn to_http_new(self) -> Self::Output { |
| 124 | + self.to_string().parse().expect("URI should be valid between both versions") |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl ToHttpNew for http_old::Version { |
| 129 | + type Output = http::Version; |
| 130 | + |
| 131 | + fn to_http_new(self) -> Self::Output { |
| 132 | + if self == http_old::Version::HTTP_09 { |
| 133 | + http::Version::HTTP_09 |
| 134 | + } else if self == http_old::Version::HTTP_10 { |
| 135 | + http::Version::HTTP_10 |
| 136 | + } else if self == http_old::Version::HTTP_11 { |
| 137 | + http::Version::HTTP_11 |
| 138 | + } else if self == http_old::Version::HTTP_2 { |
| 139 | + http::Version::HTTP_2 |
| 140 | + } else if self == http_old::Version::HTTP_3 { |
| 141 | + http::Version::HTTP_3 |
| 142 | + } else { |
| 143 | + // Current http code doesn't have other variants. |
| 144 | + unreachable!() |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +impl ToHttpNew for http_old::HeaderMap<http_old::HeaderValue> { |
| 150 | + type Output = http::HeaderMap<http::HeaderValue>; |
| 151 | + |
| 152 | + fn to_http_new(self) -> Self::Output { |
| 153 | + let mut map = http::HeaderMap::new(); |
| 154 | + map.extend( |
| 155 | + self.into_iter() |
| 156 | + .map(|(name, value)| (name.map(ToHttpNew::to_http_new), value.to_http_new())), |
| 157 | + ); |
| 158 | + |
| 159 | + map |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +impl ToHttpNew for http_old::HeaderName { |
| 164 | + type Output = http::HeaderName; |
| 165 | + |
| 166 | + fn to_http_new(self) -> Self::Output { |
| 167 | + http::HeaderName::from_bytes(self.as_ref()) |
| 168 | + .expect("header name should be valid between both versions") |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl ToHttpNew for http_old::HeaderValue { |
| 173 | + type Output = http::HeaderValue; |
| 174 | + |
| 175 | + fn to_http_new(self) -> Self::Output { |
| 176 | + http::HeaderValue::from_bytes(self.as_bytes()) |
| 177 | + .expect("header value should be valid between both versions") |
| 178 | + } |
| 179 | +} |
0 commit comments