pub trait Forward {
fn host(&self) -> Option<&str>;
fn client_ip(&self) -> IpAddr;
fn forwarded_ips(&self) -> Vec<IpAddr>ⓘ;
fn forwarded_proto(&self) -> Option<&str>;
}
Expand description
A context extension Forward
used to parse X-Forwarded-*
request headers.
Required methods
Get true host.
- If “x-forwarded-host” is set and valid, use it.
- Else if “host” is set and valid, use it.
- Else throw Err(400 BAD REQUEST).
Example
use roa::{Context, Result};
use roa::forward::Forward;
async fn get(ctx: &mut Context) -> Result {
if let Some(host) = ctx.host() {
println!("host: {}", host);
}
Ok(())
}
Get true client ip.
- If “x-forwarded-for” is set and valid, use the first ip.
- Else use the ip of
Context::remote_addr()
.
Example
use roa::{Context, Result};
use roa::forward::Forward;
async fn get(ctx: &mut Context) -> Result {
println!("client ip: {}", ctx.client_ip());
Ok(())
}
Get true forwarded ips.
- If “x-forwarded-for” is set and valid, use it.
- Else return an empty vector.
Example
use roa::{Context, Result};
use roa::forward::Forward;
async fn get(ctx: &mut Context) -> Result {
println!("forwarded ips: {:?}", ctx.forwarded_ips());
Ok(())
}
fn forwarded_proto(&self) -> Option<&str>
fn forwarded_proto(&self) -> Option<&str>
Try to get forwarded proto.
- If “x-forwarded-proto” is not set, return None.
- If “x-forwarded-proto” is set but fails to string, return Some(Err(400 BAD REQUEST)).
Example
use roa::{Context, Result};
use roa::forward::Forward;
async fn get(ctx: &mut Context) -> Result {
if let Some(proto) = ctx.forwarded_proto() {
println!("forwarded proto: {}", proto);
}
Ok(())
}