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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::header;
use crate::status::Status;
use bytecodec::bytes::Utf8Encoder;
use bytecodec::marker::Never;
use bytecodec::{self, ByteCount, Encode, EncodeExt, Eos};
use httpcodec::{
    BodyEncoder, Header, HeaderMut, HttpVersion, ReasonPhrase, Response, ResponseEncoder,
    StatusCode,
};
use std::fmt;

/// HTTP response.
///
/// `T` is the type of the response body.
#[derive(Debug)]
pub struct Res<T>(pub(crate) Response<T>);
impl<T> Res<T> {
    /// Makes a new `Res` instance.
    pub fn new(status: Status, body: T) -> Self {
        let inner = unsafe {
            Response::new(
                HttpVersion::V1_1,
                StatusCode::new_unchecked(status.code()),
                ReasonPhrase::new_unchecked(status.reason_phrase()),
                body,
            )
        };
        Res(inner)
    }

    /// Returns the HTTP version of the response.
    pub fn version(&self) -> HttpVersion {
        self.0.http_version()
    }

    /// Returns the status code of the response.
    pub fn status_code(&self) -> u16 {
        self.0.status_code().as_u16()
    }

    /// Returns the header of the response.
    pub fn header(&self) -> Header {
        self.0.header()
    }

    /// Returns the mutable header of the response.
    pub fn header_mut(&mut self) -> HeaderMut {
        self.0.header_mut()
    }

    /// Returns a reference to the body of the response.
    pub fn body(&self) -> &T {
        self.0.body()
    }

    /// Returns a mutable reference to the body of the response.
    pub fn body_mut(&mut self) -> &mut T {
        self.0.body_mut()
    }
}
impl<T: fmt::Display> fmt::Display for Res<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}
impl<T> From<Response<T>> for Res<T> {
    fn from(f: Response<T>) -> Self {
        Res(f)
    }
}

pub struct ResEncoder(Box<Encode<Item = Never> + Send + 'static>);
impl ResEncoder {
    pub fn new<E>(inner: E) -> Self
    where
        E: Encode<Item = Never> + Send + 'static,
    {
        ResEncoder(Box::new(inner))
    }

    pub fn error(status: Status) -> Self {
        let mut res = Res::new(status, status.reason_phrase());
        res.header_mut().add_field(header::Connection::Close);

        let encoder = ResponseEncoder::new(BodyEncoder::new(Utf8Encoder::new()));
        ResEncoder(Box::new(encoder.last(res.0)))
    }
}
impl fmt::Debug for ResEncoder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "ResEncoder(_)")
    }
}
impl Encode for ResEncoder {
    type Item = Never;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> bytecodec::Result<usize> {
        self.0.encode(buf, eos)
    }

    fn start_encoding(&mut self, _item: Self::Item) -> bytecodec::Result<()> {
        unreachable!()
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}