1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-01-24 07:43:49 +01:00
actix-extras/src/request.rs

219 lines
5.4 KiB
Rust
Raw Normal View History

2018-11-16 19:28:07 -08:00
use std::cell::{Ref, RefMut};
2018-10-01 19:18:24 -07:00
use std::fmt;
2018-07-04 22:52:49 +06:00
use std::rc::Rc;
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;
use crate::message::{Message, MessagePool, 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: Option<P>,
2018-11-16 19:28:07 -08:00
pub(crate) inner: Rc<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 {
2018-11-15 22:34:29 -08:00
&self.inner.head.headers
2018-06-25 10:58:04 +06:00
}
#[inline]
fn payload(&mut self) -> Option<P> {
self.payload.take()
2019-02-06 11:44:15 -08:00
}
}
impl Request<Payload> {
/// Create new Request instance
pub fn new() -> Request<Payload> {
Request {
2019-02-07 13:39:15 -08:00
payload: None,
2019-02-06 11:44:15 -08:00
inner: MessagePool::get_message(),
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 {
2019-02-06 11:44:15 -08:00
payload: Some(payload),
2018-11-16 19:28:07 -08:00
inner: MessagePool::get_message(),
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 {
2019-02-07 13:39:15 -08:00
payload: Some(payload.into()),
2019-02-06 11:44:15 -08:00
inner: self.inner.clone(),
}
}
/// Take request's payload
pub fn take_payload(mut self) -> (Option<Payload>, Request<()>) {
2019-02-06 11:44:15 -08:00
(
self.payload.take(),
2019-02-06 11:44:15 -08:00
Request {
payload: Some(()),
inner: self.inner.clone(),
},
)
}
2018-11-16 19:28:07 -08:00
// /// Create new Request instance with pool
// pub(crate) fn with_pool(pool: &'static MessagePool) -> Request {
// Request {
// inner: Rc::new(Message {
// pool,
// url: Url::default(),
// head: RequestHead::default(),
// status: StatusCode::OK,
// flags: Cell::new(MessageFlags::empty()),
// payload: RefCell::new(None),
// extensions: RefCell::new(Extensions::new()),
// }),
// }
// }
2018-07-04 22:52:49 +06:00
#[inline]
2018-10-23 21:44:20 -07:00
#[doc(hidden)]
2018-11-16 19:28:07 -08:00
pub fn inner(&self) -> &Message<RequestHead> {
2018-07-04 22:52:49 +06:00
self.inner.as_ref()
}
#[inline]
2018-10-23 21:44:20 -07:00
#[doc(hidden)]
2018-11-16 19:28:07 -08:00
pub fn inner_mut(&mut self) -> &mut Message<RequestHead> {
2018-07-04 22:52:49 +06:00
Rc::get_mut(&mut self.inner).expect("Multiple copies exist")
}
2018-11-19 14:57:12 -08:00
#[inline]
/// Http message part of the request
pub fn head(&self) -> &RequestHead {
&self.inner.as_ref().head
}
#[inline]
#[doc(hidden)]
/// Mutable reference to a http message part of the request
pub fn head_mut(&mut self) -> &mut RequestHead {
&mut self.inner_mut().head
}
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 {
&self.inner().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 {
&mut self.inner_mut().head.uri
2018-06-25 10:58:04 +06:00
}
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method {
2018-11-15 22:34:29 -08:00
&self.inner().head.method
2018-06-25 10:58:04 +06:00
}
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version {
2018-11-15 22:34:29 -08:00
self.inner().head.version
2018-06-25 10:58:04 +06:00
}
/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
2018-11-18 20:08:43 -08:00
self.inner().head.uri.path()
2018-06-25 10:58:04 +06:00
}
#[inline]
/// Returns Request's headers.
pub fn headers(&self) -> &HeaderMap {
2018-11-15 22:34:29 -08:00
&self.inner().head.headers
2018-06-25 10:58:04 +06:00
}
#[inline]
/// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
2018-11-15 22:34:29 -08:00
&mut self.inner_mut().head.headers
2018-06-25 10:58:04 +06:00
}
/// Request extensions
#[inline]
pub fn extensions(&self) -> Ref<Extensions> {
2018-07-04 22:52:49 +06:00
self.inner().extensions.borrow()
2018-06-25 10:58:04 +06:00
}
/// Mutable reference to a the request's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<Extensions> {
2018-07-04 22:52:49 +06:00
self.inner().extensions.borrow_mut()
2018-06-25 10:58:04 +06:00
}
/// Check if request requires connection upgrade
pub fn upgrade(&self) -> bool {
2018-11-15 22:34:29 -08:00
if let Some(conn) = self.inner().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");
}
}
2018-11-15 22:34:29 -08:00
self.inner().head.method == Method::CONNECT
2018-06-25 10:58:04 +06:00
}
2019-02-06 11:44:15 -08:00
// #[doc(hidden)]
// /// Note: this method should be called only as part of clone operation
// /// of wrapper type.
// pub fn clone_request(&self) -> Self {
// Request {
// inner: self.inner.clone(),
// }
// }
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> Drop for Request<Payload> {
2018-10-04 21:14:18 -07:00
fn drop(&mut self) {
if Rc::strong_count(&self.inner) == 1 {
self.inner.pool.release(self.inner.clone());
2018-07-04 22:52:49 +06:00
}
2018-06-25 10:58:04 +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(())
}
}