use crate::prelude::{internals::*, *};
pub use response::Http1Body;
#[derive(Debug)]
pub enum Error {
Parse(utils::parse::Error),
Io(io::Error),
#[cfg(feature = "http2")]
H2(h2::Error),
VersionNotSupported,
PushOnHttp1,
ClientRefusedResponse,
}
impl From<utils::parse::Error> for Error {
#[inline]
fn from(err: utils::parse::Error) -> Self {
Self::Parse(err)
}
}
impl From<io::Error> for Error {
#[inline]
fn from(err: io::Error) -> Self {
if let io::ErrorKind::BrokenPipe = err.kind() {
return Self::ClientRefusedResponse;
}
Self::Io(err)
}
}
#[cfg(feature = "http2")]
impl From<h2::Error> for Error {
#[inline]
fn from(err: h2::Error) -> Self {
if !err.is_io() && err.reason().is_none() {
return Self::ClientRefusedResponse;
}
Self::H2(err)
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> io::Error {
match err {
Error::Parse(err) => err.into(),
Error::Io(io) => io,
#[cfg(feature = "http2")]
Error::H2(h2) => io::Error::new(io::ErrorKind::InvalidData, h2),
Error::VersionNotSupported => io::Error::new(
io::ErrorKind::InvalidData,
"http version unsupported. Invalid ALPN config.",
),
Error::PushOnHttp1 => io::Error::new(
io::ErrorKind::InvalidInput,
"can not push requests on http/1",
),
Error::ClientRefusedResponse => {
io::Error::new(io::ErrorKind::ConnectionReset, "client refused response")
}
}
}
}
#[derive(Debug)]
pub enum HttpConnection {
Http1(Arc<Mutex<Encryption>>),
#[cfg(feature = "http2")]
Http2(Box<h2::server::Connection<Encryption, bytes::Bytes>>),
}
#[derive(Debug)]
#[must_use]
pub struct ByteBody {
content: Bytes,
read: usize,
}
impl ByteBody {
pub fn inner(&self) -> &Bytes {
&self.content
}
}
impl From<Bytes> for ByteBody {
fn from(b: Bytes) -> Self {
Self {
content: b,
read: 0,
}
}
}
#[derive(Debug)]
pub enum Body {
Bytes(ByteBody),
Http1(response::Http1Body<Encryption>),
#[cfg(feature = "http2")]
Http2(h2::RecvStream),
}
#[derive(Debug)]
#[must_use]
pub enum ResponsePipe {
Http1(Arc<Mutex<Encryption>>),
#[cfg(feature = "http2")]
Http2(h2::server::SendResponse<Bytes>),
}
#[derive(Debug)]
pub enum ResponseBodyPipe {
Http1(Arc<Mutex<Encryption>>),
#[cfg(feature = "http2")]
Http2(h2::SendStream<Bytes>),
}
#[derive(Debug)]
#[must_use]
pub enum PushedResponsePipe {
#[cfg(feature = "http2")]
Http2(h2::server::SendPushedResponse<Bytes>),
}
impl HttpConnection {
pub async fn new(stream: Encryption, version: Version) -> Result<Self, Error> {
#[allow(clippy::match_same_arms)] match version {
Version::HTTP_09 | Version::HTTP_10 | Version::HTTP_11 => {
Ok(Self::Http1(Arc::new(Mutex::new(stream))))
}
#[cfg(feature = "http2")]
Version::HTTP_2 => {
let result = h2::server::Builder::new()
.max_concurrent_streams(512)
.handshake(stream)
.await;
match result {
Ok(connection) => Ok(HttpConnection::Http2(Box::new(connection))),
Err(err) => Err(err.into()),
}
}
#[cfg(not(feature = "http2"))]
Version::HTTP_2 => Err(Error::VersionNotSupported),
_ => Err(Error::VersionNotSupported),
}
}
pub async fn accept(
&mut self,
default_host: Option<&[u8]>,
) -> Result<(Request<Body>, ResponsePipe), Error> {
match self {
Self::Http1(stream) => {
let response = ResponsePipe::Http1(Arc::clone(stream));
request::parse_http_1(Arc::clone(stream), 16 * 1024, default_host)
.await
.map(|request| (request, response))
}
#[cfg(feature = "http2")]
Self::Http2(connection) => match connection.accept().await {
Some(exchange) => match exchange {
Ok((request, response)) => {
Ok((request.map(Body::Http2), ResponsePipe::Http2(response)))
}
Err(err) => Err(err.into()),
},
None => Err(utils::parse::Error::Done.into()),
},
}
}
pub async fn shutdown(self) {
match self {
Self::Http1(h) => {
drop(h.lock().await.shutdown().await);
}
#[cfg(feature = "http2")]
Self::Http2(_h) => {}
}
}
}
mod request {
use super::{
async_bits::read, io, response, utils, Arc, AsyncRead, Body, Bytes, Context, Encryption,
Error, Mutex, Pin, Poll, ReadBuf, Request,
};
#[inline]
pub(crate) async fn parse_http_1(
stream: Arc<Mutex<Encryption>>,
max_len: usize,
default_host: Option<&[u8]>,
) -> Result<Request<Body>, Error> {
let scheme = match &*stream.lock().await {
Encryption::Tcp(_) => "http",
#[cfg(feature = "https")]
Encryption::TcpTls(_) => "https",
};
let lock = stream.lock().await;
let (head, bytes) = read::request(
lock,
max_len,
default_host,
scheme,
std::time::Duration::from_secs(5),
)
.await?;
let body = Body::Http1(response::Http1Body::new(
stream,
bytes,
utils::get_body_length_request(&head),
));
Ok(head.map(|()| body))
}
impl Body {
#[inline]
pub async fn read_to_bytes(&mut self) -> io::Result<Bytes> {
match self {
Self::Bytes(bytes) => Ok(bytes.inner().clone()),
Self::Http1(h1) => h1.read_to_bytes().await,
#[cfg(feature = "http2")]
Self::Http2(h2) => {
let mut bytes = bytes::BytesMut::new();
while let Some(result) = h2.data().await {
let data = result
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
bytes.extend_from_slice(&data);
h2.flow_control().release_capacity(data.len()).unwrap();
}
Ok(bytes.freeze())
}
}
}
}
impl AsyncRead for Body {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Http1(s) => unsafe { Pin::new_unchecked(s).poll_read(cx, buf) },
#[cfg(feature = "http2")]
Self::Http2(tls) => {
let data = match tls.poll_data(cx) {
Poll::Ready(data) => data,
Poll::Pending => return Poll::Pending,
};
match data {
Some(d) => match d {
Ok(data) => buf.put_slice(&data),
Err(err) => {
let err = io::Error::new(io::ErrorKind::InvalidData, err);
return Poll::Ready(Err(err));
}
},
None => return Poll::Ready(Ok(())),
}
Poll::Ready(Ok(()))
}
Self::Bytes(byte_body) => {
let rest = byte_body.inner().get(byte_body.read..).unwrap_or(&[]);
if rest.is_empty() {
return Poll::Ready(Ok(()));
}
let len = std::cmp::min(buf.remaining(), rest.len());
buf.put_slice(&rest[..len]);
byte_body.read += len;
cx.waker().wake_by_ref();
Poll::Pending
}
}
}
}
}
mod response {
use crate::prelude::{application::*, internals::*, *};
#[must_use]
pub struct Http1Body<R: AsyncRead + Unpin> {
reader: Arc<Mutex<R>>,
bytes: Bytes,
offset: usize,
content_length: usize,
}
impl<R: AsyncRead + Unpin> Http1Body<R> {
#[inline]
pub fn new(reader: Arc<Mutex<R>>, bytes: Bytes, content_length: usize) -> Self {
Self {
reader,
bytes,
offset: 0,
content_length,
}
}
#[inline]
pub async fn read_to_bytes(&mut self) -> io::Result<Bytes> {
let len = self.content_length;
if len == 0 {
return Ok(Bytes::new());
}
let mut buffer = BytesMut::with_capacity(len);
if len < self.bytes.len() {
buffer.extend_from_slice(&self.bytes[..len]);
self.offset = len;
} else {
buffer.extend_from_slice(&self.bytes);
self.offset = self.bytes.len();
}
if let Ok(result) = timeout(
Duration::from_millis(250),
async_bits::read_to_end_or_max(&mut buffer, &mut *self, len),
)
.await
{
result?;
} else {
self.content_length = 0;
return Err(io::Error::new(
io::ErrorKind::TimedOut,
"Reading of request body timed out.",
));
}
self.content_length = 0;
Ok(buffer.freeze())
}
}
impl<R: AsyncRead + Unpin + Debug> Debug for Http1Body<R> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut s = f.debug_struct(utils::ident_str!(
Http1Body,
R,
R: AsyncRead + Unpin + Debug
));
utils::fmt_fields!(
s,
(self.reader),
(self.bytes, &"[internal buffer]".as_clean()),
(self.offset),
(self.content_length)
);
s.finish()
}
}
impl<R: AsyncRead + Unpin> AsyncRead for Http1Body<R> {
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
if self.offset < self.bytes.len() {
let remaining = buf.remaining();
if self.bytes.len() - self.offset > remaining {
buf.put_slice(&self.bytes[self.offset..self.offset + remaining]);
self.offset += remaining;
} else {
buf.put_slice(&self.bytes[self.offset..]);
self.offset = self.bytes.len();
}
cx.waker().wake_by_ref();
Poll::Pending
} else {
let mut lock = self.reader.lock();
let mut reader = match unsafe { Pin::new_unchecked(&mut lock) }.poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(r) => r,
};
let size = buf.filled().len();
let result = unsafe { Pin::new_unchecked(&mut *reader).poll_read(cx, buf) };
drop(reader);
drop(lock);
let difference = buf.filled().len() - size;
self.offset += difference;
if self.offset == self.content_length {
return Poll::Ready(Ok(()));
}
result
}
}
}
impl ResponsePipe {
#[inline]
pub async fn send_response(
&mut self,
mut response: Response<()>,
#[allow(unused_variables)] end_of_stream: bool,
) -> Result<ResponseBodyPipe, Error> {
match self {
Self::Http1(s) => {
let mut writer = s.lock().await;
match response
.headers()
.get("connection")
.map(HeaderValue::to_str)
.and_then(Result::ok)
{
Some("close") | None => {
response
.headers_mut()
.insert("connection", HeaderValue::from_static("keep-alive"));
}
_ => {}
}
let mut writer = tokio::io::BufWriter::with_capacity(512, &mut *writer);
async_bits::write::response(&response, b"", &mut writer).await?;
writer.flush().await?;
writer.into_inner();
Ok(ResponseBodyPipe::Http1(Arc::clone(s)))
}
#[cfg(feature = "http2")]
Self::Http2(s) => match s.send_response(response, end_of_stream) {
Err(ref err) if err.get_io().is_none() && err.reason().is_none() => {
Err(Error::ClientRefusedResponse)
}
Err(err) => Err(err.into()),
Ok(pipe) => Ok(ResponseBodyPipe::Http2(pipe)),
},
}
}
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn push_request(
&mut self,
#[allow(unused_variables)] request: Request<()>,
) -> Result<PushedResponsePipe, Error> {
match self {
Self::Http1(_) => Err(Error::PushOnHttp1),
#[cfg(feature = "http2")]
Self::Http2(h2) => match h2.push_request(request) {
Ok(pipe) => Ok(PushedResponsePipe::Http2(pipe)),
Err(err) => Err(err.into()),
},
}
}
#[inline]
pub fn ensure_version_and_length<T>(&self, response: &mut Response<T>, len: usize) {
match self {
Self::Http1(_) => match response.version() {
Version::HTTP_09 | Version::HTTP_10 | Version::HTTP_11 => {
utils::set_content_length(response.headers_mut(), len);
}
_ => *response.version_mut() = Version::HTTP_11,
},
#[cfg(feature = "http2")]
Self::Http2(_) => *response.version_mut() = Version::HTTP_2,
}
}
}
#[allow(unused_variables)]
impl PushedResponsePipe {
#[inline]
#[allow(clippy::needless_pass_by_value)]
pub fn send_response(
&mut self,
response: Response<()>,
end_of_stream: bool,
) -> Result<ResponseBodyPipe, Error> {
match self {
#[cfg(feature = "http2")]
Self::Http2(s) => {
let mut response = response;
*response.version_mut() = Version::HTTP_2;
match s.send_response(response, end_of_stream) {
Err(err) => Err(err.into()),
Ok(pipe) => Ok(ResponseBodyPipe::Http2(pipe)),
}
}
#[cfg(not(any(feature = "http2")))]
_ => unreachable!(),
}
}
#[inline]
#[allow(unused_variables)]
pub fn ensure_version<T>(&self, response: &mut Response<T>) {
match self {
#[cfg(feature = "http2")]
Self::Http2(_) => *response.version_mut() = Version::HTTP_2,
#[cfg(not(any(feature = "http2")))]
_ => unreachable!(),
}
}
}
impl ResponseBodyPipe {
#[inline]
pub async fn send(&mut self, data: Bytes) -> Result<(), Error> {
self.send_with_maybe_close(data, false).await
}
#[inline]
pub(crate) async fn send_with_maybe_close(
&mut self,
data: Bytes,
end_of_stream: bool,
) -> Result<(), Error> {
match self {
Self::Http1(h1) => {
let mut lock = h1.lock().await;
lock.write_all(&data).await?;
if end_of_stream {
lock.flush().await?;
}
}
#[cfg(feature = "http2")]
Self::Http2(h2) => h2.send_data(data, end_of_stream)?,
}
Ok(())
}
#[inline]
pub async fn close(&mut self) -> Result<(), Error> {
match self {
Self::Http1(h1) => h1.lock().await.flush().await.map_err(Into::into),
#[cfg(feature = "http2")]
Self::Http2(h2) => h2.send_data(Bytes::new(), true).map_err(Error::from),
}
}
}
impl AsyncRead for ResponseBodyPipe {
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>,
) -> Poll<io::Result<()>> {
match self.get_mut() {
Self::Http1(s) => match s.try_lock() {
Err(_) => Poll::Pending,
Ok(mut s) => Pin::new(&mut *s).poll_read(cx, buf),
},
#[cfg(feature = "http2")]
Self::Http2(_) => Poll::Ready(Ok(())),
}
}
}
impl AsyncWrite for ResponseBodyPipe {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
match self.get_mut() {
Self::Http1(s) => match s.try_lock() {
Err(_) => Poll::Pending,
Ok(mut s) => Pin::new(&mut *s).poll_write(cx, buf),
},
#[cfg(feature = "http2")]
Self::Http2(s) => Poll::Ready(
s.send_data(Bytes::copy_from_slice(buf), false)
.map_err(|e| {
if e.is_io() {
e.into_io().unwrap()
} else {
io::Error::new(io::ErrorKind::Other, e.to_string())
}
})
.map(|()| buf.len()),
),
}
}
fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
Poll::Ready(Ok(()))
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> {
if let Self::Http1(s) = self.get_mut() {
if let Ok(mut s) = s.try_lock() {
Pin::new(&mut *s).poll_flush(cx)
} else {
Poll::Pending
}
} else {
Poll::Ready(Ok(()))
}
}
}
}