1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 10:02:38 +01:00
actix-web/src/request.rs

181 lines
4.4 KiB
Rust
Raw Normal View History

2018-11-17 04:28:07 +01:00
use std::cell::{Ref, RefMut};
2018-10-02 04:18:24 +02:00
use std::fmt;
2018-07-04 18:52:49 +02:00
use std::rc::Rc;
2018-06-25 06:58:04 +02:00
2018-11-17 04:28:07 +01:00
use http::{header, HeaderMap, Method, Uri, Version};
2018-06-25 06:58:04 +02:00
use extensions::Extensions;
use httpmessage::HttpMessage;
use payload::Payload;
2018-11-19 02:52:56 +01:00
use message::{Head, Message, MessagePool, RequestHead};
2018-06-25 06:58:04 +02:00
/// Request
2018-06-25 06:58:04 +02:00
pub struct Request {
2018-11-17 04:28:07 +01:00
pub(crate) inner: Rc<Message<RequestHead>>,
2018-06-25 06:58:04 +02:00
}
impl HttpMessage for Request {
type Stream = Payload;
fn headers(&self) -> &HeaderMap {
2018-11-16 07:34:29 +01:00
&self.inner.head.headers
2018-06-25 06:58:04 +02:00
}
#[inline]
fn payload(&self) -> Payload {
if let Some(payload) = self.inner.payload.borrow_mut().take() {
payload
} else {
Payload::empty()
}
}
}
impl Request {
2018-10-05 05:02:10 +02:00
/// Create new Request instance
pub fn new() -> Request {
2018-06-25 06:58:04 +02:00
Request {
2018-11-17 04:28:07 +01:00
inner: MessagePool::get_message(),
2018-06-25 06:58:04 +02:00
}
}
2018-11-17 04:28:07 +01: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 18:52:49 +02:00
#[inline]
2018-10-24 06:44:20 +02:00
#[doc(hidden)]
2018-11-17 04:28:07 +01:00
pub fn inner(&self) -> &Message<RequestHead> {
2018-07-04 18:52:49 +02:00
self.inner.as_ref()
}
#[inline]
2018-10-24 06:44:20 +02:00
#[doc(hidden)]
2018-11-17 04:28:07 +01:00
pub fn inner_mut(&mut self) -> &mut Message<RequestHead> {
2018-07-04 18:52:49 +02:00
Rc::get_mut(&mut self.inner).expect("Multiple copies exist")
}
2018-11-16 07:34:29 +01:00
/// Request's uri.
2018-06-25 06:58:04 +02:00
#[inline]
2018-11-16 07:34:29 +01:00
pub fn uri(&self) -> &Uri {
&self.inner().head.uri
2018-06-25 06:58:04 +02:00
}
2018-11-16 07:34:29 +01:00
/// Mutable reference to the request's uri.
2018-06-25 06:58:04 +02:00
#[inline]
2018-11-16 07:34:29 +01:00
pub fn uri_mut(&mut self) -> &mut Uri {
&mut self.inner_mut().head.uri
2018-06-25 06:58:04 +02:00
}
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method {
2018-11-16 07:34:29 +01:00
&self.inner().head.method
2018-06-25 06:58:04 +02:00
}
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version {
2018-11-16 07:34:29 +01:00
self.inner().head.version
2018-06-25 06:58:04 +02:00
}
/// The target path of this Request.
#[inline]
pub fn path(&self) -> &str {
2018-11-17 04:28:07 +01:00
if let Some(path) = self.inner().url.path() {
path
} else {
self.inner().head.uri.path()
}
2018-06-25 06:58:04 +02:00
}
#[inline]
/// Returns Request's headers.
pub fn headers(&self) -> &HeaderMap {
2018-11-16 07:34:29 +01:00
&self.inner().head.headers
2018-06-25 06:58:04 +02:00
}
#[inline]
/// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
2018-11-16 07:34:29 +01:00
&mut self.inner_mut().head.headers
2018-06-25 06:58:04 +02:00
}
/// Checks if a connection should be kept alive.
#[inline]
pub fn keep_alive(&self) -> bool {
2018-11-19 02:52:56 +01:00
self.inner().head.keep_alive()
2018-06-25 06:58:04 +02:00
}
/// Request extensions
#[inline]
pub fn extensions(&self) -> Ref<Extensions> {
2018-07-04 18:52:49 +02:00
self.inner().extensions.borrow()
2018-06-25 06:58:04 +02:00
}
/// Mutable reference to a the request's extensions
#[inline]
pub fn extensions_mut(&self) -> RefMut<Extensions> {
2018-07-04 18:52:49 +02:00
self.inner().extensions.borrow_mut()
2018-06-25 06:58:04 +02:00
}
/// Check if request requires connection upgrade
pub fn upgrade(&self) -> bool {
2018-11-16 07:34:29 +01:00
if let Some(conn) = self.inner().head.headers.get(header::CONNECTION) {
2018-06-25 06:58:04 +02:00
if let Ok(s) = conn.to_str() {
return s.to_lowercase().contains("upgrade");
}
}
2018-11-16 07:34:29 +01:00
self.inner().head.method == Method::CONNECT
2018-06-25 06:58:04 +02:00
}
2018-10-12 05:15:10 +02:00
#[doc(hidden)]
/// Note: this method should be called only as part of clone operation
/// of wrapper type.
pub fn clone_request(&self) -> Self {
2018-07-04 18:57:40 +02:00
Request {
inner: self.inner.clone(),
}
}
2018-10-05 06:14:18 +02:00
}
2018-07-04 18:57:40 +02:00
2018-10-05 06:14:18 +02:00
impl Drop for Request {
fn drop(&mut self) {
if Rc::strong_count(&self.inner) == 1 {
self.inner.pool.release(self.inner.clone());
2018-07-04 18:52:49 +02:00
}
2018-06-25 06:58:04 +02:00
}
}
2018-10-02 04:18:24 +02:00
impl 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().iter() {
writeln!(f, " {:?}: {:?}", key, val)?;
}
Ok(())
}
}