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
//! Controllers: one unit of request handling for [`App`](crate::app::App).
//!
//! A [`Controller`] answers two questions: `is_matching(request, connection)` —
//! does this request belong to me? — and `process(request, response, connection)`
//! — produce the response. `App::execute` asks each built-in controller in order
//! and uses the first match. `is_matching` runs for every request, so keep it
//! cheap and silent (check the method before the path).
//!
//! The server can't recover from errors inside a controller, so `process` must
//! turn failures into a response itself (e.g. a `500`). `src/controller/example/`
//! has an `IndexController` that serves `index.html` from the working directory,
//! falling back to a page compiled into the binary with `include_bytes!`.
use crateRequest;
use crateResponse;
use crateConnectionInfo;
/// Core routing and handling trait. Implement this to add a route to the server.
///
/// # Example
///
/// ```rust,no_run
/// use rust_web_server::controller::Controller;
/// use rust_web_server::request::{METHOD, Request};
/// use rust_web_server::response::{Response, STATUS_CODE_REASON_PHRASE};
/// use rust_web_server::range::Range;
/// use rust_web_server::mime_type::MimeType;
/// use rust_web_server::server::ConnectionInfo;
///
/// pub struct HelloController;
///
/// impl Controller for HelloController {
/// fn is_matching(request: &Request, _: &ConnectionInfo) -> bool {
/// request.method == METHOD.get && request.request_uri == "/hello"
/// }
///
/// fn process(_: &Request, mut response: Response, _: &ConnectionInfo) -> Response {
/// response.status_code = *STATUS_CODE_REASON_PHRASE.n200_ok.status_code;
/// response.reason_phrase = STATUS_CODE_REASON_PHRASE.n200_ok.reason_phrase.to_string();
/// response.content_range_list = vec![
/// Range::get_content_range(b"Hello!".to_vec(), MimeType::TEXT_PLAIN.to_string())
/// ];
/// response
/// }
/// }
/// ```