Remove DEFAULT_HOST (#153)
In a few places we relied on "localhost" as a default if a URL's host was not set, but I think it's better to error out in these cases. In general, there are a few places in Unit that assumed there is a host as part of the URL. I've made that explicit by doing a check at the beginning of `connect()`. I've also tried to plumb through the semantics of "host is always present" by changing the parameter types of some of the functions that use the hostname. I considered a more thorough way to express this with types - for instance implementing an `HttpUrl` struct that embeds a `Url`, and exports most of the same methods, but guarantees that host is always present. However, that was more invasive than this so I did a smaller change to start.
This commit is contained in:
committed by
GitHub
parent
fec79dcef3
commit
e8c3403f7b
@@ -12,7 +12,6 @@ use crate::body::BodySize;
|
||||
use crate::body::{Payload, SizedReader};
|
||||
use crate::error::Error;
|
||||
use crate::header::{self, Header};
|
||||
use crate::pool;
|
||||
use crate::unit::{self, Unit};
|
||||
use crate::Response;
|
||||
|
||||
@@ -500,8 +499,13 @@ impl Request {
|
||||
/// assert_eq!(req2.get_host().unwrap(), "localhost");
|
||||
/// ```
|
||||
pub fn get_host(&self) -> Result<String, Error> {
|
||||
self.to_url()
|
||||
.map(|u| u.host_str().unwrap_or(pool::DEFAULT_HOST).to_string())
|
||||
match self.to_url() {
|
||||
Ok(u) => match u.host_str() {
|
||||
Some(host) => Ok(host.to_string()),
|
||||
None => Err(Error::BadUrl("No hostname in URL".into())),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the scheme for this request.
|
||||
@@ -637,3 +641,13 @@ impl fmt::Debug for TLSConnector {
|
||||
f.debug_struct("TLSConnector").finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_hostname() {
|
||||
let req = Request::new(
|
||||
&Agent::default(),
|
||||
"GET".to_string(),
|
||||
"unix:/run/foo.socket".to_string(),
|
||||
);
|
||||
assert!(req.get_host().is_err());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user