use std::fmt;
use http::{Method, Request, Version};
pub(crate) fn method_name(method: &Method) -> (&'static str, Option<&str>) {
let known = match *method {
Method::GET => "GET",
Method::HEAD => "HEAD",
Method::POST => "POST",
Method::PUT => "PUT",
Method::DELETE => "DELETE",
Method::CONNECT => "CONNECT",
Method::OPTIONS => "OPTIONS",
Method::TRACE => "TRACE",
Method::PATCH => "PATCH",
_ if method.as_str() == "QUERY" => "QUERY",
_ => return ("_OTHER", Some(method.as_str())),
};
(known, None)
}
pub(crate) fn protocol_version(version: Version) -> &'static str {
match version {
Version::HTTP_09 => "0.9",
Version::HTTP_10 => "1.0",
Version::HTTP_11 => "1.1",
Version::HTTP_2 => "2",
Version::HTTP_3 => "3",
_ => "_OTHER",
}
}
pub(crate) fn server_address<B>(req: &Request<B>) -> (Option<&str>, Option<u16>) {
let authority = req
.uri()
.authority()
.map(|a| a.as_str())
.or_else(|| req.headers().get(http::header::HOST)?.to_str().ok());
let Some(authority) = authority else {
return (None, None);
};
let authority = authority
.rsplit_once('@')
.map_or(authority, |(_, host)| host);
if let Some(v6) = authority.strip_prefix('[') {
let Some((host, rest)) = v6.split_once(']') else {
return (None, None);
};
return (
Some(host),
rest.strip_prefix(':').and_then(|p| p.parse().ok()),
);
}
match authority.rsplit_once(':') {
Some((host, port)) => (Some(host), port.parse().ok()),
None => (Some(authority), None),
}
}
#[derive(Debug)]
pub struct RequestInfo<'a> {
pub method: &'static str,
pub method_original: Option<&'a str>,
pub path: &'a str,
pub scheme: &'a str,
pub route: Option<&'a str>,
pub server_address: Option<&'a str>,
pub server_port: Option<u16>,
pub protocol_version: &'static str,
pub user_agent: Option<&'a str>,
}
impl RequestInfo<'_> {
pub fn span_name(&self) -> SpanName<'_> {
SpanName {
method: if self.method == "_OTHER" {
"HTTP"
} else {
self.method
},
route: self.route,
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct SpanName<'a> {
method: &'a str,
route: Option<&'a str>,
}
impl fmt::Display for SpanName<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.route {
Some(route) => write!(f, "{} {route}", self.method),
None => f.write_str(self.method),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn request(uri: &str, host: Option<&str>) -> Request<()> {
let mut b = Request::builder().uri(uri);
if let Some(host) = host {
b = b.header("host", host);
}
b.body(()).unwrap()
}
#[test]
fn addresses_come_from_the_authority_or_host() {
assert_eq!(
server_address(&request("/x", Some("api.example:8080"))),
(Some("api.example"), Some(8080))
);
assert_eq!(
server_address(&request("/x", Some("api.example"))),
(Some("api.example"), None)
);
assert_eq!(
server_address(&request("http://u:p@[::1]:9/x", None)),
(Some("::1"), Some(9))
);
assert_eq!(server_address(&request("/x", None)), (None, None));
}
#[test]
fn unknown_methods_are_other() {
assert_eq!(method_name(&Method::GET), ("GET", None));
let purge = Method::from_bytes(b"PURGE").unwrap();
assert_eq!(method_name(&purge), ("_OTHER", Some("PURGE")));
let query = Method::from_bytes(b"QUERY").unwrap();
assert_eq!(method_name(&query), ("QUERY", None));
}
}