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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use std::fmt;
use std::mem;
use bytecodec::{ByteCount, Decode, DecodeExt, Encode, Eos, ErrorKind, ExactBytesEncode, Result};
use bytecodec::combinator::Length;
use trackable::error::ErrorKindExt;

use {Header, HeaderField, HeaderMut};
use chunked_body::{ChunkedBodyDecoder, ChunkedBodyEncoder};

/// `BodyDecode` is used for representing HTTP body decoders.
pub trait BodyDecode: Decode {
    /// This method is called before starting to decode a HTTP body.
    ///
    /// The default implementation does nothing.
    #[allow(unused_variables)]
    fn initialize(&mut self, header: &Header) -> Result<()> {
        Ok(())
    }
}

/// `BodyEncode` is used for representing HTTP body encoders.
pub trait BodyEncode: Encode {
    /// This method is called before starting to encode a HTTP body.
    ///
    /// It is used for adjusting HTTP header by using the encoder specific information.
    ///
    /// The default implementation does nothing.
    #[allow(unused_variables)]
    fn update_header(&self, header: &mut HeaderMut) -> Result<()> {
        Ok(())
    }
}

/// A body decoder that consumes no bytes.
///
/// This does consume no bytes and immediately returns `()` as the decoded item.
///
/// It is mainly intended to be used for decoding HEAD responses.
/// It can also be used to prefetch the HTTP header before decoding the body of
/// a HTTP message.
#[derive(Debug, Default)]
pub struct NoBodyDecoder;
impl Decode for NoBodyDecoder {
    type Item = ();

    fn decode(&mut self, _buf: &[u8], _eos: Eos) -> Result<(usize, Option<Self::Item>)> {
        Ok((0, Some(())))
    }

    fn has_terminated(&self) -> bool {
        false
    }

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(0)
    }
}
impl BodyDecode for NoBodyDecoder {}

/// A body encoder that produces no bytes.
///
/// `NoBodyDecoder` updates HTTP header ordinally but
/// discards all data produced by the inner encoder.
///
/// It is mainly intended to be used for encoding HEAD responses.
#[derive(Debug, Default)]
pub struct NoBodyEncoder<E>(E);
impl<E: BodyEncode> NoBodyEncoder<E> {
    /// Makes a new `NoBodyEncoder` instance.
    pub fn new(inner: E) -> Self {
        NoBodyEncoder(inner)
    }

    /// Returns a reference to a inner body encoder.
    pub fn inner_ref(&self) -> &E {
        &self.0
    }

    /// Returns a mutable reference to a inner body encoder.
    pub fn inner_mut(&mut self) -> &mut E {
        &mut self.0
    }

    /// Takes ownership of `NoBodyEncoder` and returns the inner body encoder.
    pub fn into_inner(self) -> E {
        self.0
    }
}
impl<E: BodyEncode> Encode for NoBodyEncoder<E> {
    type Item = E::Item;

    fn encode(&mut self, _buf: &mut [u8], _eos: Eos) -> Result<usize> {
        let mut temp_buf = [0; 1024];
        while !self.0.is_idle() {
            track!(self.0.encode(&mut temp_buf, Eos::new(false)))?;
        }
        Ok(0)
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        track!(self.0.start_encoding(item))
    }

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

    fn requiring_bytes(&self) -> ByteCount {
        ByteCount::Finite(0)
    }
}
impl<E: BodyEncode> ExactBytesEncode for NoBodyEncoder<E> {
    fn exact_requiring_bytes(&self) -> u64 {
        0
    }
}
impl<E: BodyEncode> BodyEncode for NoBodyEncoder<E> {
    fn update_header(&self, header: &mut HeaderMut) -> Result<()> {
        self.0.update_header(header)
    }
}

/// Basic HTTP body decoder.
///
/// It is typically used for making a body decoder from a `Decode` implementor.
#[derive(Debug, Default)]
pub struct BodyDecoder<D: Decode>(BodyDecoderInner<D>);
impl<D: Decode> BodyDecoder<D> {
    /// Makes a new `BodyDecoder` instance.
    pub fn new(inner: D) -> Self {
        BodyDecoder(BodyDecoderInner::WithoutLength(inner))
    }
}
impl<D: Decode> Decode for BodyDecoder<D> {
    type Item = D::Item;

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

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

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}
impl<D: Decode> BodyDecode for BodyDecoder<D> {
    fn initialize(&mut self, header: &Header) -> Result<()> {
        self.0.initialize(header)
    }
}

enum BodyDecoderInner<D: Decode> {
    Chunked(ChunkedBodyDecoder<D>),
    WithLength(Length<D>),
    WithoutLength(D),
    None,
}
impl<D: Decode> BodyDecoderInner<D> {
    fn update_inner<F>(&mut self, f: F) -> Result<()>
    where
        F: FnOnce(D) -> Result<Self>,
    {
        let inner = match mem::replace(self, BodyDecoderInner::None) {
            BodyDecoderInner::Chunked(x) => x.into_inner(),
            BodyDecoderInner::WithLength(x) => x.into_inner(),
            BodyDecoderInner::WithoutLength(x) => x,
            BodyDecoderInner::None => return Ok(()),
        };
        *self = f(inner)?;
        Ok(())
    }
}
impl<D: Decode> Decode for BodyDecoderInner<D> {
    type Item = D::Item;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<(usize, Option<Self::Item>)> {
        match *self {
            BodyDecoderInner::Chunked(ref mut d) => track!(d.decode(buf, eos)),
            BodyDecoderInner::WithLength(ref mut d) => track!(d.decode(buf, eos)),
            BodyDecoderInner::WithoutLength(ref mut d) => track!(d.decode(buf, eos)),
            BodyDecoderInner::None => track_panic!(ErrorKind::DecoderTerminated),
        }
    }

    fn has_terminated(&self) -> bool {
        match *self {
            BodyDecoderInner::Chunked(ref d) => d.has_terminated(),
            BodyDecoderInner::WithLength(ref d) => d.has_terminated(),
            BodyDecoderInner::WithoutLength(ref d) => d.has_terminated(),
            BodyDecoderInner::None => true,
        }
    }

    fn requiring_bytes(&self) -> ByteCount {
        match *self {
            BodyDecoderInner::Chunked(ref d) => d.requiring_bytes(),
            BodyDecoderInner::WithLength(ref d) => d.requiring_bytes(),
            BodyDecoderInner::WithoutLength(ref d) => d.requiring_bytes(),
            BodyDecoderInner::None => ByteCount::Finite(0),
        }
    }
}
impl<D: Decode + Default> Default for BodyDecoderInner<D> {
    fn default() -> Self {
        BodyDecoderInner::WithoutLength(D::default())
    }
}
impl<D: Decode> BodyDecode for BodyDecoderInner<D> {
    fn initialize(&mut self, header: &Header) -> Result<()> {
        self.update_inner(|inner| {
            for field in header.fields() {
                if field.name().eq_ignore_ascii_case("content-length") {
                    let size: u64 = track!(
                        field
                            .value()
                            .parse()
                            .map_err(|e| ErrorKind::InvalidInput.cause(e))
                    )?;
                    return Ok(BodyDecoderInner::WithLength(inner.length(size)));
                } else if field.name().eq_ignore_ascii_case("transfer-encoding") {
                    track_assert_eq!(field.value(), "chunked", ErrorKind::Other);
                    return Ok(BodyDecoderInner::Chunked(ChunkedBodyDecoder::new(inner)));
                }
            }
            Ok(BodyDecoderInner::WithoutLength(inner))
        })
    }
}
impl<D: Decode> fmt::Debug for BodyDecoderInner<D> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            BodyDecoderInner::Chunked(_) => write!(f, "Chunked(_)"),
            BodyDecoderInner::WithLength(_) => write!(f, "WithLength(_)"),
            BodyDecoderInner::WithoutLength(_) => write!(f, "WithoutLength(_)"),
            BodyDecoderInner::None => write!(f, "None"),
        }
    }
}

/// Basic HTTP body encoder.
///
/// It is typically used for making a body encoder from a `Encode` implementor.
///
/// If `E::requiring_bytes()` returns `ByteCount::Unknown`,
/// the chunked body transfer encoding will be used.
#[derive(Debug, Default)]
pub struct BodyEncoder<E>(BodyEncoderInner<E>);
impl<E> BodyEncoder<E> {
    /// Makes a new `BodyEncoder` instance.
    pub fn new(inner: E) -> Self {
        BodyEncoder(BodyEncoderInner::NotStarted(inner))
    }
}
impl<E: Encode> Encode for BodyEncoder<E> {
    type Item = E::Item;

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

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        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: Encode> BodyEncode for BodyEncoder<E> {
    fn update_header(&self, header: &mut HeaderMut) -> Result<()> {
        match self.0 {
            BodyEncoderInner::NotStarted(_) | BodyEncoderInner::None => {
                track_panic!(ErrorKind::Other)
            }
            BodyEncoderInner::WithLength(ref x) => {
                let n = track_assert_some!(x.requiring_bytes().to_u64(), ErrorKind::Other);
                header.add_field(HeaderField::new("content-length", &n.to_string())?);
                Ok(())
            }
            BodyEncoderInner::Chunked(ref x) => x.update_header(header),
        }
    }
}

#[derive(Debug)]
enum BodyEncoderInner<E> {
    NotStarted(E),
    WithLength(E),
    Chunked(ChunkedBodyEncoder<E>),
    None,
}
impl<E: Encode> Encode for BodyEncoderInner<E> {
    type Item = E::Item;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        match *self {
            BodyEncoderInner::NotStarted(_) => Ok(0),
            BodyEncoderInner::None => track_panic!(ErrorKind::Other),
            BodyEncoderInner::WithLength(ref mut x) => track!(x.encode(buf, eos)),
            BodyEncoderInner::Chunked(ref mut x) => track!(x.encode(buf, eos)),
        }
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        let mut inner =
            if let BodyEncoderInner::NotStarted(x) = mem::replace(self, BodyEncoderInner::None) {
                x
            } else {
                track_panic!(ErrorKind::EncoderFull);
            };
        track!(inner.start_encoding(item))?;
        let this = match inner.requiring_bytes() {
            ByteCount::Infinite => track_panic!(ErrorKind::Other),
            ByteCount::Unknown => BodyEncoderInner::Chunked(ChunkedBodyEncoder(inner)),
            ByteCount::Finite(_) => BodyEncoderInner::WithLength(inner),
        };
        *self = this;
        Ok(())
    }

    fn is_idle(&self) -> bool {
        match *self {
            BodyEncoderInner::NotStarted(_) | BodyEncoderInner::None => true,
            BodyEncoderInner::WithLength(ref x) => x.is_idle(),
            BodyEncoderInner::Chunked(ref x) => x.is_idle(),
        }
    }

    fn requiring_bytes(&self) -> ByteCount {
        match *self {
            BodyEncoderInner::NotStarted(_) => ByteCount::Finite(0),
            BodyEncoderInner::WithLength(ref x) => x.requiring_bytes(),
            BodyEncoderInner::Chunked(ref x) => x.requiring_bytes(),
            BodyEncoderInner::None => ByteCount::Finite(0),
        }
    }
}
impl<E: Default> Default for BodyEncoderInner<E> {
    fn default() -> Self {
        BodyEncoderInner::NotStarted(E::default())
    }
}