2018-10-05 03:02:10 +00:00
|
|
|
//! HTTP/1 implementation
|
2018-11-20 00:11:58 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
2018-10-14 06:57:31 +00:00
|
|
|
use actix_net::codec::Framed;
|
2018-10-23 01:18:05 +00:00
|
|
|
use bytes::Bytes;
|
2018-10-14 06:57:31 +00:00
|
|
|
|
2018-10-23 01:18:05 +00:00
|
|
|
mod client;
|
2018-10-05 03:02:10 +00:00
|
|
|
mod codec;
|
|
|
|
mod decoder;
|
|
|
|
mod dispatcher;
|
2018-10-05 17:03:10 +00:00
|
|
|
mod encoder;
|
2018-10-05 03:02:10 +00:00
|
|
|
mod service;
|
|
|
|
|
2018-11-14 17:38:16 +00:00
|
|
|
pub use self::client::{ClientCodec, ClientPayloadCodec};
|
2018-10-23 01:18:05 +00:00
|
|
|
pub use self::codec::Codec;
|
2018-10-05 03:02:10 +00:00
|
|
|
pub use self::dispatcher::Dispatcher;
|
2018-10-15 23:46:13 +00:00
|
|
|
pub use self::service::{H1Service, H1ServiceHandler, OneRequest};
|
2018-10-14 06:57:31 +00:00
|
|
|
|
2018-12-06 22:32:52 +00:00
|
|
|
use crate::request::Request;
|
2018-10-14 06:57:31 +00:00
|
|
|
|
|
|
|
/// H1 service response type
|
|
|
|
pub enum H1ServiceResult<T> {
|
|
|
|
Disconnected,
|
|
|
|
Shutdown(T),
|
|
|
|
Unhandled(Request, Framed<T, Codec>),
|
|
|
|
}
|
2018-10-23 01:18:05 +00:00
|
|
|
|
2018-11-20 00:11:58 +00:00
|
|
|
impl<T: fmt::Debug> fmt::Debug for H1ServiceResult<T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
H1ServiceResult::Disconnected => write!(f, "H1ServiceResult::Disconnected"),
|
|
|
|
H1ServiceResult::Shutdown(ref v) => {
|
|
|
|
write!(f, "H1ServiceResult::Shutdown({:?})", v)
|
|
|
|
}
|
|
|
|
H1ServiceResult::Unhandled(ref req, _) => {
|
|
|
|
write!(f, "H1ServiceResult::Unhandled({:?})", req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:18:05 +00: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-14 06:53:30 +00:00
|
|
|
Stream,
|
2018-10-23 01:18:05 +00:00
|
|
|
}
|
2018-11-06 03:32:03 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
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"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|