2018-10-04 20:02:10 -07:00
|
|
|
//! HTTP/1 implementation
|
2019-04-02 11:11:32 -07:00
|
|
|
use bytes::{Bytes, BytesMut};
|
2018-10-13 23:57:31 -07:00
|
|
|
|
2018-10-22 18:18:05 -07:00
|
|
|
mod client;
|
2018-10-04 20:02:10 -07:00
|
|
|
mod codec;
|
|
|
|
mod decoder;
|
|
|
|
mod dispatcher;
|
2018-10-05 10:03:10 -07:00
|
|
|
mod encoder;
|
2019-04-05 16:46:44 -07:00
|
|
|
mod expect;
|
2019-02-07 11:06:05 -08:00
|
|
|
mod payload;
|
2018-10-04 20:02:10 -07:00
|
|
|
mod service;
|
2019-04-08 14:51:16 -07:00
|
|
|
mod upgrade;
|
2019-04-10 12:24:17 -07:00
|
|
|
mod utils;
|
2018-10-04 20:02:10 -07:00
|
|
|
|
2018-11-14 09:38:16 -08:00
|
|
|
pub use self::client::{ClientCodec, ClientPayloadCodec};
|
2018-10-22 18:18:05 -07:00
|
|
|
pub use self::codec::Codec;
|
2018-10-04 20:02:10 -07:00
|
|
|
pub use self::dispatcher::Dispatcher;
|
2019-04-05 16:46:44 -07:00
|
|
|
pub use self::expect::ExpectHandler;
|
2019-04-07 10:29:26 -07:00
|
|
|
pub use self::payload::Payload;
|
2020-12-26 20:46:36 +08:00
|
|
|
pub use self::service::{H1Service, H1ServiceHandler};
|
2019-04-08 14:51:16 -07:00
|
|
|
pub use self::upgrade::UpgradeHandler;
|
2019-04-10 12:24:17 -07:00
|
|
|
pub use self::utils::SendResponse;
|
2018-10-13 23:57:31 -07:00
|
|
|
|
2018-10-22 18:18:05 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
/// Codec message
|
|
|
|
pub enum Message<T> {
|
|
|
|
/// Http message
|
|
|
|
Item(T),
|
|
|
|
/// Payload chunk
|
|
|
|
Chunk(Option<Bytes>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<T> for Message<T> {
|
|
|
|
fn from(item: T) -> Self {
|
|
|
|
Message::Item(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Incoming request type
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum MessageType {
|
|
|
|
None,
|
|
|
|
Payload,
|
2018-11-13 22:53:30 -08:00
|
|
|
Stream,
|
2018-10-22 18:18:05 -07:00
|
|
|
}
|
2018-11-05 19:32:03 -08:00
|
|
|
|
2019-04-02 11:11:32 -07:00
|
|
|
const LW: usize = 2 * 1024;
|
|
|
|
const HW: usize = 32 * 1024;
|
|
|
|
|
|
|
|
pub(crate) fn reserve_readbuf(src: &mut BytesMut) {
|
|
|
|
let cap = src.capacity();
|
|
|
|
if cap < LW {
|
|
|
|
src.reserve(HW - cap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 19:32:03 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-06 22:56:34 -08:00
|
|
|
use crate::request::Request;
|
2018-11-05 19:32:03 -08:00
|
|
|
|
|
|
|
impl Message<Request> {
|
|
|
|
pub fn message(self) -> Request {
|
|
|
|
match self {
|
|
|
|
Message::Item(req) => req,
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chunk(self) -> Bytes {
|
|
|
|
match self {
|
|
|
|
Message::Chunk(Some(data)) => data,
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn eof(self) -> bool {
|
|
|
|
match self {
|
|
|
|
Message::Chunk(None) => true,
|
|
|
|
Message::Chunk(Some(_)) => false,
|
|
|
|
_ => panic!("error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|