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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use std::convert::TryFrom;
use std::net::SocketAddr;
use hyper_http::method::Method as HMethod;
use hyper_http::uri::{
Uri as HUri,
Scheme as HScheme,
Authority as HAuthority
};
use hyper_http::version::Version as HVersion;
use hyper_http::status::StatusCode as HStatusCode;
use hyper_http::header::{
HeaderMap as HHeaderMap,
HeaderValue as HHeaderValue
};
use hyper_http::response::Parts as HParts;
use http::header::{ RequestHeader, Method, Version, Uri, HeaderValues };
use http::header::{ ResponseHeader, StatusCode };
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HeaderError {
Method(HMethod),
NoHost,
HostInvalid,
Uri,
Version(HVersion),
Unknown
}
type Result<T> = std::result::Result<T, HeaderError>;
pub fn convert_hyper_parts_to_fire_header(
parts: hyper_http::request::Parts,
address: SocketAddr
) -> Result<RequestHeader> {
let method = convert_hyper_method_to_fire_method( parts.method )?;
let uri = convert_hyper_uri_to_fire_uri( parts.uri, &parts.headers )?;
let version = convert_hyper_version_to_fire_version( parts.version )?;
let values = convert_hyper_values_to_values( parts.headers )?;
Ok( RequestHeader { address, method, uri, version, values } )
}
fn convert_hyper_method_to_fire_method(method: HMethod) -> Result<Method> {
Ok(match method {
HMethod::GET => Method::Get,
HMethod::POST => Method::Post,
HMethod::PUT => Method::Put,
HMethod::DELETE => Method::Delete,
HMethod::HEAD => Method::Head,
HMethod::OPTIONS => Method::Options,
HMethod::CONNECT => Method::Connect,
HMethod::PATCH => Method::Patch,
HMethod::TRACE => Method::Trace,
_ => return Err(HeaderError::Method(method))
})
}
fn convert_hyper_uri_to_fire_uri(uri: HUri, headers: &HHeaderMap<HHeaderValue>) -> Result<Uri> {
let mut parts = uri.into_parts();
parts.scheme = Some(HScheme::HTTP);
let host = headers.get("host").ok_or(HeaderError::NoHost)?;
parts.authority = Some(
HAuthority::try_from(host.as_bytes())
.map_err(|_| HeaderError::HostInvalid)?
);
let uri = HUri::from_parts(parts)
.map_err(|_| HeaderError::Uri)?;
Uri::new(uri).ok_or(HeaderError::Uri)
}
fn convert_hyper_version_to_fire_version(version: HVersion) -> Result<Version> {
Ok(match version {
HVersion::HTTP_10 => Version::One,
HVersion::HTTP_11 => Version::OnePointOne,
HVersion::HTTP_2 => Version::Two,
HVersion::HTTP_3 => Version::Three,
_ => return Err(HeaderError::Version(version))
})
}
fn convert_hyper_values_to_values( header: HHeaderMap<HHeaderValue> ) -> Result<HeaderValues> {
Ok(HeaderValues::from_inner(header))
}
pub fn convert_fire_header_to_hyper_header(parts: &mut HParts, mut header: ResponseHeader) {
parts.version = convert_fire_version_to_hyper_version(header.version);
parts.status = convert_fire_status_to_hyper_status(header.status_code);
let content_type = header.content_type.to_string();
if !content_type.is_empty() {
header.values.insert("content-type", content_type);
}
parts.headers = header.values.into_inner();
}
fn convert_fire_version_to_hyper_version(version: Version) -> HVersion {
match version {
Version::One => HVersion::HTTP_10,
Version::OnePointOne => HVersion::HTTP_11,
Version::Two => HVersion::HTTP_2,
Version::Three => HVersion::HTTP_3
}
}
fn convert_fire_status_to_hyper_status(status: StatusCode) -> HStatusCode {
HStatusCode::from_u16( status.code() ).unwrap()
}