use std::cell::{Ref, RefMut}; use std::{fmt, net}; use http::{header, Method, Uri, Version}; use crate::extensions::Extensions; use crate::header::HeaderMap; use crate::httpmessage::HttpMessage; use crate::message::{Message, RequestHead}; use crate::payload::{Payload, PayloadStream}; /// Request pub struct Request
{ pub(crate) payload: Payload
,
pub(crate) head: Message HttpMessage for Request {
type Stream = P;
#[inline]
fn headers(&self) -> &HeaderMap {
&self.head().headers
}
/// Request extensions
#[inline]
fn extensions(&self) -> Ref {
std::mem::replace(&mut self.payload, Payload::None)
}
}
impl From Request {
/// Create new Request instance
pub fn with_payload(payload: Payload ) -> Request {
Request {
payload,
head: Message::new(),
}
}
/// Create new Request instance
pub fn replace_payload ) {
let pl = self.payload;
(
Request {
payload,
head: self.head,
},
pl,
)
}
/// Get request's payload
pub fn take_payload(&mut self) -> Payload {
std::mem::replace(&mut self.payload, Payload::None)
}
/// Split request into request head and payload
pub fn into_parts(self) -> (Message ) {
(self.head, self.payload)
}
#[inline]
/// Http message part of the request
pub fn head(&self) -> &RequestHead {
&*self.head
}
#[inline]
#[doc(hidden)]
/// Mutable reference to a http message part of the request
pub fn head_mut(&mut self) -> &mut RequestHead {
&mut *self.head
}
/// Mutable reference to the message's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
&mut self.head_mut().headers
}
/// Request's uri.
#[inline]
pub fn uri(&self) -> &Uri {
&self.head().uri
}
/// Mutable reference to the request's uri.
#[inline]
pub fn uri_mut(&mut self) -> &mut Uri {
&mut self.head_mut().uri
}
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method {
&self.head().method
}
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version {
self.head().version
}
/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
self.head().uri.path()
}
/// Check if request requires connection upgrade
#[inline]
pub fn upgrade(&self) -> bool {
if let Some(conn) = self.head().headers.get(header::CONNECTION) {
if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade");
}
}
self.head().method == Method::CONNECT
}
/// Peer socket address
///
/// Peer address is actual socket address, if proxy is used in front of
/// actix http server, then peer address would be address of this proxy.
#[inline]
pub fn peer_addr(&self) -> Option fmt::Debug for Request {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(
f,
"\nRequest {:?} {}:{}",
self.version(),
self.method(),
self.path()
)?;
if let Some(q) = self.uri().query().as_ref() {
writeln!(f, " query: ?{:?}", q)?;
}
writeln!(f, " headers:")?;
for (key, val) in self.headers() {
writeln!(f, " {:?}: {:?}", key, val)?;
}
Ok(())
}
}