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

275 lines
6.7 KiB
Rust
Raw Normal View History

2018-06-25 06:58:04 +02:00
use std::cell::{Cell, Ref, RefCell, RefMut};
use std::collections::VecDeque;
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
use http::{header, HeaderMap, Method, StatusCode, Uri, Version};
2018-06-25 06:58:04 +02:00
use client::ClientResponse;
2018-06-25 06:58:04 +02:00
use extensions::Extensions;
use httpmessage::HttpMessage;
use payload::Payload;
2018-10-12 05:15:10 +02:00
use uri::Url;
2018-06-25 06:58:04 +02:00
bitflags! {
pub(crate) struct MessageFlags: u8 {
const KEEPALIVE = 0b0000_0001;
const CONN_INFO = 0b0000_0010;
}
}
/// Request
2018-06-25 06:58:04 +02:00
pub struct Request {
pub(crate) inner: Rc<Message>,
2018-06-25 06:58:04 +02:00
}
2018-11-16 07:34:29 +01:00
pub struct RequestHead {
pub uri: Uri,
2018-10-24 06:44:20 +02:00
pub method: Method,
2018-11-16 07:34:29 +01:00
pub version: Version,
2018-10-24 06:44:20 +02:00
pub headers: HeaderMap,
2018-11-16 07:34:29 +01:00
}
impl Default for RequestHead {
fn default() -> RequestHead {
RequestHead {
uri: Uri::default(),
method: Method::default(),
version: Version::HTTP_11,
headers: HeaderMap::with_capacity(16),
}
}
}
pub struct Message {
pub head: RequestHead,
pub url: Url,
pub status: StatusCode,
2018-10-24 06:44:20 +02:00
pub extensions: RefCell<Extensions>,
pub payload: RefCell<Option<Payload>>,
pub(crate) pool: &'static MessagePool,
2018-10-24 06:44:20 +02:00
pub(crate) flags: Cell<MessageFlags>,
2018-07-04 18:52:49 +02:00
}
impl Message {
2018-07-04 18:52:49 +02:00
#[inline]
/// Reset request instance
pub fn reset(&mut self) {
2018-11-16 07:34:29 +01:00
self.head.clear();
2018-07-04 18:52:49 +02:00
self.extensions.borrow_mut().clear();
self.flags.set(MessageFlags::empty());
*self.payload.borrow_mut() = None;
}
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 {
Request::with_pool(MessagePool::pool())
2018-10-05 05:02:10 +02:00
}
/// Create new Request instance with pool
pub(crate) fn with_pool(pool: &'static MessagePool) -> Request {
2018-06-25 06:58:04 +02:00
Request {
inner: Rc::new(Message {
2018-07-04 18:52:49 +02:00
pool,
2018-10-12 05:15:10 +02:00
url: Url::default(),
2018-11-16 07:34:29 +01:00
head: RequestHead::default(),
status: StatusCode::OK,
2018-06-25 06:58:04 +02:00
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)]
pub fn inner(&self) -> &Message {
2018-07-04 18:52:49 +02:00
self.inner.as_ref()
}
#[inline]
2018-10-24 06:44:20 +02:00
#[doc(hidden)]
pub fn inner_mut(&mut self) -> &mut Message {
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-07-04 18:52:49 +02:00
self.inner().url.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-07-04 18:52:49 +02:00
self.inner().flags.get().contains(MessageFlags::KEEPALIVE)
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(())
}
}
2018-10-05 05:02:10 +02:00
/// Request's objects pool
pub(crate) struct MessagePool(RefCell<VecDeque<Rc<Message>>>);
2018-06-25 06:58:04 +02:00
thread_local!(static POOL: &'static MessagePool = MessagePool::create());
2018-06-25 06:58:04 +02:00
impl MessagePool {
fn create() -> &'static MessagePool {
let pool = MessagePool(RefCell::new(VecDeque::with_capacity(128)));
2018-06-25 06:58:04 +02:00
Box::leak(Box::new(pool))
}
2018-10-05 05:02:10 +02:00
/// Get default request's pool
pub fn pool() -> &'static MessagePool {
2018-10-05 02:34:57 +02:00
POOL.with(|p| *p)
2018-06-25 06:58:04 +02:00
}
2018-10-05 05:02:10 +02:00
/// Get Request object
2018-06-25 06:58:04 +02:00
#[inline]
pub fn get_request(pool: &'static MessagePool) -> Request {
2018-10-05 06:14:18 +02:00
if let Some(mut msg) = pool.0.borrow_mut().pop_front() {
if let Some(r) = Rc::get_mut(&mut msg) {
r.reset();
}
return Request { inner: msg };
2018-06-25 06:58:04 +02:00
}
2018-10-05 06:14:18 +02:00
Request::with_pool(pool)
2018-06-25 06:58:04 +02:00
}
/// Get Client Response object
#[inline]
pub fn get_response(pool: &'static MessagePool) -> ClientResponse {
if let Some(mut msg) = pool.0.borrow_mut().pop_front() {
if let Some(r) = Rc::get_mut(&mut msg) {
r.reset();
}
2018-11-14 07:53:30 +01:00
return ClientResponse {
inner: msg,
payload: RefCell::new(None),
2018-11-14 07:53:30 +01:00
};
}
ClientResponse::with_pool(pool)
}
2018-06-25 06:58:04 +02:00
#[inline]
2018-07-04 17:01:27 +02:00
/// Release request instance
pub(crate) fn release(&self, msg: Rc<Message>) {
2018-06-25 06:58:04 +02:00
let v = &mut self.0.borrow_mut();
if v.len() < 128 {
v.push_front(msg);
}
}
}