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
//! Asynchronous HTTP client.
//!
//! ## Installation
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! async-http-client = "0.2"
//! ```
//! ## Example
//!
//! ```no-run
//! extern crate async_http_client;
//!
//! use async_http_client::prelude::*;
//! use async_http_client::{HttpRequest, HttpCodec};
//!
//! let req = HttpRequest::get("http://www.google.com").unwrap();
//! let mut core = Core::new().unwrap();
//! let addr = req.addr().unwrap();
//! let handle = core.handle();
//! let (res, framed) = core.run(TcpStream::connect(&addr, &handle).and_then(|connection| {
//!     let framed = connection.framed(HttpCodec::new());
//!     req.send(framed)
//! })).unwrap();
//! println!("got response {}", res.unwrap());
//! ```

pub extern crate futures;
pub extern crate tokio_core;

pub extern crate url;

#[macro_use]
extern crate nom;

use std::borrow::Cow;
use std::fmt;
use std::io::{self, Error, ErrorKind, Write};
use std::net::{SocketAddr, ToSocketAddrs};

use futures::{Future, Sink, Stream};

use tokio_core::io::{EasyBuf, Codec, Framed, Io, IoFuture};

/// Commonly needed reexports from futures and tokio-core.
pub mod prelude {
    pub use tokio_core::io::Io;
    pub use tokio_core::net::TcpStream;
    pub use tokio_core::reactor::Core;

    pub use futures::{Future, Sink, Stream, IntoFuture};
    pub use futures::future::{BoxFuture, empty, err, lazy, ok, result};
}

use url::{Url, ParseError};

use nom::IResult;

mod parser;
mod response;

pub use response::{HttpResponse, Header};

/// Representation of an HTTP request.
pub struct HttpRequest {
    url: Url,
    method: Method,
    headers: Vec<(Cow<'static, str>, Cow<'static, str>)>,
    body: Vec<u8>
}

/// Representation of an HTTP method.
pub enum Method {
    Get,
    Head,
    Post,
    Put,
    Delete,
    Connect,
    Options,
    Trace,
    Other(String)
}

impl fmt::Display for Method {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Method::*;
        match *self {
            Get => write!(f, "GET"),
            Head => write!(f, "HEAD"),
            Post => write!(f, "POST"),
            Put => write!(f, "PUT"),
            Delete => write!(f, "DELETE"),
            Connect => write!(f, "CONNECT"),
            Options => write!(f, "OPTIONS"),
            Trace => write!(f, "TRACE"),
            Other(ref other) => write!(f, "{}", other)
        }
    }
}

impl HttpRequest {
    /// Creates a new HTTP request.
    pub fn new<U: AsRef<str>>(method: Method, url: U) -> Result<HttpRequest, ParseError> {
        url.as_ref().parse().map(|url: Url| {
            use std::fmt::Write;

            let mut host = url.host_str().unwrap_or("").to_string();
            if let Some(port) = url.port() {
                write!(host, ":{}", port).unwrap();
            }

            HttpRequest {
                url: url,
                method: method,
                headers: vec![],
                body: vec![]
            }.header("Host", host)
        })
    }

    pub fn header<K: Into<Cow<'static, str>>, V: Into<Cow<'static, str>>>(mut self, name: K, value: V) -> HttpRequest {
        self.headers.push((name.into(), value.into()));
        self
    }

    pub fn get<U: AsRef<str>>(url: U) -> Result<HttpRequest, ParseError> {
        Self::new(Method::Get, url)
    }

    pub fn post<U: AsRef<str>, I: Into<Vec<u8>>>(url: U, body: I) -> Result<HttpRequest, ParseError> {
        let bytes = body.into();
        let mut req = Self::new(Method::Post, url)?.header("Content-Length", bytes.len().to_string());
        req.body = bytes;
        Ok(req)
    }

    pub fn addr(&self) -> Result<SocketAddr, Error> {
        let mut addrs = self.url.to_socket_addrs()?;
        addrs.next().ok_or(Error::new(ErrorKind::UnexpectedEof, "no address"))
    }

    /// Returns a future that, given a framed, will resolve to a tuple (response?, framed).
    pub fn send<T: 'static + Io + Send>(self, framed: Framed<T, HttpCodec>) -> IoFuture<(Option<HttpResponse>, Framed<T, HttpCodec>)> {
        framed.send(self).and_then(|framed| {
            framed.into_future().and_then(|(res, stream)| {
                Ok((res, stream))
            }).map_err(|(err, _stream)| err)
        }).boxed()
    }
}

impl fmt::Display for HttpRequest {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // request line
        write!(f, "{} {}", self.method, self.url.path())?;
        if let Some(query) = self.url.query() {
            write!(f, "?{}", query)?;
        }
        if let Some(fragment) = self.url.fragment() {
            write!(f, "#{}", fragment)?;
        }
        write!(f, " HTTP/1.1\r\n")?;

        // headers
        for &(ref name, ref value) in &self.headers {
            write!(f, "{}: {}\r\n", name, value)?;
        }
        write!(f, "\r\n")
    }
}

/// Codec that parses HTTP responses.
#[derive(Debug)]
pub struct HttpCodec {
    response: Option<HttpResponse>,
    bytes_left: usize
}

impl HttpCodec {
    /// Creates a new HTTP codec.
    pub fn new() -> HttpCodec {
        HttpCodec {
            response: None,
            bytes_left: 0
        }
    }

    fn decode_header(&mut self, buf: &mut EasyBuf) -> Result<Option<HttpResponse>, Error> {
        let (bytes_left, response) = match parser::response(buf.as_ref()) {
            IResult::Incomplete(_) => return Ok(None), // not enough data
            IResult::Error(e) => return Err(Error::new(ErrorKind::InvalidData, e)),
            IResult::Done(rest, response) => (rest.len(), response)
        };

        // eat parsed bytes
        let after_header = buf.len() - bytes_left;
        buf.drain_to(after_header);

        // no content
        if response.is_informational() || response.status() == 204 ||
            response.status() == 304 {
            assert!(bytes_left == 0);
            return Ok(Some(response));
        }

        // chunked
        if response.has("Transfer-Encoding", "chunked") {
            unimplemented!()
        }

        let length =
            if let Some(ref length) = response["Content-Length"] {
                Some(length.parse::<usize>().map_err(|e| Error::new(ErrorKind::InvalidData, e))?)
            } else {
                None
            };

        if let Some(length) = length {
            self.response = Some(response);
            self.bytes_left = length;
            return self.decode(buf);
        } else {
            // legacy HTTP/1.0 mode (close connection)
            unimplemented!()
        }
    }
}

impl Codec for HttpCodec {
    type In = HttpResponse;
    type Out = HttpRequest;

    fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<HttpResponse>, Error> {
        if self.response.is_none() {
            self.decode_header(buf)
        } else {
            let buf_len = buf.len();
            println!("{} bytes left to read, got {} bytes", self.bytes_left, buf_len);
            if buf_len > self.bytes_left {
                Err(Error::new(ErrorKind::InvalidData, "extraneous data"))
            } else {
                self.response.as_mut().map(|res| response::append(res, buf.drain_to(buf_len).as_slice()));
                if buf_len == self.bytes_left {
                    Ok(self.response.take())
                } else {
                    self.bytes_left -= buf_len;
                    Ok(None) // not enough data
                }
            }
        }
    }

    fn encode(&mut self, msg: HttpRequest, buf: &mut Vec<u8>) -> io::Result<()> {
        write!(buf, "{}", msg)?;
        buf.extend_from_slice(&msg.body);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    extern crate env_logger;

    //use std::env;
    use std::io::{Error, ErrorKind};
    use std::thread;
    use std::time::Duration;

    use super::prelude::*;
    use super::futures::sync::mpsc;
    use {HttpRequest, HttpCodec};

    #[test]
    fn channel() {
        // Create the event loop that will drive this server
        let string = "http://localhost:3000/post-test".to_string();
        let req = HttpRequest::post(&string, vec![1, 2, 3, 4]).unwrap()
            .header("Content-Type", "text/plain");

        let mut core = Core::new().unwrap();
        let addr = req.addr().unwrap();
        let handle = core.handle();

        let (mut sender, receiver) = mpsc::channel(1);

        thread::spawn(|| {
            for i in 0 .. 4 {
                let url = "http://localhost:3000/post-test";
                let elements = (0 .. (i + 1)).collect::<Vec<_>>();
                let req = HttpRequest::post(url, elements).unwrap()
                    .header("Content-Type", "text/plain");
                sender = sender.send(req).wait().unwrap();
                thread::sleep(Duration::from_millis(100));
            }
        });


        let _framed = core.run(TcpStream::connect(&addr, &handle).and_then(|connection| {
            let framed = connection.framed(HttpCodec::new());
            receiver.fold(framed, |framed, req| {
                req.send(framed).and_then(|(res, framed)| {
                    println!("channel got response {}", res.unwrap());
                    Ok(framed)
                }).map_err(|_| ())
            }).map_err(|()| Error::new(ErrorKind::Other, "oops"))
        })).unwrap();
    }

    #[test]
    fn two_frames() {
        // Create the event loop that will drive this server
        let string = "http://localhost:3000/post-test".to_string();
        let req = HttpRequest::post(&string, vec![1, 2, 3, 4, 5, 6]).unwrap()
            .header("Content-Type", "text/plain");

        let mut core = Core::new().unwrap();
        let addr = req.addr().unwrap();
        let handle = core.handle();
        let (res, framed) = core.run(TcpStream::connect(&addr, &handle).and_then(|connection| {
            let framed = connection.framed(HttpCodec::new());
            req.send(framed)
        })).unwrap();
        println!("hello 1 {}", res.unwrap());

        thread::sleep(Duration::from_secs(1));

        // should receive a response and then close the connection
        let req = HttpRequest::get("http://localhost:3000/").unwrap();
        let (res, _framed) = core.run(req.send(framed)).unwrap();
        if let Some(res) = res {
            println!("hello 2 {}", res);
            assert!(res.is("Connection", "close"));
        } else {
            assert!(false);
        }
    }
}