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
113
114
115
116
117
118
119
120
121
122
//! Request Payload Trait

use crate::{Error, IntoResponse, Response, Result, StatusCode, ThisError};

#[derive(ThisError, Debug)]
pub enum PayloadError {
    /// 400
    #[error("failed to read payload")]
    Read,

    /// 400
    #[error("failed to parse payload")]
    Parse,

    /// 400
    #[error("multipart missing boundary")]
    MissingBoundary,

    /// 400
    #[error("parse utf8: {0}")]
    Utf8(#[from] std::string::FromUtf8Error),

    /// 400
    #[error("{0}")]
    Hyper(#[from] hyper::Error),

    #[cfg(feature = "json")]
    /// 400
    #[error("json: {0}")]
    Json(#[from] serde_json::Error),

    #[cfg(any(feature = "form", feature = "query"))]
    /// 400
    #[error("url decode: {0}")]
    UrlDecode(#[from] serde_urlencoded::de::Error),

    /// 411
    #[error("content-length is required")]
    LengthRequired,

    /// 413
    #[error("payload is too large")]
    TooLarge,

    /// 415
    #[error("unsupported media type, `{}` is required", .0.to_string())]
    UnsupportedMediaType(mime::Mime),

    /// 500
    #[error("missing data type `{0}`")]
    Data(&'static str),
}

impl IntoResponse for PayloadError {
    fn into_response(self) -> Response {
        (
            match self {
                PayloadError::Read
                | PayloadError::Parse
                | PayloadError::MissingBoundary
                | PayloadError::Utf8(_)
                | PayloadError::Hyper(_) => StatusCode::BAD_REQUEST,
                #[cfg(feature = "json")]
                PayloadError::Json(_) => StatusCode::BAD_REQUEST,
                #[cfg(any(feature = "form", feature = "query"))]
                PayloadError::UrlDecode(_) => StatusCode::BAD_REQUEST,
                PayloadError::LengthRequired => StatusCode::LENGTH_REQUIRED,
                PayloadError::TooLarge => StatusCode::PAYLOAD_TOO_LARGE,
                PayloadError::UnsupportedMediaType(_) => StatusCode::UNSUPPORTED_MEDIA_TYPE,
                PayloadError::Data(_) => StatusCode::INTERNAL_SERVER_ERROR,
            },
            self.to_string(),
        )
            .into_response()
    }
}

impl From<PayloadError> for Error {
    fn from(e: PayloadError) -> Self {
        e.into_error()
    }
}

pub trait Payload {
    const NAME: &'static str = "payload";

    /// 1MB
    const LIMIT: u64 = 1024 * 1024;

    fn mime() -> mime::Mime;

    fn detect(m: &mime::Mime) -> bool;

    #[inline]
    fn limit(limit: Option<u64>) -> u64 {
        limit.unwrap_or(Self::LIMIT)
    }

    /// Checks `Content-Type` & `Content-Length`
    #[inline]
    fn check_header(
        m: Option<mime::Mime>,
        len: Option<u64>,
        limit: Option<u64>,
    ) -> Result<mime::Mime, PayloadError> {
        let m = m.ok_or_else(|| PayloadError::UnsupportedMediaType(Self::mime()))?;

        if !Self::detect(&m) {
            return Err(PayloadError::UnsupportedMediaType(Self::mime()));
        }

        if len == None {
            return Err(PayloadError::LengthRequired);
        }

        if matches!(len, Some(len) if len  > Self::limit(limit)) {
            return Err(PayloadError::TooLarge);
        }

        Ok(m)
    }
}