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
|
use mlua::UserData;
use tokio::io::{self};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("error parsing request: {0}")]
Parse(#[from] httparse::Error),
#[error("io error: {0}")]
Io(#[from] io::Error),
#[error("invalid method: {0}")]
Method(#[from] http::method::InvalidMethod),
#[error("invalid request: {0}")]
Request(#[from] http::Error),
#[error("unsupported version")]
Version,
}
#[derive(Debug, Clone)]
pub struct Request<T>(http::Request<T>);
impl<T> Request<T> {
pub fn inner(&self) -> &http::Request<T> {
&self.0
}
pub fn new(request: http::Request<T>) -> Self {
Self(request)
}
}
impl<T> UserData for Request<T> {
fn add_fields<F: mlua::UserDataFields<Self>>(fields: &mut F) {
fields.add_field_method_get("method", |_, this| {
Ok(this.inner().method().as_str().to_string())
});
fields.add_field_method_get("path", |_, this| Ok(this.inner().uri().path().to_string()));
fields.add_field_method_get("headers", |lua, this| {
let table = lua.create_table()?;
for (key, value) in this.inner().headers() {
table.set(key.as_str(), value.as_bytes())?;
}
Ok(table)
})
}
}
|