1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-25 00:12:59 +01:00
actix-extras/src/server/message.rs

264 lines
6.9 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;
use std::net::SocketAddr;
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, Uri, Version};
use extensions::Extensions;
use httpmessage::HttpMessage;
use info::ConnectionInfo;
use payload::Payload;
use server::ServerSettings;
use uri::Url as InnerUrl;
bitflags! {
pub(crate) struct MessageFlags: u8 {
const KEEPALIVE = 0b0000_0001;
const CONN_INFO = 0b0000_0010;
}
}
/// Request's context
pub struct Request {
2018-07-04 18:52:49 +02:00
pub(crate) inner: Rc<InnerRequest>,
2018-06-25 06:58:04 +02:00
}
pub(crate) struct InnerRequest {
pub(crate) version: Version,
pub(crate) method: Method,
pub(crate) url: InnerUrl,
pub(crate) flags: Cell<MessageFlags>,
pub(crate) headers: HeaderMap,
pub(crate) extensions: RefCell<Extensions>,
pub(crate) addr: Option<SocketAddr>,
pub(crate) info: RefCell<ConnectionInfo>,
pub(crate) payload: RefCell<Option<Payload>>,
pub(crate) settings: ServerSettings,
pub(crate) stream_extensions: Option<Rc<Extensions>>,
2018-07-04 18:52:49 +02:00
pool: &'static RequestPool,
}
impl InnerRequest {
#[inline]
/// Reset request instance
pub fn reset(&mut self) {
self.headers.clear();
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 {
&self.inner.headers
}
#[inline]
fn payload(&self) -> Payload {
if let Some(payload) = self.inner.payload.borrow_mut().take() {
payload
} else {
Payload::empty()
}
}
}
impl Request {
/// Create new RequestContext instance
2018-07-04 18:52:49 +02:00
pub(crate) fn new(pool: &'static RequestPool, settings: ServerSettings) -> Request {
2018-06-25 06:58:04 +02:00
Request {
2018-07-04 18:52:49 +02:00
inner: Rc::new(InnerRequest {
pool,
2018-06-25 06:58:04 +02:00
settings,
method: Method::GET,
url: InnerUrl::default(),
version: Version::HTTP_11,
headers: HeaderMap::with_capacity(16),
flags: Cell::new(MessageFlags::empty()),
addr: None,
info: RefCell::new(ConnectionInfo::default()),
payload: RefCell::new(None),
extensions: RefCell::new(Extensions::new()),
stream_extensions: None,
2018-06-25 06:58:04 +02:00
}),
}
}
2018-07-04 18:52:49 +02:00
#[inline]
pub(crate) fn inner(&self) -> &InnerRequest {
self.inner.as_ref()
}
#[inline]
pub(crate) fn inner_mut(&mut self) -> &mut InnerRequest {
Rc::get_mut(&mut self.inner).expect("Multiple copies exist")
}
2018-06-25 06:58:04 +02:00
#[inline]
pub(crate) fn url(&self) -> &InnerUrl {
2018-07-04 18:52:49 +02:00
&self.inner().url
2018-06-25 06:58:04 +02:00
}
/// Read the Request Uri.
#[inline]
pub fn uri(&self) -> &Uri {
2018-07-04 18:52:49 +02:00
self.inner().url.uri()
2018-06-25 06:58:04 +02:00
}
/// Read the Request method.
#[inline]
pub fn method(&self) -> &Method {
2018-07-04 18:52:49 +02:00
&self.inner().method
2018-06-25 06:58:04 +02:00
}
/// Read the Request Version.
#[inline]
pub fn version(&self) -> Version {
2018-07-04 18:52:49 +02:00
self.inner().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-07-04 18:52:49 +02:00
&self.inner().headers
2018-06-25 06:58:04 +02:00
}
#[inline]
/// Returns mutable Request's headers.
pub fn headers_mut(&mut self) -> &mut HeaderMap {
2018-07-04 18:52:49 +02:00
&mut self.inner_mut().headers
2018-06-25 06:58:04 +02:00
}
/// Peer socket address
///
/// Peer address is actual socket address, if proxy is used in front of
/// actix http server, then peer address would be address of this proxy.
///
/// To get client connection information `connection_info()` method should
/// be used.
pub fn peer_addr(&self) -> Option<SocketAddr> {
2018-07-04 18:52:49 +02:00
self.inner().addr
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-07-04 18:52:49 +02:00
if let Some(conn) = self.inner().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-07-04 18:52:49 +02:00
self.inner().method == Method::CONNECT
2018-06-25 06:58:04 +02:00
}
/// Get *ConnectionInfo* for the correct request.
pub fn connection_info(&self) -> Ref<ConnectionInfo> {
2018-07-04 18:52:49 +02:00
if self.inner().flags.get().contains(MessageFlags::CONN_INFO) {
self.inner().info.borrow()
2018-06-25 06:58:04 +02:00
} else {
2018-07-04 18:52:49 +02:00
let mut flags = self.inner().flags.get();
2018-06-25 06:58:04 +02:00
flags.insert(MessageFlags::CONN_INFO);
2018-07-04 18:52:49 +02:00
self.inner().flags.set(flags);
self.inner().info.borrow_mut().update(self);
self.inner().info.borrow()
2018-06-25 06:58:04 +02:00
}
}
/// Io stream extensions
#[inline]
pub fn stream_extensions(&self) -> Option<&Extensions> {
self.inner().stream_extensions.as_ref().map(|e| e.as_ref())
}
2018-06-25 06:58:04 +02:00
/// Server settings
#[inline]
pub fn server_settings(&self) -> &ServerSettings {
2018-07-04 18:52:49 +02:00
&self.inner().settings
2018-06-25 06:58:04 +02:00
}
2018-07-04 18:57:40 +02:00
pub(crate) fn clone(&self) -> Self {
Request {
inner: self.inner.clone(),
}
}
2018-07-04 18:52:49 +02:00
pub(crate) fn release(self) {
let mut inner = self.inner;
if let Some(r) = Rc::get_mut(&mut inner) {
r.reset();
} else {
return;
}
inner.pool.release(inner);
2018-06-25 06:58:04 +02:00
}
}
2018-07-04 18:52:49 +02:00
pub(crate) struct RequestPool(
RefCell<VecDeque<Rc<InnerRequest>>>,
RefCell<ServerSettings>,
);
2018-06-25 06:58:04 +02:00
thread_local!(static POOL: &'static RequestPool = RequestPool::create());
impl RequestPool {
fn create() -> &'static RequestPool {
let pool = RequestPool(
RefCell::new(VecDeque::with_capacity(128)),
RefCell::new(ServerSettings::default()),
);
Box::leak(Box::new(pool))
}
pub fn pool(settings: ServerSettings) -> &'static RequestPool {
POOL.with(|p| {
*p.1.borrow_mut() = settings;
*p
})
}
#[inline]
2018-07-04 18:52:49 +02:00
pub fn get(pool: &'static RequestPool) -> Request {
if let Some(msg) = pool.0.borrow_mut().pop_front() {
Request { inner: msg }
2018-06-25 06:58:04 +02:00
} else {
2018-07-04 18:52:49 +02:00
Request::new(pool, pool.1.borrow().clone())
2018-06-25 06:58:04 +02:00
}
}
#[inline]
2018-07-04 17:01:27 +02:00
/// Release request instance
2018-07-04 18:52:49 +02:00
pub fn release(&self, msg: Rc<InnerRequest>) {
2018-06-25 06:58:04 +02:00
let v = &mut self.0.borrow_mut();
if v.len() < 128 {
v.push_front(msg);
}
}
}