Skip to content

Commit 1c34a05

Browse files
committed
feat(client): add HttpConnector.enforce_http
This will make the `HttpConnector` require the `scheme` to be `http`, and return an error otherwise. This value is enabled by default, so any requests to URLs that aren't of scheme `http` will now see an error message stating the failure. When constructing a connector that wraps an `HttpConnector`, this enforcement can be disabled to allow connecting over TCP easily even when the scheme is not `http`. To do, call `connector.enforce_http(false)`.
1 parent f1859df commit 1c34a05

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed

src/client/connect.rs

+72-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::error::Error as StdError;
12
use std::fmt;
23
use std::io;
34
//use std::net::SocketAddr;
@@ -42,6 +43,7 @@ where T: Service<Request=Uri, Error=io::Error> + 'static,
4243
#[derive(Clone)]
4344
pub struct HttpConnector {
4445
dns: dns::Dns,
46+
enforce_http: bool,
4547
handle: Handle,
4648
}
4749

@@ -50,15 +52,26 @@ impl HttpConnector {
5052
/// Construct a new HttpConnector.
5153
///
5254
/// Takes number of DNS worker threads.
55+
#[inline]
5356
pub fn new(threads: usize, handle: &Handle) -> HttpConnector {
5457
HttpConnector {
5558
dns: dns::Dns::new(threads),
59+
enforce_http: true,
5660
handle: handle.clone(),
5761
}
5862
}
63+
64+
/// Option to enforce all `Uri`s have the `http` scheme.
65+
///
66+
/// Enabled by default.
67+
#[inline]
68+
pub fn enforce_http(&mut self, is_enforced: bool) {
69+
self.enforce_http = is_enforced;
70+
}
5971
}
6072

6173
impl fmt::Debug for HttpConnector {
74+
#[inline]
6275
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
6376
f.debug_struct("HttpConnector")
6477
.finish()
@@ -73,12 +86,18 @@ impl Service for HttpConnector {
7386

7487
fn call(&self, uri: Uri) -> Self::Future {
7588
debug!("Http::connect({:?})", uri);
89+
90+
if self.enforce_http {
91+
if uri.scheme() != Some("http") {
92+
return invalid_url(InvalidUrl::NotHttp, &self.handle);
93+
}
94+
} else if uri.scheme().is_none() {
95+
return invalid_url(InvalidUrl::MissingScheme, &self.handle);
96+
}
97+
7698
let host = match uri.host() {
7799
Some(s) => s,
78-
None => return HttpConnecting {
79-
state: State::Error(Some(io::Error::new(io::ErrorKind::InvalidInput, "invalid url"))),
80-
handle: self.handle.clone(),
81-
},
100+
None => return invalid_url(InvalidUrl::MissingAuthority, &self.handle),
82101
};
83102
let port = match uri.port() {
84103
Some(port) => port,
@@ -94,7 +113,37 @@ impl Service for HttpConnector {
94113
handle: self.handle.clone(),
95114
}
96115
}
116+
}
117+
118+
#[inline]
119+
fn invalid_url(err: InvalidUrl, handle: &Handle) -> HttpConnecting {
120+
HttpConnecting {
121+
state: State::Error(Some(io::Error::new(io::ErrorKind::InvalidInput, err))),
122+
handle: handle.clone(),
123+
}
124+
}
125+
126+
#[derive(Debug, Clone, Copy)]
127+
enum InvalidUrl {
128+
MissingScheme,
129+
NotHttp,
130+
MissingAuthority,
131+
}
132+
133+
impl fmt::Display for InvalidUrl {
134+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135+
f.write_str(self.description())
136+
}
137+
}
97138

139+
impl StdError for InvalidUrl {
140+
fn description(&self) -> &str {
141+
match *self {
142+
InvalidUrl::MissingScheme => "invalid URL, missing scheme",
143+
InvalidUrl::NotHttp => "invalid URL, scheme must be http",
144+
InvalidUrl::MissingAuthority => "invalid URL, missing domain",
145+
}
146+
}
98147
}
99148

100149
/// A Future representing work to connect to a URL.
@@ -195,12 +244,30 @@ mod tests {
195244
use super::{Connect, HttpConnector};
196245

197246
#[test]
198-
fn test_non_http_url() {
247+
fn test_errors_missing_authority() {
199248
let mut core = Core::new().unwrap();
200249
let url = "/foo/bar?baz".parse().unwrap();
201250
let connector = HttpConnector::new(1, &core.handle());
202251

203252
assert_eq!(core.run(connector.connect(url)).unwrap_err().kind(), io::ErrorKind::InvalidInput);
204253
}
205254

255+
#[test]
256+
fn test_errors_enforce_http() {
257+
let mut core = Core::new().unwrap();
258+
let url = "https://example.domain/foo/bar?baz".parse().unwrap();
259+
let connector = HttpConnector::new(1, &core.handle());
260+
261+
assert_eq!(core.run(connector.connect(url)).unwrap_err().kind(), io::ErrorKind::InvalidInput);
262+
}
263+
264+
265+
#[test]
266+
fn test_errors_missing_scheme() {
267+
let mut core = Core::new().unwrap();
268+
let url = "example.domain".parse().unwrap();
269+
let connector = HttpConnector::new(1, &core.handle());
270+
271+
assert_eq!(core.run(connector.connect(url)).unwrap_err().kind(), io::ErrorKind::InvalidInput);
272+
}
206273
}

0 commit comments

Comments
 (0)