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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Gemini Responses
//!
//! Gemini reponses consist of a `Header` and an optional response body
//! consisting of a stream of bytes.

use std::str;

use crate::{
    gemtext::Builder,
    header::Header,
    status::{Category, Status},
};

/// Gemini responses
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Response {
    /// Response header
    pub header: Header,
    body: Option<Vec<u8>>,
}

impl Response {
    /// Return the status of this `Response`.
    pub fn status(&self) -> Status {
        self.header.status
    }

    /// Return whether or not this `Response` has a body.
    pub fn has_body(&self) -> bool {
        self.body.is_some()
    }

    /// Construct a new response.
    ///
    /// This function maintains the invariant that only successful responses
    /// may include response bodies.
    pub fn new(header: Header, body: Option<Vec<u8>>) -> Option<Self> {
        match (header.status.category(), &body) {
            (Category::Success, Some(_)) | (_, None) => Some(Response { header, body }),
            _ => None,
        }
    }

    // TODO: add some built-in mime-types for more infallible constructors?

    /// Construct a new response with a body.
    pub fn with_body(mime_type: String, body: Vec<u8>) -> Option<Self> {
        let header = Header::new(Status::SUCCESS, mime_type)?;
        let body = Some(body);
        Some(Response { header, body })
    }

    /// Construct a new response with a gemtext body.
    pub fn with_gemtext(builder: Builder) -> Self {
        let header = Header::gemtext();
        let body = Some(builder.build().bytes().collect());
        Self::new_unchecked(header, body)
    }

    /// Construct a new response with no body.
    pub fn without_body(header: Header) -> Option<Self> {
        Self::new(header, None)
    }

    /// Construct a new response without maintaining invariants.
    ///
    /// This constructor may produce a `Response' which is invalid with respect
    /// to the Gemini spec. Users of this function should be careful to check
    /// that `header` has `Category::Success`.
    pub fn new_unchecked(header: Header, body: Option<Vec<u8>>) -> Self {
        Response { header, body }
    }

    /// Return the response body as a string, if present and valid utf-8.
    ///
    /// If you wish to allow invalid utf-8, call `body_bytes` and use the
    /// appropriate `String` function.
    pub fn body_text(&self) -> Option<&str> {
        let bytes = self.body.as_ref()?;
        str::from_utf8(bytes).ok()
    }

    /// Return the response body as raw bytes, if present.
    pub fn body_bytes(&self) -> Option<&[u8]> {
        self.body.as_deref()
    }
}

/// Parser
#[cfg(feature = "parsers")]
pub mod parse {
    use nom::{
        combinator::{map_opt, rest},
        error::context,
        sequence::pair,
        IResult,
    };

    use super::*;

    use crate::header::parse::header;

    /// A `nom` parser for a response. Any bytes after the CRLF that separates
    /// the header from the response body will be considered part of the body.
    pub fn response(input: &[u8]) -> IResult<&[u8], Response> {
        context(
            "response",
            map_opt(pair(header, rest), |t| {
                if t.1.is_empty() {
                    Response::new(t.0, None)
                } else {
                    let v = Vec::from(t.1);
                    Response::new(t.0, Some(v))
                }
            }),
        )(input)
    }

    #[cfg(test)]
    mod test {
        use super::*;

        #[test]
        fn test_response() {
            let bytes = b"20 text/gemini\r\n=> gemini://foo.bar.baz/ wow";
            let res = response(bytes).unwrap().1;
            assert_eq!(res.body_text().unwrap(), "=> gemini://foo.bar.baz/ wow")
        }

        #[test]
        fn test_response_no_body() {
            let bytes = b"60 owwwwwwww!\r\ni shouldn't be here";
            assert!(response(bytes).is_err());

            let bytes = b"61 this is fine\r\n";
            assert!(response(bytes).is_ok());
        }
    }
}