1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![feature(test)]

use std::marker::Unpin;

use tokio_util::codec::Framed;
use tokio::stream::StreamExt;
use tokio::io::{
    AsyncRead,
    AsyncWrite,
};

pub mod codec;
pub use codec::HttpCodec;

pub mod error;
pub use error::HttpResult;
pub use error::HttpError;

pub mod request;
pub use request::Request;

pub mod method;
pub use method::Method;

#[derive(Debug)]
pub struct Http<S> {
    pub transport: Framed<S, HttpCodec>,
}

impl<S> Http<S>
where
    S: AsyncRead + AsyncWrite + Unpin,
{
    pub fn new(stream: S) -> Self {
        let transport = Framed::new(stream, HttpCodec::default());

        Self {
            transport,
        }
    }

    pub async fn next(&mut self) -> Option<Request> {
        if let Some(Ok(req)) = self.transport.next().await {
            return Some(req)
        }

        None
    }
}