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
use std::io;
use futures::{Async, Poll};
use http2;
use super::{helpers, HttpHandlerTask, Writer};
use http::{StatusCode, Version};
use Error;
#[derive(Debug)]
pub enum AcceptorError<T> {
Service(T),
Io(io::Error),
Timeout,
}
#[derive(Fail, Debug)]
pub enum HttpDispatchError {
#[fail(display = "Application specific error: {}", _0)]
App(Error),
#[fail(display = "IO error: {}", _0)]
Io(io::Error),
#[fail(display = "The first request did not complete within the specified timeout")]
SlowRequestTimeout,
#[fail(display = "Connection shutdown timeout")]
ShutdownTimeout,
#[fail(display = "HTTP2 error: {}", _0)]
Http2(http2::Error),
#[fail(display = "Task is completed but request's payload is not consumed")]
PayloadIsNotConsumed,
#[fail(display = "Malformed request")]
MalformedRequest,
#[fail(display = "Internal error")]
InternalError,
#[fail(display = "Unknown error")]
Unknown,
}
impl From<Error> for HttpDispatchError {
fn from(err: Error) -> Self {
HttpDispatchError::App(err)
}
}
impl From<io::Error> for HttpDispatchError {
fn from(err: io::Error) -> Self {
HttpDispatchError::Io(err)
}
}
impl From<http2::Error> for HttpDispatchError {
fn from(err: http2::Error) -> Self {
HttpDispatchError::Http2(err)
}
}
pub(crate) struct ServerError(Version, StatusCode);
impl ServerError {
pub fn err(ver: Version, status: StatusCode) -> Box<HttpHandlerTask> {
Box::new(ServerError(ver, status))
}
}
impl HttpHandlerTask for ServerError {
fn poll_io(&mut self, io: &mut Writer) -> Poll<bool, Error> {
{
let bytes = io.buffer();
bytes.reserve(helpers::STATUS_LINE_BUF_SIZE + 1);
helpers::write_status_line(self.0, self.1.as_u16(), bytes);
}
let reason = self.1.canonical_reason().unwrap_or("");
io.buffer().extend_from_slice(reason.as_bytes());
io.buffer().extend_from_slice(b"\r\ncontent-length: 0\r\n");
io.set_date();
Ok(Async::Ready(true))
}
}