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
use std::str;
use crate::{
gemtext::Builder,
header::Header,
status::{Category, Status},
};
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct Response {
pub header: Header,
body: Option<Vec<u8>>,
}
impl Response {
pub fn status(&self) -> Status {
self.header.status
}
pub fn has_body(&self) -> bool {
self.body.is_some()
}
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,
}
}
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 })
}
pub fn with_gemtext(builder: Builder) -> Self {
let header = Header::gemtext();
let body = Some(builder.build().bytes().collect());
Self::new_unchecked(header, body)
}
pub fn without_body(header: Header) -> Option<Self> {
Self::new(header, None)
}
pub fn new_unchecked(header: Header, body: Option<Vec<u8>>) -> Self {
Response { header, body }
}
pub fn body_text(&self) -> Option<&str> {
let bytes = self.body.as_ref()?;
str::from_utf8(bytes).ok()
}
pub fn body_bytes(&self) -> Option<&[u8]> {
self.body.as_deref()
}
}
#[cfg(feature = "parsers")]
pub mod parse {
use nom::{
combinator::{map_opt, rest},
error::context,
sequence::pair,
IResult,
};
use super::*;
use crate::header::parse::header;
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());
}
}
}