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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use bytecodec::tuple::TupleDecoder;
use bytecodec::{ByteCount, Decode, Encode, Eos, Result, SizedEncode};
use std::fmt;
use std::str;

use body::{BodyDecode, BodyEncode};
use header::{Header, HeaderFieldPosition, HeaderMut};
use message::{Message, MessageDecoder, MessageEncoder};
use method::{Method, MethodDecoder};
use options::DecodeOptions;
use request_target::{RequestTarget, RequestTargetDecoder};
use util::CrlfDecoder;
use version::{HttpVersion, HttpVersionDecoder};

/// HTTP request message.
#[derive(Debug)]
pub struct Request<T> {
    buf: Vec<u8>,
    request_line: RequestLine,
    header: Vec<HeaderFieldPosition>,
    body: T,
}
impl<T> Request<T> {
    /// Makes a new `Request` instance with the given request-line components and body.
    pub fn new(method: Method, target: RequestTarget, version: HttpVersion, body: T) -> Self {
        let mut buf = Vec::new();
        buf.extend_from_slice(method.as_str().as_bytes());
        buf.push(b' ');
        buf.extend_from_slice(target.as_str().as_bytes());
        buf.push(b' ');
        buf.extend_from_slice(version.as_str().as_bytes());
        buf.extend_from_slice(b"\r\n");

        let request_line = RequestLine {
            method_size: method.as_str().len(),
            request_target_size: target.as_str().len(),
            http_version: version,
        };

        Request {
            buf,
            request_line,
            header: Vec::new(),
            body,
        }
    }

    /// Returns the method of the request.
    pub fn method(&self) -> Method {
        unsafe {
            Method::new_unchecked(str::from_utf8_unchecked(
                &self.buf[..self.request_line.method_size],
            ))
        }
    }

    /// Returns the target of the request.
    pub fn request_target(&self) -> RequestTarget {
        let start = self.request_line.method_size + 1;
        let end = start + self.request_line.request_target_size;
        unsafe { RequestTarget::new_unchecked(str::from_utf8_unchecked(&self.buf[start..end])) }
    }

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

    /// Returns the header of the request.
    pub fn header(&self) -> Header {
        Header::new(&self.buf, &self.header)
    }

    /// Returns the mutable header of the request.
    pub fn header_mut(&mut self) -> HeaderMut {
        HeaderMut::new(&mut self.buf, &mut self.header)
    }

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

    /// Returns a mutable reference to the body of the request.
    pub fn body_mut(&mut self) -> &mut T {
        &mut self.body
    }

    /// Takes ownership of the request, and returns its body.
    pub fn into_body(self) -> T {
        self.body
    }

    /// Splits the head part and the body part of the request.
    pub fn take_body(self) -> (Request<()>, T) {
        let req = Request {
            buf: self.buf,
            request_line: self.request_line,
            header: self.header,
            body: (),
        };
        (req, self.body)
    }

    /// Converts the body of the request to `U` by using the given function.
    pub fn map_body<U, F>(self, f: F) -> Request<U>
    where
        F: FnOnce(T) -> U,
    {
        let body = f(self.body);
        Request {
            buf: self.buf,
            request_line: self.request_line,
            header: self.header,
            body,
        }
    }
}
impl<T: fmt::Display> fmt::Display for Request<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(
            f,
            "{} {} {}\r",
            self.method(),
            self.request_target(),
            self.http_version()
        )?;
        write!(f, "{}", self.header())?;
        write!(f, "{}", self.body)?;
        Ok(())
    }
}

/// HTTP request decoder.
#[derive(Debug)]
pub struct RequestDecoder<D>(MessageDecoder<RequestLineDecoder, D>);
impl<D: BodyDecode> RequestDecoder<D> {
    /// Make a new `RequestDecoder` instance.
    pub fn new(body_decoder: D) -> Self {
        Self::with_options(body_decoder, DecodeOptions::default())
    }

    /// Make a new `RequestDecoder` instance with the given options.
    pub fn with_options(body_decoder: D, options: DecodeOptions) -> Self {
        let inner = MessageDecoder::new(RequestLineDecoder::default(), body_decoder, options);
        RequestDecoder(inner)
    }
}
impl<D: BodyDecode> Decode for RequestDecoder<D> {
    type Item = Request<D::Item>;

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

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let m = track!(self.0.finish_decoding())?;
        Ok(Request {
            buf: m.buf,
            request_line: m.start_line,
            header: m.header,
            body: m.body,
        })
    }

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

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }
}
impl<D: Default + BodyDecode> Default for RequestDecoder<D> {
    fn default() -> Self {
        Self::new(D::default())
    }
}

#[derive(Debug)]
struct RequestLine {
    method_size: usize,
    request_target_size: usize,
    http_version: HttpVersion,
}

#[derive(Debug, Default)]
struct RequestLineDecoder(
    TupleDecoder<(
        MethodDecoder,
        RequestTargetDecoder,
        HttpVersionDecoder,
        CrlfDecoder,
    )>,
);
impl Decode for RequestLineDecoder {
    type Item = RequestLine;

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

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        let t = track!(self.0.finish_decoding())?;
        Ok(RequestLine {
            method_size: t.0,
            request_target_size: t.1,
            http_version: t.2,
        })
    }

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

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

/// HTTP request encoder.
#[derive(Debug, Default)]
pub struct RequestEncoder<E>(MessageEncoder<E>);
impl<E: BodyEncode> RequestEncoder<E> {
    /// Makes a new `RequestEncoder` instance.
    pub fn new(body_encoder: E) -> Self {
        RequestEncoder(MessageEncoder::new(body_encoder))
    }
}
impl<E: BodyEncode> Encode for RequestEncoder<E> {
    type Item = Request<E::Item>;

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

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        let item = Message {
            buf: item.buf,
            start_line: (),
            header: item.header,
            body: item.body,
        };
        track!(self.0.start_encoding(item))
    }

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

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}
impl<E: SizedEncode + BodyEncode> SizedEncode for RequestEncoder<E> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.0.exact_requiring_bytes()
    }
}

#[cfg(test)]
mod test {
    use bytecodec::EncodeExt;
    use bytecodec::bytes::{BytesEncoder, RemainingBytesDecoder, Utf8Decoder};
    use bytecodec::io::{IoDecodeExt, IoEncodeExt};
    use std::str;

    use super::*;
    use {BodyDecoder, BodyEncoder, HttpVersion, Method, RequestTarget};

    #[test]
    fn request_encoder_works() {
        let request = Request::new(
            Method::new("GET").unwrap(),
            RequestTarget::new("/foo").unwrap(),
            HttpVersion::V1_1,
            b"barbaz",
        );
        let mut encoder =
            RequestEncoder::<BodyEncoder<BytesEncoder<_>>>::with_item(request).unwrap();

        let mut buf = Vec::new();
        track_try_unwrap!(encoder.encode_all(&mut buf));
        assert_eq!(
            str::from_utf8(&buf).ok(),
            Some("GET /foo HTTP/1.1\r\nContent-Length: 6\r\n\r\nbarbaz")
        );
    }

    #[test]
    fn request_decoder_works() {
        let mut decoder =
            RequestDecoder::<BodyDecoder<Utf8Decoder<RemainingBytesDecoder>>>::default();
        let item = track_try_unwrap!(
            decoder.decode_exact(b"GET /foo HTTP/1.1\r\nContent-Length: 6\r\n\r\nbarbaz".as_ref())
        );
        assert_eq!(
            item.to_string(),
            "GET /foo HTTP/1.1\r\nContent-Length: 6\r\n\r\nbarbaz"
        );
        assert_eq!(item.method().as_str(), "GET");
        assert_eq!(item.request_target().as_str(), "/foo");
        assert_eq!(item.http_version(), HttpVersion::V1_1);
        assert_eq!(
            item.header()
                .fields()
                .map(|f| (f.name().to_owned(), f.value().to_owned()))
                .collect::<Vec<_>>(),
            vec![("Content-Length".to_owned(), "6".to_owned())]
        );
        assert_eq!(item.body(), "barbaz");
    }
}