2021-01-15 03:11:10 +01:00
|
|
|
//! HTTP requests.
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
cell::{Ref, RefMut},
|
|
|
|
fmt, net,
|
|
|
|
};
|
2019-03-26 19:54:35 +01:00
|
|
|
|
2019-04-07 00:02:02 +02:00
|
|
|
use http::{header, Method, Uri, Version};
|
2019-03-26 19:54:35 +01:00
|
|
|
|
|
|
|
use crate::extensions::Extensions;
|
2019-04-07 00:02:02 +02:00
|
|
|
use crate::header::HeaderMap;
|
2021-02-11 23:58:35 +01:00
|
|
|
use crate::HttpMessage;
|
2019-03-26 19:54:35 +01:00
|
|
|
use crate::message::{Message, RequestHead};
|
|
|
|
use crate::payload::{Payload, PayloadStream};
|
|
|
|
|
|
|
|
/// Request
|
|
|
|
pub struct Request<P = PayloadStream> {
|
|
|
|
pub(crate) payload: Payload<P>,
|
|
|
|
pub(crate) head: Message<RequestHead>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> HttpMessage for Request<P> {
|
|
|
|
type Stream = P;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn headers(&self) -> &HeaderMap {
|
|
|
|
&self.head().headers
|
|
|
|
}
|
|
|
|
|
2021-01-04 00:47:04 +01:00
|
|
|
fn take_payload(&mut self) -> Payload<P> {
|
|
|
|
std::mem::replace(&mut self.payload, Payload::None)
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// Request extensions
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-26 19:54:35 +01:00
|
|
|
self.head.extensions()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the request's extensions
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
2019-03-26 19:54:35 +01:00
|
|
|
self.head.extensions_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Message<RequestHead>> for Request<PayloadStream> {
|
|
|
|
fn from(head: Message<RequestHead>) -> Self {
|
|
|
|
Request {
|
|
|
|
head,
|
|
|
|
payload: Payload::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Request<PayloadStream> {
|
|
|
|
/// Create new Request instance
|
|
|
|
pub fn new() -> Request<PayloadStream> {
|
|
|
|
Request {
|
|
|
|
head: Message::new(),
|
|
|
|
payload: Payload::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> Request<P> {
|
|
|
|
/// Create new Request instance
|
|
|
|
pub fn with_payload(payload: Payload<P>) -> Request<P> {
|
|
|
|
Request {
|
|
|
|
payload,
|
|
|
|
head: Message::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create new Request instance
|
|
|
|
pub fn replace_payload<P1>(self, payload: Payload<P1>) -> (Request<P1>, Payload<P>) {
|
|
|
|
let pl = self.payload;
|
|
|
|
(
|
|
|
|
Request {
|
|
|
|
payload,
|
|
|
|
head: self.head,
|
|
|
|
},
|
|
|
|
pl,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-19 13:54:19 +01:00
|
|
|
/// Get request's payload
|
|
|
|
pub fn payload(&mut self) -> &mut Payload<P> {
|
|
|
|
&mut self.payload
|
|
|
|
}
|
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
/// Get request's payload
|
|
|
|
pub fn take_payload(&mut self) -> Payload<P> {
|
|
|
|
std::mem::replace(&mut self.payload, Payload::None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Split request into request head and payload
|
|
|
|
pub fn into_parts(self) -> (Message<RequestHead>, Payload<P>) {
|
|
|
|
(self.head, self.payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Http message part of the request
|
|
|
|
pub fn head(&self) -> &RequestHead {
|
|
|
|
&*self.head
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
#[doc(hidden)]
|
2021-02-11 23:39:54 +01:00
|
|
|
/// Mutable reference to a HTTP message part of the request
|
2019-03-26 19:54:35 +01:00
|
|
|
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
|
2019-04-16 18:54:02 +02:00
|
|
|
#[inline]
|
2019-03-26 19:54:35 +01:00
|
|
|
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
|
|
|
|
}
|
2019-04-16 18:54:02 +02:00
|
|
|
|
2021-02-11 23:39:54 +01:00
|
|
|
/// Peer socket address.
|
2019-04-16 18:54:02 +02:00
|
|
|
///
|
2021-02-11 23:39:54 +01:00
|
|
|
/// Peer address is the directly connected peer's socket address. If a proxy is used in front of
|
|
|
|
/// the Actix Web server, then it would be address of this proxy.
|
|
|
|
///
|
|
|
|
/// Will only return None when called in unit tests.
|
2019-04-16 18:54:02 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn peer_addr(&self) -> Option<net::SocketAddr> {
|
|
|
|
self.head().peer_addr
|
|
|
|
}
|
2019-03-26 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<P> fmt::Debug for Request<P> {
|
2019-12-07 19:46:51 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-03-26 19:54:35 +01:00
|
|
|
writeln!(
|
|
|
|
f,
|
|
|
|
"\nRequest {:?} {}:{}",
|
|
|
|
self.version(),
|
|
|
|
self.method(),
|
|
|
|
self.path()
|
|
|
|
)?;
|
2021-02-09 23:59:17 +01:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
if let Some(q) = self.uri().query().as_ref() {
|
|
|
|
writeln!(f, " query: ?{:?}", q)?;
|
|
|
|
}
|
2021-02-09 23:59:17 +01:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
writeln!(f, " headers:")?;
|
2021-02-09 23:59:17 +01:00
|
|
|
|
|
|
|
for (key, val) in self.headers().iter() {
|
2019-03-26 19:54:35 +01:00
|
|
|
writeln!(f, " {:?}: {:?}", key, val)?;
|
|
|
|
}
|
2021-02-09 23:59:17 +01:00
|
|
|
|
2019-03-26 19:54:35 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-04-18 02:48:25 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-12-05 18:35:43 +01:00
|
|
|
use std::convert::TryFrom;
|
2019-04-18 02:48:25 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_basics() {
|
|
|
|
let msg = Message::new();
|
|
|
|
let mut req = Request::from(msg);
|
|
|
|
req.headers_mut().insert(
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
header::HeaderValue::from_static("text/plain"),
|
|
|
|
);
|
|
|
|
assert!(req.headers().contains_key(header::CONTENT_TYPE));
|
|
|
|
|
|
|
|
*req.uri_mut() = Uri::try_from("/index.html?q=1").unwrap();
|
|
|
|
assert_eq!(req.uri().path(), "/index.html");
|
|
|
|
assert_eq!(req.uri().query(), Some("q=1"));
|
|
|
|
|
|
|
|
let s = format!("{:?}", req);
|
|
|
|
assert!(s.contains("Request HTTP/1.1 GET:/index.html"));
|
|
|
|
}
|
|
|
|
}
|