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
//! Nemesis [`Request`](crate::request::Request) implementation using
//! [Rocket](rocket).

use rocket::http::uri::Origin;
use rocket::http::{Method, Status};
use rocket::request::{self as rocketreq, FromRequest};
use rocket::Outcome;

use crate::request as nemesisreq;
use crate::Error;

pub struct Request<'a> {
    method: Method,
    origin: &'a Origin<'a>,
    base: &'a str,
}

impl<'a> nemesisreq::Request for Request<'a> {
    fn origin(&self) -> String {
        self.origin.path().to_owned()
    }

    fn http_version(&self) -> Option<String> {
        // Rocket does not expose HTTP version
        // https://github.com/SergioBenitez/Rocket/issues/1019
        None
    }

    fn request_method(&self) -> String {
        format!("{}", self.method)
    }

    fn script_name(&self) -> String {
        match self.base {
            "/" => "".to_owned(),
            base => base.to_owned(),
        }
    }

    fn path_info(&self) -> String {
        let uri = self.origin.path();
        // This &str slice is safe because URIs are guaranteed to be ASCII
        // (single byte characters).
        uri[self.base.len()..].to_owned()
    }

    fn query_string(&self) -> String {
        self.origin.query().unwrap_or_default().to_owned()
    }

    fn server_name(&self) -> String {
        // TODO: implement using config fairing, see GH-61.
        "localhost".to_owned()
    }

    fn server_port(&self) -> u16 {
        // TODO: implement using config fairing, see GH-61.
        8000
    }

    fn url_scheme(&self) -> String {
        // TODO: implement using config fairing, see GH-61.
        "http".to_owned()
    }
}

impl<'a, 'r> FromRequest<'a, 'r> for Request<'a> {
    type Error = Error;

    fn from_request(request: &'a rocket::Request<'r>) -> rocketreq::Outcome<Self, Self::Error> {
        if let Some(route) = request.route() {
            Outcome::Success(Request {
                method: request.method(),
                origin: request.uri(),
                base: route.base(),
            })
        } else {
            Outcome::Failure((Status::NotFound, Error::NoRoute))
        }
    }
}