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
use std::convert::TryInto;
use std::fs;
use std::io::{copy, Result as IoResult, Seek, SeekFrom, Write};

/// The kinds of request bodies currently supported by this crate.
#[derive(Debug, Clone, Copy)]
pub enum BodyKind {
    /// An empty request body
    Empty,
    /// A request body with a known length
    KnownLength(u64),
    /// A request body that is transferred using chunked encoding
    Chunked,
}

/// A generic rewindable request body
pub trait Body {
    /// Determine the kind of the request body
    fn kind(&mut self) -> IoResult<BodyKind>;

    /// Write out the request body into the given writer
    ///
    /// This method can be called multiple times if a request is redirected.
    fn write<W: Write>(&mut self, writer: W) -> IoResult<()>;

    /// Gets the content type this body is tied to if it has one.
    fn content_type(&mut self) -> IoResult<Option<String>> {
        Ok(None)
    }
}

/// An empty request body
#[derive(Debug, Clone, Copy)]
pub struct Empty;

impl Body for Empty {
    fn kind(&mut self) -> IoResult<BodyKind> {
        Ok(BodyKind::Empty)
    }

    fn write<W: Write>(&mut self, _writer: W) -> IoResult<()> {
        Ok(())
    }
}

/// A request body containing UTF-8-encoded text
#[derive(Debug, Clone)]
pub struct Text<B>(pub B);

impl<B: AsRef<str>> Body for Text<B> {
    fn kind(&mut self) -> IoResult<BodyKind> {
        let len = self.0.as_ref().len().try_into().unwrap();
        Ok(BodyKind::KnownLength(len))
    }

    fn write<W: Write>(&mut self, mut writer: W) -> IoResult<()> {
        writer.write_all(self.0.as_ref().as_bytes())
    }
}

/// A request body containing binary data
#[derive(Debug, Clone)]
pub struct Bytes<B>(pub B);

impl<B: AsRef<[u8]>> Body for Bytes<B> {
    fn kind(&mut self) -> IoResult<BodyKind> {
        let len = self.0.as_ref().len().try_into().unwrap();
        Ok(BodyKind::KnownLength(len))
    }

    fn write<W: Write>(&mut self, mut writer: W) -> IoResult<()> {
        writer.write_all(self.0.as_ref())
    }
}

/// A request body backed by a local file
#[derive(Debug)]
pub struct File(pub fs::File);

impl Body for File {
    fn kind(&mut self) -> IoResult<BodyKind> {
        let len = self.0.seek(SeekFrom::End(0))?;
        Ok(BodyKind::KnownLength(len))
    }

    fn write<W: Write>(&mut self, mut writer: W) -> IoResult<()> {
        self.0.rewind()?;
        copy(&mut self.0, &mut writer)?;
        Ok(())
    }
}

pub(crate) struct ChunkedWriter<W>(pub W);

impl<W: Write> ChunkedWriter<W> {
    pub fn close(mut self) -> IoResult<()> {
        self.0.write_all(b"0\r\n\r\n")
    }
}

impl<W: Write> Write for ChunkedWriter<W> {
    fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
        write!(self.0, "{:x}\r\n", buf.len())?;
        self.0.write_all(buf)?;
        write!(self.0, "\r\n")?;
        Ok(buf.len())
    }

    fn flush(&mut self) -> IoResult<()> {
        self.0.flush()
    }
}

#[cfg(feature = "json")]
mod json {
    use super::*;

    use std::io::BufWriter;

    use serde::ser::Serialize;
    use serde_json::ser::to_writer;

    /// A request body for streaming out JSON
    #[derive(Debug, Clone)]
    pub struct Json<B>(pub B);

    impl<B: Serialize> Body for Json<B> {
        fn kind(&mut self) -> IoResult<BodyKind> {
            Ok(BodyKind::Chunked)
        }

        fn write<W: Write>(&mut self, writer: W) -> IoResult<()> {
            let mut writer = BufWriter::new(writer);
            to_writer(&mut writer, &self.0)?;
            writer.flush()?;
            Ok(())
        }
    }
}

#[cfg(feature = "json")]
pub use json::Json;