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
use url;
use std::net;
use plugin::{Extensible, Pluggable};
pub use iron::{Url, Handler};

use iron::{self};
use bodyparser;

use backend::{self};
use super::super::framework;

use server::method;
use server::header;
use super::super::errors;
use super::request;

pub type HandleResultStrict<T> = Result<T, errors::StrictErrorResponse>;
pub type HandleResult<T> = Result<T, errors::ErrorResponse>;
pub type HandleSuccessResult = HandleResult<()>;

impl<'a, 'b> request::Body for iron::request::Body<'a, 'b> { }

pub trait WrapUrl {
    fn wrap_url(self) -> Url;
}

impl WrapUrl for url::Url {
    fn wrap_url(self) -> Url {
        Url::from_generic_url(self).unwrap()
    }
}

impl backend::AsUrl for Url {
    fn scheme(&self) -> &str { &self.scheme() }
    fn host(&self) -> url::Host<&str> { self.host() }
    fn port(&self) -> u16 { self.port() }
    fn path(&self) -> Vec<&str> { self.path() }
    fn username(&self) -> Option<&str> { self.username() }
    fn password(&self) -> Option<&str> { self.password() }
    fn query(&self) -> Option<&str> { self.query() }
    fn fragment(&self) -> Option<&str> { self.fragment() }
}

impl<'a, 'b> backend::Request for iron::Request<'a, 'b> {
    fn remote_addr(&self) -> &net::SocketAddr { &self.remote_addr }
    fn headers(&self) -> &header::Headers { &self.headers }
    fn method(&self) -> &method::Method { &self.method }
    fn url(&self) -> &backend::AsUrl { &self.url }
    fn body(&self) -> &request::Body { &self.body }
    fn body_mut(&mut self) -> &mut request::Body { &mut self.body }
    fn read_to_end(&mut self) -> Result<Option<String>, Box<errors::Error + Send>> {
        self.get::<bodyparser::Raw>().map_err(|err| Box::new(err) as Box<errors::Error + Send>)
    }
}

impl<'a, 'b> ::Extensible for iron::Request<'a, 'b> {
    fn ext(&self) -> &::typemap::TypeMap { self.extensions() }
    fn ext_mut(&mut self) -> &mut ::typemap::TypeMap { self.extensions_mut() }
}

impl Handler for framework::Application {
    fn handle<'a, 'b>(&self, req: &mut iron::Request<'a, 'b>) -> iron::IronResult<iron::Response> {
        self.call(req)
            .map(|resp| {
                iron::Response {
                    status: Some(resp.status),
                    headers: resp.headers,
                    body: resp.body,
                    extensions: resp.ext
                }
            })
            .map_err(|err_resp| {
                let errors::StrictErrorResponse{error, response} = err_resp;
                iron::IronError {
                    error: error,
                    response: iron::Response {
                        status: Some(response.status),
                        headers: response.headers,
                        body: response.body,
                        extensions: response.ext
                    }
                }
            })
    }
}