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
use crate::server::CookieOptions;
use crate::transport::TransportKind;
use serde::Serialize;
use std::collections::HashMap;
use strum_macros::EnumString;

// FIXME: rename this to HttpRequestContext?
#[derive(Debug, Clone)]
pub struct RequestContext {
    pub query: HashMap<String, String>,
    pub origin: String,
    pub user_agent: String,
    pub transport_kind: TransportKind,
    pub http_method: HttpMethod,
    pub remote_address: String,
    pub body: Option<String>,
}

#[derive(Display, Debug, Copy, Clone, PartialEq)]
pub enum HttpMethod {
    Options,
    Get,
    Post,
    Put,
    Delete,
    Head,
    Trace,
    Connect,
    Patch,
}

#[derive(Display, Debug, Clone, PartialEq, EnumString)]
pub enum ServerError {
    #[strum(serialize = "Transport unknown")]
    UnknownTransport = 0,
    #[strum(serialize = "Session ID unknown")]
    UnknownSid = 1,
    #[strum(serialize = "Bad handshake method")]
    BadHandshakeMethod = 2,
    #[strum(serialize = "Bad request")]
    BadRequest = 3,
    #[strum(serialize = "Forbidden")]
    Forbidden = 4,
    #[strum(serialize = "Unknown")]
    Unknown = -1,
    #[strum(serialize = "Server is shutting down")]
    ShuttingDown = -99,
}

#[derive(Debug, Serialize)]
pub struct ServerErrorMessage {
    pub code: i8,
    pub message: String,
}

impl From<ServerError> for ServerErrorMessage {
    fn from(server_error: ServerError) -> Self {
        let message = server_error.to_string().to_owned();
        ServerErrorMessage {
            code: server_error as i8,
            message,
        }
    }
}

pub struct SetCookie {
    pub name: String,
    pub value: String,
    pub path: String,
    pub http_only: bool,
    pub same_site: bool,
}

impl SetCookie {
    pub(crate) fn from_cookie_options(
        cookie_options: &Option<CookieOptions>,
        value: String,
    ) -> Option<Self> {
        match cookie_options {
            Some(options) => Some(SetCookie {
                name: options.name.clone(),
                value,
                path: options.path.clone(),
                http_only: options.http_only,
                same_site: true,
            }),
            None => None,
        }
    }
}

// pub async fn wait_on_callback<R: Send + 'static>(callback: <Box<dyn Fn() + Send + 'static>>) -> R {
//     unimplemented!()
// }