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
use std::ffi::OsStr;
use std::fmt::{Display, Error as FmtError, Formatter};
use std::fs;
use std::io::Read;
use std::str::from_utf8;

/// Used when call is in send request state.
#[derive(Debug)]
pub enum SendState {
    /// Unrecoverable error has occured and call is finished.
    Error(crate::Error),
    /// How many bytes of body have just been sent.
    SentBody(usize),
    /// Waiting for body to be provided for sending.
    WaitReqBody,
    /// Call has switched to receiving state.
    Receiving,
    /// Request is done, body has been returned or
    /// there is no response body.
    Done,
    /// Nothing yet to return.
    Wait,
}

#[derive(Default, Debug)]
pub struct Response {
    pub(crate) hdrs: Vec<u8>,
    pub status: u16,
    pub(crate) ws: bool,
}
impl Response {
    pub(crate) fn new() -> Response {
        Response {
            ..Default::default()
        }
    }

    pub fn headers(&self) -> Headers {
        let mut raw = [::httparse::EMPTY_HEADER; 32];
        let mut out = Headers::new();
        {
            let mut presp = ::httparse::Response::new(&mut raw);
            let _ = presp.parse(&self.hdrs);
        }
        let mut pos = 0;
        for i in 0..raw.len() {
            if raw[i].name.len() == 0 {
                break;
            }
            if let Ok(v) = from_utf8(raw[i].value) {
                out.headers[pos] = Header::new(raw[i].name, v);
                pos += 1;
                out.len = pos;
            }
        }
        out
    }
}

/// A single header
#[derive(Default, Copy, Clone)]
pub struct Header<'a> {
    pub name: &'a str,
    pub value: &'a str,
}
impl<'a> Header<'a> {
    fn new(n: &'a str, v: &'a str) -> Header<'a> {
        Header { name: n, value: v }
    }

    /// Case insensitive header name comparison
    pub fn is(&self, v: &str) -> bool {
        self.name.eq_ignore_ascii_case(v)
    }
}
impl<'a> Display for Header<'a> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        write!(f, "[ {}: {} ]", self.name, self.value)
    }
}

/// Iterator over headers in response
#[derive(Default, Copy, Clone)]
pub struct Headers<'a> {
    headers: [Header<'a>; 32],
    len: usize,
    next: usize,
}
impl<'a> Headers<'a> {
    fn new() -> Headers<'a> {
        Default::default()
    }
}

impl<'a> Display for Headers<'a> {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        for h in 0..self.len {
            self.headers[h].fmt(f)?;
        }
        Ok(())
    }
}

impl<'a> Iterator for Headers<'a> {
    type Item = Header<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.next == self.len {
            return None;
        }
        self.next += 1;
        Some(self.headers[self.next - 1])
    }
}

/// Top level configuration for mio_http.
#[derive(Default, Clone)]
pub struct HttpcCfg {
    /// Extra root certificates in der format.
    pub der_ca: Vec<Vec<u8>>,
    /// Extra root certificates in pem format.
    pub pem_ca: Vec<Vec<u8>>,
    /// Default: 8
    ///
    /// Max 8K buffers to keep cached for subsequent requests.
    /// Every request requires 2.
    pub cache_buffers: usize,
    /// Set DNS servers if library can not easily get them from system (like Android).
    /// macOS, iOS and unix with /etc/resolv.conf are supported ATM.
    /// If none provided and library can't get them, google DNS servers (8.8.8.8:53, 8.8.4.4:53) will be used.
    pub dns_servers: Vec<::std::net::SocketAddr>,
    /// SSL Pinning. every element is (hostname, vec![pin1,pin2,..])
    /// A pin is formatted as: sha256/base64(subjectPublicKeyInfo)
    /// mio_httpc will check if public key of server matches any of the pins before
    /// sending the HTTP request.
    pub pins: Vec<(String, Vec<String>)>,
}

impl HttpcCfg {
    pub fn new() -> HttpcCfg {
        HttpcCfg {
            cache_buffers: 8,
            ..Default::default()
        }
    }

    /// Will read pem files (extensions .crt or .pem) from path.
    /// Path can be to file or folder.
    pub fn certs_from_path(path: &str) -> ::std::io::Result<HttpcCfg> {
        let mut cfg = HttpcCfg::new();
        let certs = [OsStr::new("crt"), OsStr::new("pem")];
        let metadata = fs::metadata(path)?;
        if metadata.is_file() {
            let mut file = fs::File::open(path)?;
            let mut contents = Vec::new();
            file.read_to_end(&mut contents)?;
            cfg.pem_ca.push(contents);
            return Ok(cfg);
        }
        for de in fs::read_dir(path)? {
            if de.is_err() {
                continue;
            }
            let de = de.unwrap();
            match de.path().extension() {
                Some(ex) if certs.contains(&ex) => {
                    let meta = fs::metadata(de.path())?;
                    if meta.len() < 1024 * 8 {
                        let mut file = fs::File::open(de.path())?;
                        let mut contents = Vec::new();
                        file.read_to_end(&mut contents)?;
                        cfg.pem_ca.push(contents);
                    }
                }
                _ => {}
            }
        }
        Ok(cfg)
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ResponseBody {
    Sized(usize),
    Streamed,
}
impl ::std::fmt::Display for ResponseBody {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        match *self {
            ResponseBody::Sized(sz) => write!(f, "ResponseBody::Sized({})", sz),
            ResponseBody::Streamed => write!(f, "ResponseBody::Streamed"),
        }
    }
}

impl ResponseBody {
    pub fn is_empty(&self) -> bool {
        match *self {
            ResponseBody::Sized(n) if n == 0 => true,
            _ => false,
        }
    }
}

/// Used when call is in receive response state.
#[derive(Debug)]
pub enum RecvState {
    /// Unrecoverable error has occured and call is finished.
    Error(crate::Error),
    /// HTTP Response and response body size.
    /// If there is a body it will follow, otherwise call is done.
    Response(Response, ResponseBody),
    /// How many bytes were received.
    ReceivedBody(usize),
    /// Request is done with body.
    DoneWithBody(Vec<u8>),
    /// We are not done sending request yet. State may switch back to sending
    /// if we are following redirects or need to send request again due to digest auth.
    Sending,
    /// Request is done, body has been returned or
    /// there is no response body.
    Done,
    /// Nothing yet to return.
    Wait,
}

/// Call structure.
#[derive(Debug, PartialEq)] // much fewer derives then ref on purpose. We want a single instance.
pub struct Call {
    id: u64,
    // two connections possible, we try connecting to
    // ipv4 and ipv6 resolved IP. First one to establish connection wins.
    con1: usize,
    con2: usize,
    pub(crate) fixed: bool,
} //(u64, usize);

impl Call {
    /// Get a CallRef that matches this call.
    pub fn get_ref(&self) -> CallRef {
        CallRef(self.id)
    }

    pub fn simple(self) -> SimpleCall {
        SimpleCall::from(self)
    }

    /// Is CallRef for this call.
    pub fn is_ref(&self, r: CallRef) -> bool {
        self.id == r.0
    }

    pub(crate) fn remove_con(&mut self, conid: usize) {
        if conid == self.con1 {
            self.con1 = usize::max_value();
        } else if conid == self.con2 {
            self.con2 = usize::max_value();
        }
    }
    pub(crate) fn id(&self) -> u64 {
        self.id
    }
    // pub(crate) fn set_con1(&mut self, con: usize) {
    //     self.con1 = con;
    // }
    // pub(crate) fn set_con2(&mut self, con: usize) {
    //     self.con2 = con;
    // }
    pub(crate) fn new(id: u64, con1: usize, con2: usize, fixed: bool) -> Call {
        Call {
            id,
            con1,
            con2,
            fixed,
        }
    }
    pub(crate) fn con(&self) -> usize {
        if self.con1 == usize::max_value() {
            self.con2
        } else {
            self.con1
        }
    }
    pub(crate) fn cons(&self) -> [usize; 2] {
        [self.con1, self.con2]
    }

    // keep this functionality internal to lib
    pub(crate) fn clone(&self) -> Call {
        Call {
            id: self.id,
            con1: self.con1,
            con2: self.con2,
            fixed: self.fixed,
        }
    }

    pub(crate) fn empty() -> Call {
        Call::new(
            u64::max_value(),
            usize::max_value(),
            usize::max_value(),
            false,
        )
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.con1 == self.con2 && self.con1 == usize::max_value()
    }
    // pub(crate) fn call_id(&self) -> u16 {
    //     ((self.0 >> 16) & 0xFFFF) as u16
    // }
    // pub(crate) fn con_id(&self) -> u16 {
    //     (self.0 & 0xFFFF) as u16
    // }
    // Once call finished it gets invalidated.
    // This is a fail-safe so we can destroy Call structure
    // from Httpc on error or request finished.
    pub(crate) fn invalidate(&mut self) {
        // *self = Call::empty();
        self.con1 = usize::max_value();
        self.con2 = usize::max_value();
    }
}

/// Reference to call. Used for matching mio Token with call.
/// If you have lots of calls, you can use this as a key in a HashMap
/// (you probably want fnv HashMap).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CallRef(u64);
impl CallRef {
    pub(crate) fn new(call_id: u64) -> CallRef {
        CallRef(call_id)
    }
}

#[allow(unused_imports)]
mod websocket;
pub use self::websocket::*;

mod simple_call;
pub use self::simple_call::*;

mod builder;
pub use self::builder::*;