pub struct RequestHead<'a> { /* private fields */ }Expand description
A parsed request head, borrowed from the connection’s buffer.
Every accessor is a slice of the bytes that arrived; nothing is copied.
The view stays valid until the next call that mutates the connection.
Read it again later with ServerConn::request.
§Examples
use http_net::{Method, ServerConn, ServerEvent, Version};
let mut conn = ServerConn::new();
let _ = conn.feed(b"GET /search?q=rust HTTP/1.1\r\nHost: example.com\r\n\r\n");
let ServerEvent::Request(req) = conn.poll().unwrap() else { panic!() };
assert_eq!(req.method(), Method::Get);
assert_eq!(req.path(), "/search");
assert_eq!(req.query(), Some("q=rust"));
assert_eq!(req.version(), Version::Http11);
assert_eq!(req.host(), Some("example.com"));
assert!(req.keep_alive());Implementations§
Source§impl<'a> RequestHead<'a>
impl<'a> RequestHead<'a>
Sourcepub fn method_str(&self) -> &'a str
pub fn method_str(&self) -> &'a str
The method exactly as received.
§Examples
assert_eq!(req.method(), Method::Other);
assert_eq!(req.method_str(), "PROPFIND");Sourcepub fn target(&self) -> &'a str
pub fn target(&self) -> &'a str
The request target exactly as received (origin form /path?query,
absolute form, authority form for CONNECT, or *).
§Examples
assert_eq!(req.target(), "/a%20b?x=1");Sourcepub fn path(&self) -> &'a str
pub fn path(&self) -> &'a str
The path: the target up to (not including) the first ?, with
percent-encoding left as received.
For an absolute-form target (http://host/p?q) this is the path
after the authority (/p), and / when that path is empty, so a
router sees the same path whichever form the client used. For *
and for CONNECT’s authority form it is the whole target.
§Examples
assert_eq!(req.path(), "/files/a.txt");
assert_eq!(req.path(), "/files/b.txt");Sourcepub fn header(&self, name: &str) -> Option<&'a [u8]>
pub fn header(&self, name: &str) -> Option<&'a [u8]>
The value of the first field named name (ASCII case-insensitive).
§Examples
assert_eq!(req.header("accept"), Some(&b"text/html"[..]));
assert_eq!(req.header("cookie"), None);Sourcepub fn header_str(&self, name: &str) -> Option<&'a str>
pub fn header_str(&self, name: &str) -> Option<&'a str>
Sourcepub fn host(&self) -> Option<&'a str>
pub fn host(&self) -> Option<&'a str>
The authority the request is for (host [":" port]), if known.
This is the Host field, except for an absolute-form target
(GET http://example.com/ HTTP/1.1), whose authority wins: RFC 9112
section 3.2.2 requires a server to ignore Host then, so the two can
never be read differently. The raw field stays available through
header("host"). HTTP/1.1 requests always carry a valid Host;
HTTP/1.0 requests may not, and an empty Host (a target with no
authority) is reported as Some("").
§Examples
assert_eq!(req.host(), Some("127.0.0.1:8080"));
assert_eq!(req.host(), Some("a.test"));
assert_eq!(req.header("host"), Some(&b"b.test"[..]));Sourcepub fn expects_continue(&self) -> bool
pub fn expects_continue(&self) -> bool
Whether the client sent Expect: 100-continue and is waiting for a
100 Continue before sending the body. Only HTTP/1.1 requests that
have a body report it (RFC 9110 section 10.1.1).
Answer it before reading the body: either
ServerConn::send_continue, or a
final response straight away (which closes the connection afterwards,
since the body was not read). The blocking Server sends
100 Continue itself.
§Examples
assert!(req.expects_continue());Sourcepub fn framing(&self) -> BodyFraming
pub fn framing(&self) -> BodyFraming
Sourcepub fn keep_alive(&self) -> bool
pub fn keep_alive(&self) -> bool
Whether the client allows the connection to stay open after this
exchange (HTTP/1.1 unless Connection: close; HTTP/1.0 only with
Connection: keep-alive).
§Examples
assert!(!req.keep_alive());Sourcepub fn is_upgrade(&self) -> bool
pub fn is_upgrade(&self) -> bool
Whether this is an HTTP/1.1 request to switch protocols: Connection
lists upgrade and an Upgrade field names the protocol (read it
with header("upgrade")). Answer with
Response::switching_protocols
to hand the connection over.
§Examples
assert!(req.is_upgrade());
assert_eq!(req.header("upgrade"), Some(&b"websocket"[..]));