@@ -27,7 +27,7 @@ use url::ParseError as UrlError;
27
27
use header:: { Headers , Header , HeaderFormat } ;
28
28
use header:: { ContentLength , Location } ;
29
29
use method:: Method ;
30
- use net:: { NetworkConnector , HttpConnector , ContextVerifier } ;
30
+ use net:: { NetworkConnector , NetworkStream , HttpConnector , ContextVerifier } ;
31
31
use status:: StatusClass :: Redirection ;
32
32
use { Url , HttpResult } ;
33
33
use HttpError :: HttpUriError ;
@@ -41,68 +41,65 @@ pub mod response;
41
41
/// A Client to use additional features with Requests.
42
42
///
43
43
/// Clients can handle things such as: redirect policy.
44
- pub struct Client < C > {
45
- connector : C ,
44
+ pub struct Client {
45
+ connector : Connector ,
46
46
redirect_policy : RedirectPolicy ,
47
47
}
48
48
49
- impl < ' v > Client < HttpConnector < ' v > > {
49
+ impl Client {
50
50
51
51
/// Create a new Client.
52
- pub fn new ( ) -> Client < HttpConnector < ' v > > {
52
+ pub fn new ( ) -> Client {
53
53
Client :: with_connector ( HttpConnector ( None ) )
54
54
}
55
55
56
- /// Set the SSL verifier callback for use with OpenSSL.
57
- pub fn set_ssl_verifier ( & mut self , verifier : ContextVerifier < ' v > ) {
58
- self . connector = HttpConnector ( Some ( verifier) ) ;
59
- }
60
-
61
- }
62
-
63
- impl < C : NetworkConnector > Client < C > {
64
-
65
56
/// Create a new client with a specific connector.
66
- pub fn with_connector ( connector : C ) -> Client < C > {
57
+ pub fn with_connector < C , S > ( connector : C ) -> Client
58
+ where C : NetworkConnector < Stream =S > + Send + ' static , S : NetworkStream + Send {
67
59
Client {
68
- connector : connector,
60
+ connector : with_connector ( connector) ,
69
61
redirect_policy : Default :: default ( )
70
62
}
71
63
}
72
64
65
+ /// Set the SSL verifier callback for use with OpenSSL.
66
+ pub fn set_ssl_verifier ( & mut self , verifier : ContextVerifier ) {
67
+ self . connector = with_connector ( HttpConnector ( Some ( verifier) ) ) ;
68
+ }
69
+
73
70
/// Set the RedirectPolicy.
74
71
pub fn set_redirect_policy ( & mut self , policy : RedirectPolicy ) {
75
72
self . redirect_policy = policy;
76
73
}
77
74
78
75
/// Build a Get request.
79
- pub fn get < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U , C > {
76
+ pub fn get < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
80
77
self . request ( Method :: Get , url)
81
78
}
82
79
83
80
/// Build a Head request.
84
- pub fn head < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U , C > {
81
+ pub fn head < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
85
82
self . request ( Method :: Head , url)
86
83
}
87
84
88
85
/// Build a Post request.
89
- pub fn post < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U , C > {
86
+ pub fn post < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
90
87
self . request ( Method :: Post , url)
91
88
}
92
89
93
90
/// Build a Put request.
94
- pub fn put < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U , C > {
91
+ pub fn put < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
95
92
self . request ( Method :: Put , url)
96
93
}
97
94
98
95
/// Build a Delete request.
99
- pub fn delete < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U , C > {
96
+ pub fn delete < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
100
97
self . request ( Method :: Delete , url)
101
98
}
102
99
103
100
104
101
/// Build a new request using this Client.
105
- pub fn request < U : IntoUrl > ( & mut self , method : Method , url : U ) -> RequestBuilder < U , C > {
102
+ pub fn request < U : IntoUrl > ( & mut self , method : Method , url : U ) -> RequestBuilder < U > {
106
103
RequestBuilder {
107
104
client : self ,
108
105
method : method,
@@ -113,34 +110,60 @@ impl<C: NetworkConnector> Client<C> {
113
110
}
114
111
}
115
112
113
+ fn with_connector < C : NetworkConnector < Stream =S > + Send + ' static , S : NetworkStream + Send > ( c : C ) -> Connector {
114
+ Connector ( Box :: new ( ConnAdapter ( c) ) )
115
+ }
116
+
117
+ struct ConnAdapter < C : NetworkConnector + Send > ( C ) ;
118
+
119
+ impl < C : NetworkConnector < Stream =S > + Send , S : NetworkStream + Send > NetworkConnector for ConnAdapter < C > {
120
+ type Stream = Box < NetworkStream + Send > ;
121
+ #[ inline]
122
+ fn connect ( & mut self , host : & str , port : u16 , scheme : & str )
123
+ -> io:: Result < Box < NetworkStream + Send > > {
124
+ Ok ( try!( self . 0 . connect ( host, port, scheme) ) . into ( ) )
125
+ }
126
+ }
127
+
128
+ struct Connector ( Box < NetworkConnector < Stream =Box < NetworkStream + Send > > + Send > ) ;
129
+
130
+ impl NetworkConnector for Connector {
131
+ type Stream = Box < NetworkStream + Send > ;
132
+ #[ inline]
133
+ fn connect ( & mut self , host : & str , port : u16 , scheme : & str )
134
+ -> io:: Result < Box < NetworkStream + Send > > {
135
+ Ok ( try!( self . 0 . connect ( host, port, scheme) ) . into ( ) )
136
+ }
137
+ }
138
+
116
139
/// Options for an individual Request.
117
140
///
118
141
/// One of these will be built for you if you use one of the convenience
119
142
/// methods, such as `get()`, `post()`, etc.
120
- pub struct RequestBuilder < ' a , U : IntoUrl , C : NetworkConnector + ' a > {
121
- client : & ' a mut Client < C > ,
143
+ pub struct RequestBuilder < ' a , U : IntoUrl > {
144
+ client : & ' a mut Client ,
122
145
url : U ,
123
146
headers : Option < Headers > ,
124
147
method : Method ,
125
148
body : Option < Body < ' a > > ,
126
149
}
127
150
128
- impl < ' a , U : IntoUrl , C : NetworkConnector > RequestBuilder < ' a , U , C > {
151
+ impl < ' a , U : IntoUrl > RequestBuilder < ' a , U > {
129
152
130
153
/// Set a request body to be sent.
131
- pub fn body < B : IntoBody < ' a > > ( mut self , body : B ) -> RequestBuilder < ' a , U , C > {
154
+ pub fn body < B : IntoBody < ' a > > ( mut self , body : B ) -> RequestBuilder < ' a , U > {
132
155
self . body = Some ( body. into_body ( ) ) ;
133
156
self
134
157
}
135
158
136
159
/// Add additional headers to the request.
137
- pub fn headers ( mut self , headers : Headers ) -> RequestBuilder < ' a , U , C > {
160
+ pub fn headers ( mut self , headers : Headers ) -> RequestBuilder < ' a , U > {
138
161
self . headers = Some ( headers) ;
139
162
self
140
163
}
141
164
142
165
/// Add an individual new header to the request.
143
- pub fn header < H : Header + HeaderFormat > ( mut self , header : H ) -> RequestBuilder < ' a , U , C > {
166
+ pub fn header < H : Header + HeaderFormat > ( mut self , header : H ) -> RequestBuilder < ' a , U > {
144
167
{
145
168
let mut headers = match self . headers {
146
169
Some ( ref mut h) => h,
0 commit comments