2017-12-06 02:09:15 +01:00
|
|
|
use http::header::{self, HeaderName};
|
2018-06-25 06:58:04 +02:00
|
|
|
use server::Request;
|
2017-12-06 02:09:15 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
const X_FORWARDED_FOR: &[u8] = b"x-forwarded-for";
|
|
|
|
const X_FORWARDED_HOST: &[u8] = b"x-forwarded-host";
|
|
|
|
const X_FORWARDED_PROTO: &[u8] = b"x-forwarded-proto";
|
2017-12-06 02:09:15 +01:00
|
|
|
|
|
|
|
/// `HttpRequest` connection information
|
2018-06-25 06:58:04 +02:00
|
|
|
#[derive(Clone, Default)]
|
2018-06-17 20:01:41 +02:00
|
|
|
pub struct ConnectionInfo {
|
|
|
|
scheme: String,
|
|
|
|
host: String,
|
|
|
|
remote: Option<String>,
|
2017-12-06 06:38:52 +01:00
|
|
|
peer: Option<String>,
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 20:01:41 +02:00
|
|
|
impl ConnectionInfo {
|
2017-12-06 02:09:15 +01:00
|
|
|
/// Create *ConnectionInfo* instance for a request.
|
2018-10-02 06:16:56 +02:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "cargo-clippy",
|
2018-11-01 09:14:48 +01:00
|
|
|
allow(cyclomatic_complexity)
|
2018-10-02 06:16:56 +02:00
|
|
|
)]
|
2018-06-25 06:58:04 +02:00
|
|
|
pub fn update(&mut self, req: &Request) {
|
2017-12-06 02:09:15 +01:00
|
|
|
let mut host = None;
|
|
|
|
let mut scheme = None;
|
2017-12-06 06:38:52 +01:00
|
|
|
let mut remote = None;
|
|
|
|
let mut peer = None;
|
2017-12-06 02:09:15 +01:00
|
|
|
|
|
|
|
// load forwarded header
|
|
|
|
for hdr in req.headers().get_all(header::FORWARDED) {
|
|
|
|
if let Ok(val) = hdr.to_str() {
|
|
|
|
for pair in val.split(';') {
|
|
|
|
for el in pair.split(',') {
|
2017-12-06 06:38:52 +01:00
|
|
|
let mut items = el.trim().splitn(2, '=');
|
2017-12-06 02:09:15 +01:00
|
|
|
if let Some(name) = items.next() {
|
|
|
|
if let Some(val) = items.next() {
|
|
|
|
match &name.to_lowercase() as &str {
|
2017-12-06 06:38:52 +01:00
|
|
|
"for" => if remote.is_none() {
|
|
|
|
remote = Some(val.trim());
|
|
|
|
},
|
2017-12-06 02:09:15 +01:00
|
|
|
"proto" => if scheme.is_none() {
|
|
|
|
scheme = Some(val.trim());
|
|
|
|
},
|
|
|
|
"host" => if host.is_none() {
|
|
|
|
host = Some(val.trim());
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// scheme
|
|
|
|
if scheme.is_none() {
|
2018-05-17 21:20:20 +02:00
|
|
|
if let Some(h) = req
|
|
|
|
.headers()
|
2018-06-25 06:58:04 +02:00
|
|
|
.get(HeaderName::from_lowercase(X_FORWARDED_PROTO).unwrap())
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
2017-12-06 02:09:15 +01:00
|
|
|
if let Ok(h) = h.to_str() {
|
|
|
|
scheme = h.split(',').next().map(|v| v.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if scheme.is_none() {
|
2017-12-06 06:53:00 +01:00
|
|
|
scheme = req.uri().scheme_part().map(|a| a.as_str());
|
2018-06-25 06:58:04 +02:00
|
|
|
if scheme.is_none() && req.server_settings().secure() {
|
|
|
|
scheme = Some("https")
|
2017-12-08 18:48:53 +01:00
|
|
|
}
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// host
|
|
|
|
if host.is_none() {
|
2018-05-17 21:20:20 +02:00
|
|
|
if let Some(h) = req
|
|
|
|
.headers()
|
2018-06-25 06:58:04 +02:00
|
|
|
.get(HeaderName::from_lowercase(X_FORWARDED_HOST).unwrap())
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
2017-12-06 02:09:15 +01:00
|
|
|
if let Ok(h) = h.to_str() {
|
|
|
|
host = h.split(',').next().map(|v| v.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if host.is_none() {
|
|
|
|
if let Some(h) = req.headers().get(header::HOST) {
|
2017-12-06 06:53:00 +01:00
|
|
|
host = h.to_str().ok();
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
if host.is_none() {
|
2017-12-08 18:48:53 +01:00
|
|
|
host = req.uri().authority_part().map(|a| a.as_str());
|
|
|
|
if host.is_none() {
|
2018-06-25 06:58:04 +02:00
|
|
|
host = Some(req.server_settings().host());
|
2017-12-08 18:48:53 +01:00
|
|
|
}
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 06:38:52 +01:00
|
|
|
// remote addr
|
|
|
|
if remote.is_none() {
|
2018-05-17 21:20:20 +02:00
|
|
|
if let Some(h) = req
|
|
|
|
.headers()
|
2018-06-25 06:58:04 +02:00
|
|
|
.get(HeaderName::from_lowercase(X_FORWARDED_FOR).unwrap())
|
2018-04-14 01:02:01 +02:00
|
|
|
{
|
2017-12-06 06:38:52 +01:00
|
|
|
if let Ok(h) = h.to_str() {
|
|
|
|
remote = h.split(',').next().map(|v| v.trim());
|
|
|
|
}
|
|
|
|
}
|
2018-04-14 01:02:01 +02:00
|
|
|
if remote.is_none() {
|
|
|
|
// get peeraddr from socketaddr
|
2017-12-06 06:53:00 +01:00
|
|
|
peer = req.peer_addr().map(|addr| format!("{}", addr));
|
2017-12-06 06:38:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
self.scheme = scheme.unwrap_or("http").to_owned();
|
|
|
|
self.host = host.unwrap_or("localhost").to_owned();
|
|
|
|
self.remote = remote.map(|s| s.to_owned());
|
|
|
|
self.peer = peer;
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Scheme of the request.
|
|
|
|
///
|
|
|
|
/// Scheme is resolved through the following headers, in this order:
|
|
|
|
///
|
|
|
|
/// - Forwarded
|
|
|
|
/// - X-Forwarded-Proto
|
|
|
|
/// - Uri
|
|
|
|
#[inline]
|
|
|
|
pub fn scheme(&self) -> &str {
|
2018-06-17 20:01:41 +02:00
|
|
|
&self.scheme
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Hostname of the request.
|
|
|
|
///
|
|
|
|
/// Hostname is resolved through the following headers, in this order:
|
|
|
|
///
|
|
|
|
/// - Forwarded
|
|
|
|
/// - X-Forwarded-Host
|
|
|
|
/// - Host
|
|
|
|
/// - Uri
|
2017-12-26 23:36:03 +01:00
|
|
|
/// - Server hostname
|
2017-12-06 02:09:15 +01:00
|
|
|
pub fn host(&self) -> &str {
|
2018-06-17 20:01:41 +02:00
|
|
|
&self.host
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remote IP of client initiated HTTP request.
|
|
|
|
///
|
|
|
|
/// The IP is resolved through the following headers, in this order:
|
|
|
|
///
|
|
|
|
/// - Forwarded
|
|
|
|
/// - X-Forwarded-For
|
2018-03-24 07:35:52 +01:00
|
|
|
/// - peer name of opened socket
|
2017-12-06 02:09:15 +01:00
|
|
|
#[inline]
|
2017-12-06 06:38:52 +01:00
|
|
|
pub fn remote(&self) -> Option<&str> {
|
2018-06-17 20:01:41 +02:00
|
|
|
if let Some(ref r) = self.remote {
|
2017-12-06 06:38:52 +01:00
|
|
|
Some(r)
|
|
|
|
} else if let Some(ref peer) = self.peer {
|
|
|
|
Some(peer)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
2017-12-06 06:38:52 +01:00
|
|
|
}
|
2017-12-06 02:09:15 +01:00
|
|
|
|
2017-12-06 06:38:52 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-06-25 06:58:04 +02:00
|
|
|
use test::TestRequest;
|
2017-12-06 02:09:15 +01:00
|
|
|
|
2017-12-06 06:38:52 +01:00
|
|
|
#[test]
|
|
|
|
fn test_forwarded() {
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default().request();
|
|
|
|
let mut info = ConnectionInfo::default();
|
|
|
|
info.update(&req);
|
2017-12-06 06:38:52 +01:00
|
|
|
assert_eq!(info.scheme(), "http");
|
2018-06-25 06:58:04 +02:00
|
|
|
assert_eq!(info.host(), "localhost:8080");
|
2017-12-06 06:38:52 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(
|
|
|
|
header::FORWARDED,
|
2018-04-14 01:02:01 +02:00
|
|
|
"for=192.0.2.60; proto=https; by=203.0.113.43; host=rust-lang.org",
|
2018-08-23 18:48:01 +02:00
|
|
|
).request();
|
2017-12-06 06:38:52 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let mut info = ConnectionInfo::default();
|
|
|
|
info.update(&req);
|
2017-12-06 06:38:52 +01:00
|
|
|
assert_eq!(info.scheme(), "https");
|
|
|
|
assert_eq!(info.host(), "rust-lang.org");
|
|
|
|
assert_eq!(info.remote(), Some("192.0.2.60"));
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(header::HOST, "rust-lang.org")
|
|
|
|
.request();
|
2017-12-06 06:38:52 +01:00
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let mut info = ConnectionInfo::default();
|
|
|
|
info.update(&req);
|
2017-12-06 06:38:52 +01:00
|
|
|
assert_eq!(info.scheme(), "http");
|
|
|
|
assert_eq!(info.host(), "rust-lang.org");
|
|
|
|
assert_eq!(info.remote(), None);
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(X_FORWARDED_FOR, "192.0.2.60")
|
|
|
|
.request();
|
|
|
|
let mut info = ConnectionInfo::default();
|
|
|
|
info.update(&req);
|
2017-12-06 06:38:52 +01:00
|
|
|
assert_eq!(info.remote(), Some("192.0.2.60"));
|
|
|
|
|
2018-06-25 06:58:04 +02:00
|
|
|
let req = TestRequest::default()
|
|
|
|
.header(X_FORWARDED_HOST, "192.0.2.60")
|
|
|
|
.request();
|
|
|
|
let mut info = ConnectionInfo::default();
|
|
|
|
info.update(&req);
|
2017-12-06 06:38:52 +01:00
|
|
|
assert_eq!(info.host(), "192.0.2.60");
|
|
|
|
assert_eq!(info.remote(), None);
|
|
|
|
|
2018-07-04 17:01:27 +02:00
|
|
|
let req = TestRequest::default()
|
2018-06-25 06:58:04 +02:00
|
|
|
.header(X_FORWARDED_PROTO, "https")
|
|
|
|
.request();
|
|
|
|
let mut info = ConnectionInfo::default();
|
|
|
|
info.update(&req);
|
2017-12-06 06:38:52 +01:00
|
|
|
assert_eq!(info.scheme(), "https");
|
2017-12-06 02:09:15 +01:00
|
|
|
}
|
|
|
|
}
|