1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-23 23:34:35 +01:00
actix-extras/src/request.rs

177 lines
4.2 KiB
Rust
Raw Normal View History

use std::cell::{Ref, RefCell, RefMut};
2018-10-01 19:18:24 -07:00
use std::fmt;
2018-06-25 10:58:04 +06:00
2019-02-06 11:44:15 -08:00
use bytes::Bytes;
use futures::Stream;
2018-11-16 19:28:07 -08:00
use http::{header, HeaderMap, Method, Uri, Version};
2018-06-25 10:58:04 +06:00
2019-02-06 11:44:15 -08:00
use crate::error::PayloadError;
2018-12-06 14:32:52 -08:00
use crate::extensions::Extensions;
use crate::httpmessage::HttpMessage;
2019-02-07 21:16:46 -08:00
use crate::message::{Message, RequestHead};
2019-02-07 13:39:15 -08:00
use crate::payload::Payload;
2018-06-25 10:58:04 +06:00
/// Request
2019-02-06 11:44:15 -08:00
pub struct Request<P = Payload> {
pub(crate) payload: RefCell<Option<P>>,
2019-02-07 21:16:46 -08:00
pub(crate) inner: Message<RequestHead>,
2018-06-25 10:58:04 +06:00
}
2019-02-06 11:44:15 -08:00
impl<P> HttpMessage for Request<P>
where
P: Stream<Item = Bytes, Error = PayloadError>,
{
type Stream = P;
2018-06-25 10:58:04 +06:00
fn headers(&self) -> &HeaderMap {
2019-02-07 21:16:46 -08:00
&self.head().headers
2018-06-25 10:58:04 +06:00
}
#[inline]
fn payload(&self) -> Option<P> {
self.payload.borrow_mut().take()
2019-02-06 11:44:15 -08:00
}
}
2019-02-07 21:16:46 -08:00
impl<Payload> From<Message<RequestHead>> for Request<Payload> {
fn from(msg: Message<RequestHead>) -> Self {
Request {
payload: RefCell::new(None),
inner: msg,
}
}
}
2019-02-06 11:44:15 -08:00
impl Request<Payload> {
/// Create new Request instance
pub fn new() -> Request<Payload> {
Request {
payload: RefCell::new(None),
2019-02-07 21:16:46 -08:00
inner: Message::new(),
2018-06-25 10:58:04 +06:00
}
}
}
2019-02-06 11:44:15 -08:00
impl<Payload> Request<Payload> {
2018-10-04 20:02:10 -07:00
/// Create new Request instance
2019-02-06 11:44:15 -08:00
pub fn with_payload(payload: Payload) -> Request<Payload> {
2018-06-25 10:58:04 +06:00
Request {
payload: RefCell::new(Some(payload.into())),
2019-02-07 21:16:46 -08:00
inner: Message::new(),
2018-06-25 10:58:04 +06:00
}
}
2019-02-06 11:44:15 -08:00
/// Create new Request instance
2019-02-07 13:39:15 -08:00
pub fn set_payload<I, P>(self, payload: I) -> Request<P>
where
I: Into<P>,
{
2019-02-06 11:44:15 -08:00
Request {
payload: RefCell::new(Some(payload.into())),
2019-02-07 21:16:46 -08:00
inner: self.inner,
2019-02-06 11:44:15 -08:00
}
}
2019-02-07 21:16:46 -08:00
/// Split request into request head and payload
pub fn into_parts(mut self) -> (Message<RequestHead>, Option<Payload>) {
(self.inner, self.payload.get_mut().take())
2018-07-04 22:52:49 +06:00
}
2018-11-19 14:57:12 -08:00
#[inline]
/// Http message part of the request
pub fn head(&self) -> &RequestHead {
2019-02-07 21:16:46 -08:00
&*self.inner
2018-11-19 14:57:12 -08:00
}
#[inline]
#[doc(hidden)]
/// Mutable reference to a http message part of the request
pub fn head_mut(&mut self) -> &mut RequestHead {
2019-02-07 21:16:46 -08:00
&mut *self.inner
2018-11-19 14:57:12 -08:00
}
2018-11-15 22:34:29 -08:00
/// Request's uri.
2018-06-25 10:58:04 +06:00
#[inline]
2018-11-15 22:34:29 -08:00
pub fn uri(&self) -> &Uri {
2019-02-07 21:16:46 -08:00
&self.head().uri
2018-06-25 10:58:04 +06:00
}
2018-11-15 22:34:29 -08:00
/// Mutable reference to the request's uri.
2018-06-25 10:58:04 +06:00
#[inline]
2018-11-15 22:34:29 -08:00
pub fn uri_mut(&mut self) -> &mut Uri {
2019-02-07 21:16:46 -08:00
&mut self.head_mut().uri
2018-06-25 10:58:04 +06:00
}
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method {
2019-02-07 21:16:46 -08:00
&self.head().method
2018-06-25 10:58:04 +06:00
}
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version {
2019-02-07 21:16:46 -08:00
self.head().version
2018-06-25 10:58:04 +06:00
}
/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
2019-02-07 21:16:46 -08:00
self.head().uri.path()
2018-06-25 10:58:04 +06:00
}
#[inline]
/// Returns Request's headers.
pub fn headers(&self) -> &HeaderMap {
2019-02-07 21:16:46 -08:00
&self.head().headers
2018-06-25 10:58:04 +06:00
}
#[inline]
/// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
2019-02-07 21:16:46 -08:00
&mut self.head_mut().headers
2018-06-25 10:58:04 +06:00
}
/// Request extensions
#[inline]
pub fn extensions(&self) -> Ref<Extensions> {
2019-02-07 21:16:46 -08:00
self.inner.extensions()
2018-06-25 10:58:04 +06:00
}
/// Mutable reference to a the request's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<Extensions> {
2019-02-07 21:16:46 -08:00
self.inner.extensions_mut()
2018-06-25 10:58:04 +06:00
}
/// Check if request requires connection upgrade
pub fn upgrade(&self) -> bool {
2019-02-07 21:16:46 -08:00
if let Some(conn) = self.head().headers.get(header::CONNECTION) {
2018-06-25 10:58:04 +06:00
if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade");
}
}
2019-02-07 21:16:46 -08:00
self.head().method == Method::CONNECT
2018-06-25 10:58:04 +06:00
}
2018-10-04 21:14:18 -07:00
}
2018-07-04 22:57:40 +06:00
2019-02-06 11:44:15 -08:00
impl<Payload> fmt::Debug for Request<Payload> {
2018-10-01 19:18:24 -07:00
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().iter() {
writeln!(f, " {:?}: {:?}", key, val)?;
}
Ok(())
}
}