1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-25 17:02:44 +01:00
actix-web/src/client/connect.rs

78 lines
1.9 KiB
Rust
Raw Normal View History

2019-03-13 23:57:33 +01:00
use actix_connect::Address;
2018-11-12 08:12:54 +01:00
use http::uri::Uri;
2019-03-13 22:41:40 +01:00
use http::HttpTryFrom;
2018-11-12 08:12:54 +01:00
2019-03-13 22:41:40 +01:00
use super::error::InvalidUrl;
2018-11-12 08:12:54 +01:00
use super::pool::Key;
#[derive(Debug)]
/// `Connect` type represents a message that can be sent to
/// `Connector` with a connection request.
pub struct Connect {
pub(crate) uri: Uri,
}
impl Connect {
2018-11-14 07:53:30 +01:00
/// Create `Connect` message for specified `Uri`
pub fn new(uri: Uri) -> Connect {
Connect { uri }
}
2018-11-12 08:12:54 +01:00
/// Construct `Uri` instance and create `Connect` message.
2019-03-13 22:41:40 +01:00
pub fn try_from<U>(uri: U) -> Result<Connect, InvalidUrl>
2018-11-12 08:12:54 +01:00
where
Uri: HttpTryFrom<U>,
{
Ok(Connect {
uri: Uri::try_from(uri).map_err(|e| e.into())?,
})
}
pub(crate) fn is_secure(&self) -> bool {
if let Some(scheme) = self.uri.scheme_part() {
scheme.as_str() == "https"
} else {
false
}
}
pub(crate) fn key(&self) -> Key {
self.uri.authority_part().unwrap().clone().into()
}
2019-03-13 22:41:40 +01:00
pub(crate) fn validate(&self) -> Result<(), InvalidUrl> {
2018-11-12 08:12:54 +01:00
if self.uri.host().is_none() {
2019-03-13 22:41:40 +01:00
Err(InvalidUrl::MissingHost)
2018-11-12 08:12:54 +01:00
} else if self.uri.scheme_part().is_none() {
2019-03-13 22:41:40 +01:00
Err(InvalidUrl::MissingScheme)
2018-11-12 08:12:54 +01:00
} else if let Some(scheme) = self.uri.scheme_part() {
match scheme.as_str() {
"http" | "ws" | "https" | "wss" => Ok(()),
2019-03-13 22:41:40 +01:00
_ => Err(InvalidUrl::UnknownScheme),
2018-11-12 08:12:54 +01:00
}
} else {
Ok(())
}
}
2019-03-13 23:57:33 +01:00
}
2018-11-12 08:12:54 +01:00
2019-03-13 23:57:33 +01:00
impl Address for Connect {
fn host(&self) -> &str {
2018-11-12 08:12:54 +01:00
&self.uri.host().unwrap()
}
2019-03-13 23:57:33 +01:00
fn port(&self) -> u16 {
2018-11-12 08:12:54 +01:00
if let Some(port) = self.uri.port() {
port
} else if let Some(scheme) = self.uri.scheme_part() {
match scheme.as_str() {
"http" | "ws" => 80,
"https" | "wss" => 443,
_ => 80,
}
} else {
80
}
}
}