2019-02-08 06:16:46 +01:00
|
|
|
use std::cell::{Ref, RefCell, RefMut};
|
2019-04-16 18:54:02 +02:00
|
|
|
use std::net;
|
2018-11-17 04:28:07 +01:00
|
|
|
use std::rc::Rc;
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
use bitflags::bitflags;
|
|
|
|
|
2018-12-06 23:32:52 +01:00
|
|
|
use crate::extensions::Extensions;
|
2019-04-07 00:02:02 +02:00
|
|
|
use crate::header::HeaderMap;
|
|
|
|
use crate::http::{header, Method, StatusCode, Uri, Version};
|
2018-11-17 04:28:07 +01:00
|
|
|
|
2018-11-19 03:17:38 +01:00
|
|
|
/// Represents various types of connection
|
|
|
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
|
|
|
pub enum ConnectionType {
|
|
|
|
/// Close connection after response
|
|
|
|
Close,
|
2021-02-12 01:15:25 +01:00
|
|
|
|
2018-11-19 03:17:38 +01:00
|
|
|
/// Keep connection alive after response
|
|
|
|
KeepAlive,
|
2021-02-12 01:15:25 +01:00
|
|
|
|
2018-11-19 03:17:38 +01:00
|
|
|
/// Connection is upgraded to different type
|
|
|
|
Upgrade,
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
bitflags! {
|
|
|
|
pub(crate) struct Flags: u8 {
|
|
|
|
const CLOSE = 0b0000_0001;
|
|
|
|
const KEEP_ALIVE = 0b0000_0010;
|
|
|
|
const UPGRADE = 0b0000_0100;
|
2019-04-06 01:46:44 +02:00
|
|
|
const EXPECT = 0b0000_1000;
|
|
|
|
const NO_CHUNKING = 0b0001_0000;
|
2019-04-24 19:48:49 +02:00
|
|
|
const CAMEL_CASE = 0b0010_0000;
|
2019-03-27 18:38:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
pub trait Head: Default + 'static {
|
|
|
|
fn clear(&mut self);
|
|
|
|
|
2021-01-09 16:40:20 +01:00
|
|
|
fn with_pool<F, R>(f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&MessagePool<Self>) -> R;
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
#[derive(Debug)]
|
2018-11-17 04:28:07 +01:00
|
|
|
pub struct RequestHead {
|
|
|
|
pub uri: Uri,
|
|
|
|
pub method: Method,
|
|
|
|
pub version: Version,
|
|
|
|
pub headers: HeaderMap,
|
2019-02-09 19:33:49 +01:00
|
|
|
pub extensions: RefCell<Extensions>,
|
2019-04-16 18:54:02 +02:00
|
|
|
pub peer_addr: Option<net::SocketAddr>,
|
2019-03-27 18:38:01 +01:00
|
|
|
flags: Flags,
|
2018-11-17 04:28:07 +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),
|
2019-03-27 18:38:01 +01:00
|
|
|
flags: Flags::empty(),
|
2019-04-16 18:54:02 +02:00
|
|
|
peer_addr: None,
|
2019-02-09 19:33:49 +01:00
|
|
|
extensions: RefCell::new(Extensions::new()),
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Head for RequestHead {
|
|
|
|
fn clear(&mut self) {
|
2019-03-27 18:38:01 +01:00
|
|
|
self.flags = Flags::empty();
|
2018-11-17 04:28:07 +01:00
|
|
|
self.headers.clear();
|
2021-01-04 14:03:46 +01:00
|
|
|
self.extensions.get_mut().clear();
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-09 16:40:20 +01:00
|
|
|
fn with_pool<F, R>(f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&MessagePool<Self>) -> R,
|
|
|
|
{
|
|
|
|
REQUEST_POOL.with(|p| f(p))
|
2019-03-27 18:38:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestHead {
|
|
|
|
/// Message extensions
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-27 18:38:01 +01:00
|
|
|
self.extensions.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the message's extensions
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
pub fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
2019-03-27 18:38:01 +01:00
|
|
|
self.extensions.borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the message headers.
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2019-02-13 22:52:11 +01:00
|
|
|
&self.headers
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
/// Mutable reference to the message headers.
|
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2019-02-13 22:52:11 +01:00
|
|
|
&mut self.headers
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:48:49 +02:00
|
|
|
/// Is to uppercase headers with Camel-Case.
|
2020-04-21 05:09:35 +02:00
|
|
|
/// Default is `false`
|
2019-04-24 19:48:49 +02:00
|
|
|
#[inline]
|
2019-04-24 20:27:57 +02:00
|
|
|
pub fn camel_case_headers(&self) -> bool {
|
2019-04-24 19:48:49 +02:00
|
|
|
self.flags.contains(Flags::CAMEL_CASE)
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:09:35 +02:00
|
|
|
/// Set `true` to send headers which are formatted as Camel-Case.
|
2019-04-24 19:48:49 +02:00
|
|
|
#[inline]
|
2019-04-24 20:27:57 +02:00
|
|
|
pub fn set_camel_case_headers(&mut self, val: bool) {
|
2019-04-24 19:48:49 +02:00
|
|
|
if val {
|
|
|
|
self.flags.insert(Flags::CAMEL_CASE);
|
|
|
|
} else {
|
|
|
|
self.flags.remove(Flags::CAMEL_CASE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Set connection type of the message
|
|
|
|
pub fn set_connection_type(&mut self, ctype: ConnectionType) {
|
|
|
|
match ctype {
|
|
|
|
ConnectionType::Close => self.flags.insert(Flags::CLOSE),
|
|
|
|
ConnectionType::KeepAlive => self.flags.insert(Flags::KEEP_ALIVE),
|
|
|
|
ConnectionType::Upgrade => self.flags.insert(Flags::UPGRADE),
|
|
|
|
}
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Connection type
|
|
|
|
pub fn connection_type(&self) -> ConnectionType {
|
|
|
|
if self.flags.contains(Flags::CLOSE) {
|
|
|
|
ConnectionType::Close
|
|
|
|
} else if self.flags.contains(Flags::KEEP_ALIVE) {
|
|
|
|
ConnectionType::KeepAlive
|
|
|
|
} else if self.flags.contains(Flags::UPGRADE) {
|
|
|
|
ConnectionType::Upgrade
|
2018-11-19 23:57:12 +01:00
|
|
|
} else if self.version < Version::HTTP_11 {
|
2018-11-19 03:17:38 +01:00
|
|
|
ConnectionType::Close
|
2018-11-19 02:52:56 +01:00
|
|
|
} else {
|
2018-11-19 03:17:38 +01:00
|
|
|
ConnectionType::KeepAlive
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
/// Connection upgrade status
|
|
|
|
pub fn upgrade(&self) -> bool {
|
2019-03-18 13:26:12 +01:00
|
|
|
if let Some(hdr) = self.headers().get(header::CONNECTION) {
|
|
|
|
if let Ok(s) = hdr.to_str() {
|
|
|
|
s.to_ascii_lowercase().contains("upgrade")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:33:49 +01:00
|
|
|
#[inline]
|
2019-03-27 18:38:01 +01:00
|
|
|
/// Get response body chunking state
|
|
|
|
pub fn chunked(&self) -> bool {
|
|
|
|
!self.flags.contains(Flags::NO_CHUNKING)
|
2019-02-09 19:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-03-27 18:38:01 +01:00
|
|
|
pub fn no_chunking(&mut self, val: bool) {
|
|
|
|
if val {
|
|
|
|
self.flags.insert(Flags::NO_CHUNKING);
|
|
|
|
} else {
|
|
|
|
self.flags.remove(Flags::NO_CHUNKING);
|
|
|
|
}
|
2019-02-09 19:33:49 +01:00
|
|
|
}
|
2019-04-06 01:46:44 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Request contains `EXPECT` header
|
|
|
|
pub fn expect(&self) -> bool {
|
|
|
|
self.flags.contains(Flags::EXPECT)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn set_expect(&mut self) {
|
|
|
|
self.flags.insert(Flags::EXPECT);
|
|
|
|
}
|
2019-02-09 19:33:49 +01:00
|
|
|
}
|
|
|
|
|
2019-09-10 06:29:32 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum RequestHeadType {
|
|
|
|
Owned(RequestHead),
|
|
|
|
Rc(Rc<RequestHead>, Option<HeaderMap>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestHeadType {
|
|
|
|
pub fn extra_headers(&self) -> Option<&HeaderMap> {
|
|
|
|
match self {
|
|
|
|
RequestHeadType::Owned(_) => None,
|
|
|
|
RequestHeadType::Rc(_, headers) => headers.as_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<RequestHead> for RequestHeadType {
|
|
|
|
fn as_ref(&self) -> &RequestHead {
|
|
|
|
match self {
|
|
|
|
RequestHeadType::Owned(head) => &head,
|
|
|
|
RequestHeadType::Rc(head, _) => head.as_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<RequestHead> for RequestHeadType {
|
|
|
|
fn from(head: RequestHead) -> Self {
|
|
|
|
RequestHeadType::Owned(head)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:57:12 +01:00
|
|
|
#[derive(Debug)]
|
2018-11-17 04:28:07 +01:00
|
|
|
pub struct ResponseHead {
|
2018-11-17 18:03:35 +01:00
|
|
|
pub version: Version,
|
2018-11-17 04:28:07 +01:00
|
|
|
pub status: StatusCode,
|
|
|
|
pub headers: HeaderMap,
|
|
|
|
pub reason: Option<&'static str>,
|
2019-03-05 22:16:26 +01:00
|
|
|
pub(crate) extensions: RefCell<Extensions>,
|
2019-03-27 18:38:01 +01:00
|
|
|
flags: Flags,
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:06:21 +02:00
|
|
|
impl ResponseHead {
|
|
|
|
/// Create new instance of `ResponseHead` type
|
|
|
|
#[inline]
|
|
|
|
pub fn new(status: StatusCode) -> ResponseHead {
|
2018-11-17 04:28:07 +01:00
|
|
|
ResponseHead {
|
2019-04-08 08:06:21 +02:00
|
|
|
status,
|
2018-11-17 18:03:35 +01:00
|
|
|
version: Version::default(),
|
2019-04-07 00:02:02 +02:00
|
|
|
headers: HeaderMap::with_capacity(12),
|
2018-11-17 04:28:07 +01:00
|
|
|
reason: None,
|
2019-03-27 18:38:01 +01:00
|
|
|
flags: Flags::empty(),
|
2019-03-05 22:16:26 +01:00
|
|
|
extensions: RefCell::new(Extensions::new()),
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:16:26 +01:00
|
|
|
/// Message extensions
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
pub fn extensions(&self) -> Ref<'_, Extensions> {
|
2019-03-05 22:16:26 +01:00
|
|
|
self.extensions.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Mutable reference to a the message's extensions
|
|
|
|
#[inline]
|
2019-12-07 19:46:51 +01:00
|
|
|
pub fn extensions_mut(&self) -> RefMut<'_, Extensions> {
|
2019-03-05 22:16:26 +01:00
|
|
|
self.extensions.borrow_mut()
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Read the message headers.
|
|
|
|
pub fn headers(&self) -> &HeaderMap {
|
2019-02-13 22:52:11 +01:00
|
|
|
&self.headers
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Mutable reference to the message headers.
|
|
|
|
pub fn headers_mut(&mut self) -> &mut HeaderMap {
|
2019-02-13 22:52:11 +01:00
|
|
|
&mut self.headers
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Set connection type of the message
|
|
|
|
pub fn set_connection_type(&mut self, ctype: ConnectionType) {
|
|
|
|
match ctype {
|
|
|
|
ConnectionType::Close => self.flags.insert(Flags::CLOSE),
|
|
|
|
ConnectionType::KeepAlive => self.flags.insert(Flags::KEEP_ALIVE),
|
|
|
|
ConnectionType::Upgrade => self.flags.insert(Flags::UPGRADE),
|
|
|
|
}
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn connection_type(&self) -> ConnectionType {
|
|
|
|
if self.flags.contains(Flags::CLOSE) {
|
|
|
|
ConnectionType::Close
|
|
|
|
} else if self.flags.contains(Flags::KEEP_ALIVE) {
|
|
|
|
ConnectionType::KeepAlive
|
|
|
|
} else if self.flags.contains(Flags::UPGRADE) {
|
|
|
|
ConnectionType::Upgrade
|
2018-11-19 23:57:12 +01:00
|
|
|
} else if self.version < Version::HTTP_11 {
|
2018-11-19 03:17:38 +01:00
|
|
|
ConnectionType::Close
|
2018-11-19 02:52:56 +01:00
|
|
|
} else {
|
2018-11-19 03:17:38 +01:00
|
|
|
ConnectionType::KeepAlive
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Check if keep-alive is enabled
|
|
|
|
pub fn keep_alive(&self) -> bool {
|
|
|
|
self.connection_type() == ConnectionType::KeepAlive
|
2019-03-18 13:26:12 +01:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:38:01 +01:00
|
|
|
#[inline]
|
|
|
|
/// Check upgrade status of this message
|
|
|
|
pub fn upgrade(&self) -> bool {
|
|
|
|
self.connection_type() == ConnectionType::Upgrade
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
|
2018-11-19 02:52:56 +01:00
|
|
|
/// Get custom reason for the response
|
|
|
|
#[inline]
|
|
|
|
pub fn reason(&self) -> &str {
|
|
|
|
if let Some(reason) = self.reason {
|
|
|
|
reason
|
|
|
|
} else {
|
|
|
|
self.status
|
|
|
|
.canonical_reason()
|
|
|
|
.unwrap_or("<unknown status code>")
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 18:38:01 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub(crate) fn ctype(&self) -> Option<ConnectionType> {
|
|
|
|
if self.flags.contains(Flags::CLOSE) {
|
|
|
|
Some(ConnectionType::Close)
|
|
|
|
} else if self.flags.contains(Flags::KEEP_ALIVE) {
|
|
|
|
Some(ConnectionType::KeepAlive)
|
|
|
|
} else if self.flags.contains(Flags::UPGRADE) {
|
|
|
|
Some(ConnectionType::Upgrade)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Get response body chunking state
|
|
|
|
pub fn chunked(&self) -> bool {
|
|
|
|
!self.flags.contains(Flags::NO_CHUNKING)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Set no chunking for payload
|
|
|
|
pub fn no_chunking(&mut self, val: bool) {
|
|
|
|
if val {
|
|
|
|
self.flags.insert(Flags::NO_CHUNKING);
|
|
|
|
} else {
|
|
|
|
self.flags.remove(Flags::NO_CHUNKING);
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 02:52:56 +01:00
|
|
|
}
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
pub struct Message<T: Head> {
|
2021-04-09 19:07:10 +02:00
|
|
|
/// Rc here should not be cloned by anyone.
|
|
|
|
/// It's used to reuse allocation of T and no shared ownership is allowed.
|
2019-02-09 19:33:49 +01:00
|
|
|
head: Rc<T>,
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Head> Message<T> {
|
2019-02-08 06:16:46 +01:00
|
|
|
/// Get new message from the pool of objects
|
|
|
|
pub fn new() -> Self {
|
2021-01-09 16:40:20 +01:00
|
|
|
T::with_pool(|p| p.get_message())
|
2019-02-08 06:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Head> std::ops::Deref for Message<T> {
|
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
2019-02-09 19:33:49 +01:00
|
|
|
&self.head.as_ref()
|
2019-02-08 06:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Head> std::ops::DerefMut for Message<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
2019-02-09 19:33:49 +01:00
|
|
|
Rc::get_mut(&mut self.head).expect("Multiple copies exist")
|
2019-02-08 06:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Head> Drop for Message<T> {
|
|
|
|
fn drop(&mut self) {
|
2021-02-07 21:19:10 +01:00
|
|
|
T::with_pool(|p| p.release(self.head.clone()))
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 00:02:02 +02:00
|
|
|
pub(crate) struct BoxedResponseHead {
|
|
|
|
head: Option<Box<ResponseHead>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BoxedResponseHead {
|
|
|
|
/// Get new message from the pool of objects
|
2019-04-08 08:06:21 +02:00
|
|
|
pub fn new(status: StatusCode) -> Self {
|
|
|
|
RESPONSE_POOL.with(|p| p.get_message(status))
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
2019-11-22 06:49:35 +01:00
|
|
|
|
|
|
|
pub(crate) fn take(&mut self) -> Self {
|
|
|
|
BoxedResponseHead {
|
|
|
|
head: self.head.take(),
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for BoxedResponseHead {
|
|
|
|
type Target = ResponseHead;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
self.head.as_ref().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::DerefMut for BoxedResponseHead {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
self.head.as_mut().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for BoxedResponseHead {
|
|
|
|
fn drop(&mut self) {
|
2019-11-22 06:49:35 +01:00
|
|
|
if let Some(head) = self.head.take() {
|
|
|
|
RESPONSE_POOL.with(move |p| p.release(head))
|
|
|
|
}
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
#[doc(hidden)]
|
|
|
|
/// Request's objects pool
|
2019-04-08 08:06:21 +02:00
|
|
|
pub struct MessagePool<T: Head>(RefCell<Vec<Rc<T>>>);
|
2018-11-17 04:28:07 +01:00
|
|
|
|
2019-04-07 00:02:02 +02:00
|
|
|
#[doc(hidden)]
|
2019-07-17 09:55:44 +02:00
|
|
|
#[allow(clippy::vec_box)]
|
2019-04-07 00:02:02 +02:00
|
|
|
/// Request's objects pool
|
2019-04-08 08:06:21 +02:00
|
|
|
pub struct BoxedResponsePool(RefCell<Vec<Box<ResponseHead>>>);
|
2019-04-07 00:02:02 +02:00
|
|
|
|
2021-01-09 16:40:20 +01:00
|
|
|
thread_local!(static REQUEST_POOL: MessagePool<RequestHead> = MessagePool::<RequestHead>::create());
|
|
|
|
thread_local!(static RESPONSE_POOL: BoxedResponsePool = BoxedResponsePool::create());
|
2018-11-17 04:28:07 +01:00
|
|
|
|
|
|
|
impl<T: Head> MessagePool<T> {
|
2021-01-09 16:40:20 +01:00
|
|
|
fn create() -> MessagePool<T> {
|
|
|
|
MessagePool(RefCell::new(Vec::with_capacity(128)))
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 06:16:46 +01:00
|
|
|
/// Get message from the pool
|
|
|
|
#[inline]
|
2021-01-09 16:40:20 +01:00
|
|
|
fn get_message(&self) -> Message<T> {
|
2019-04-08 08:06:21 +02:00
|
|
|
if let Some(mut msg) = self.0.borrow_mut().pop() {
|
2021-01-04 14:03:46 +01:00
|
|
|
// Message is put in pool only when it's the last copy.
|
|
|
|
// which means it's guaranteed to be unique when popped out.
|
|
|
|
Rc::get_mut(&mut msg)
|
|
|
|
.expect("Multiple copies exist")
|
|
|
|
.clear();
|
2019-04-07 00:02:02 +02:00
|
|
|
Message { head: msg }
|
2019-02-08 06:16:46 +01:00
|
|
|
} else {
|
|
|
|
Message {
|
2019-02-09 19:33:49 +01:00
|
|
|
head: Rc::new(T::default()),
|
2019-02-08 06:16:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 04:28:07 +01:00
|
|
|
#[inline]
|
|
|
|
/// Release request instance
|
2019-02-09 19:33:49 +01:00
|
|
|
fn release(&self, msg: Rc<T>) {
|
2018-11-17 04:28:07 +01:00
|
|
|
let v = &mut self.0.borrow_mut();
|
|
|
|
if v.len() < 128 {
|
2019-04-08 08:06:21 +02:00
|
|
|
v.push(msg);
|
2018-11-17 04:28:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 00:02:02 +02:00
|
|
|
|
|
|
|
impl BoxedResponsePool {
|
2021-01-09 16:40:20 +01:00
|
|
|
fn create() -> BoxedResponsePool {
|
|
|
|
BoxedResponsePool(RefCell::new(Vec::with_capacity(128)))
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get message from the pool
|
|
|
|
#[inline]
|
2021-01-09 16:40:20 +01:00
|
|
|
fn get_message(&self, status: StatusCode) -> BoxedResponseHead {
|
2019-04-08 08:06:21 +02:00
|
|
|
if let Some(mut head) = self.0.borrow_mut().pop() {
|
2019-04-07 00:02:02 +02:00
|
|
|
head.reason = None;
|
2019-04-08 08:06:21 +02:00
|
|
|
head.status = status;
|
2019-04-07 00:02:02 +02:00
|
|
|
head.headers.clear();
|
|
|
|
head.flags = Flags::empty();
|
|
|
|
BoxedResponseHead { head: Some(head) }
|
|
|
|
} else {
|
|
|
|
BoxedResponseHead {
|
2021-01-07 22:55:00 +01:00
|
|
|
head: Some(Box::new(ResponseHead::new(status))),
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
/// Release request instance
|
2021-01-07 22:55:00 +01:00
|
|
|
fn release(&self, mut msg: Box<ResponseHead>) {
|
2019-04-07 00:02:02 +02:00
|
|
|
let v = &mut self.0.borrow_mut();
|
|
|
|
if v.len() < 128 {
|
2021-01-07 22:55:00 +01:00
|
|
|
msg.extensions.get_mut().clear();
|
2019-04-08 08:06:21 +02:00
|
|
|
v.push(msg);
|
2019-04-07 00:02:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|