2
2
3
3
use super :: { Error , HttpClient , Request , Response } ;
4
4
use http_types:: headers:: { HeaderName , HeaderValue } ;
5
- use http_types:: { Method , StatusCode , Version } ;
5
+ use http_types:: StatusCode ;
6
6
use hyper:: body:: HttpBody ;
7
7
use hyper_tls:: HttpsConnector ;
8
- use std:: convert:: { TryFrom , TryInto } ;
8
+ use std:: convert:: TryFrom ;
9
9
use std:: str:: FromStr ;
10
10
11
11
/// Hyper-based HTTP Client.
@@ -53,43 +53,10 @@ struct HyperHttpRequest {
53
53
54
54
impl HyperHttpRequest {
55
55
async fn try_from ( mut value : Request ) -> Result < Self , Error > {
56
- let method = match value. method ( ) {
57
- Method :: Get => hyper:: Method :: GET ,
58
- Method :: Head => hyper:: Method :: HEAD ,
59
- Method :: Post => hyper:: Method :: POST ,
60
- Method :: Put => hyper:: Method :: PUT ,
61
- Method :: Patch => hyper:: Method :: PATCH ,
62
- Method :: Options => hyper:: Method :: OPTIONS ,
63
- Method :: Trace => hyper:: Method :: TRACE ,
64
- Method :: Connect => hyper:: Method :: CONNECT ,
65
- _ => {
66
- return Err ( Error :: from_str (
67
- StatusCode :: BadRequest ,
68
- "unrecognized HTTP method" ,
69
- ) )
70
- }
71
- } ;
72
-
73
- let version = value
74
- . version ( )
75
- . map ( |v| match v {
76
- Version :: Http0_9 => Ok ( hyper:: Version :: HTTP_09 ) ,
77
- Version :: Http1_0 => Ok ( hyper:: Version :: HTTP_10 ) ,
78
- Version :: Http1_1 => Ok ( hyper:: Version :: HTTP_11 ) ,
79
- Version :: Http2_0 => Ok ( hyper:: Version :: HTTP_2 ) ,
80
- Version :: Http3_0 => Ok ( hyper:: Version :: HTTP_3 ) ,
81
- _ => Err ( Error :: from_str (
82
- StatusCode :: BadRequest ,
83
- "unrecognized HTTP version" ,
84
- ) ) ,
85
- } )
86
- . or ( Some ( Ok ( hyper:: Version :: default ( ) ) ) )
87
- . unwrap ( ) ?;
88
-
89
56
// UNWRAP: This unwrap is unjustified in `http-types`, need to check if it's actually safe.
90
57
let uri = hyper:: Uri :: try_from ( & format ! ( "{}" , value. url( ) ) ) . unwrap ( ) ;
91
58
92
- // `HttpClient ` depends on the scheme being either "http" or "https"
59
+ // `HyperClient ` depends on the scheme being either "http" or "https"
93
60
match uri. scheme_str ( ) {
94
61
Some ( "http" ) | Some ( "https" ) => ( ) ,
95
62
_ => return Err ( Error :: from_str ( StatusCode :: BadRequest , "invalid scheme" ) ) ,
@@ -114,13 +81,13 @@ impl HyperHttpRequest {
114
81
let body = value. body_bytes ( ) . await ?;
115
82
let body = hyper:: Body :: from ( body) ;
116
83
117
- let req = hyper :: Request :: builder ( )
118
- . method ( method)
119
- . version ( version)
84
+ let request = request
85
+ . method ( value . method ( ) )
86
+ . version ( value . version ( ) . map ( |v| v . into ( ) ) . unwrap_or_default ( ) )
120
87
. uri ( uri)
121
88
. body ( body) ?;
122
89
123
- Ok ( HyperHttpRequest { inner : req } )
90
+ Ok ( HyperHttpRequest { inner : request } )
124
91
}
125
92
126
93
fn into_inner ( self ) -> hyper:: Request < hyper:: Body > {
@@ -136,21 +103,6 @@ impl HttpTypesResponse {
136
103
async fn try_from ( value : hyper:: Response < hyper:: Body > ) -> Result < Self , Error > {
137
104
let ( parts, mut body) = value. into_parts ( ) ;
138
105
139
- // UNWRAP: http and http-types implement the same status codes
140
- let status: StatusCode = parts. status . as_u16 ( ) . try_into ( ) . unwrap ( ) ;
141
-
142
- let version = match parts. version {
143
- hyper:: Version :: HTTP_09 => Ok ( http_types:: Version :: Http0_9 ) ,
144
- hyper:: Version :: HTTP_10 => Ok ( http_types:: Version :: Http1_0 ) ,
145
- hyper:: Version :: HTTP_11 => Ok ( http_types:: Version :: Http1_1 ) ,
146
- hyper:: Version :: HTTP_2 => Ok ( http_types:: Version :: Http2_0 ) ,
147
- hyper:: Version :: HTTP_3 => Ok ( http_types:: Version :: Http3_0 ) ,
148
- _ => Err ( Error :: from_str (
149
- StatusCode :: BadGateway ,
150
- "unrecognized HTTP response version" ,
151
- ) ) ,
152
- } ?;
153
-
154
106
let body = match body. data ( ) . await {
155
107
None => None ,
156
108
Some ( Ok ( b) ) => Some ( b) ,
@@ -164,8 +116,8 @@ impl HttpTypesResponse {
164
116
. map ( |b| http_types:: Body :: from_bytes ( b. to_vec ( ) ) )
165
117
. unwrap_or ( http_types:: Body :: empty ( ) ) ;
166
118
167
- let mut res = Response :: new ( status) ;
168
- res. set_version ( Some ( version) ) ;
119
+ let mut res = Response :: new ( parts . status ) ;
120
+ res. set_version ( Some ( parts . version . into ( ) ) ) ;
169
121
170
122
for ( name, value) in parts. headers {
171
123
let value = value. as_bytes ( ) . to_owned ( ) ;
0 commit comments