ferron_common/
modules.rs

1use std::collections::HashSet;
2use std::error::Error;
3use std::net::SocketAddr;
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use bytes::Bytes;
8use http_body_util::combinators::BoxBody;
9use hyper::{HeaderMap, Request, Response, StatusCode, Uri};
10
11use crate::config::ServerConfiguration;
12use crate::logging::ErrorLogger;
13
14/// A trait that defines a module loader
15pub trait ModuleLoader {
16  /// Loads a module according to specific configuration
17  fn load_module(
18    &mut self,
19    config: &ServerConfiguration,
20    global_config: Option<&ServerConfiguration>,
21    secondary_runtime: &tokio::runtime::Runtime,
22  ) -> Result<Arc<dyn Module + Send + Sync>, Box<dyn Error + Send + Sync>>;
23
24  /// Determines configuration properties required to load a module
25  fn get_requirements(&self) -> Vec<&'static str> {
26    vec![]
27  }
28
29  /// Validates the server configuration
30  #[allow(unused_variables)]
31  fn validate_configuration(
32    &self,
33    config: &ServerConfiguration,
34    used_properties: &mut HashSet<String>,
35  ) -> Result<(), Box<dyn Error + Send + Sync>> {
36    Ok(())
37  }
38}
39
40/// A trait that defines a module
41pub trait Module {
42  /// Obtains the module handlers
43  fn get_module_handlers(&self) -> Box<dyn ModuleHandlers>;
44}
45
46/// A trait that defines handlers for a module
47#[async_trait(?Send)]
48pub trait ModuleHandlers {
49  /// Handles the incoming request
50  async fn request_handler(
51    &mut self,
52    request: Request<BoxBody<Bytes, std::io::Error>>,
53    config: &ServerConfiguration,
54    socket_data: &SocketData,
55    error_logger: &ErrorLogger,
56  ) -> Result<ResponseData, Box<dyn Error + Send + Sync>>;
57
58  /// Modifies the outgoing response
59  async fn response_modifying_handler(
60    &mut self,
61    response: Response<BoxBody<Bytes, std::io::Error>>,
62  ) -> Result<Response<BoxBody<Bytes, std::io::Error>>, Box<dyn Error>> {
63    Ok(response)
64  }
65}
66
67/// Contains information about a network socket, including remote and local addresses,
68/// and whether the connection is encrypted.
69pub struct SocketData {
70  /// The remote address of the socket.
71  pub remote_addr: SocketAddr,
72
73  /// The local address of the socket.
74  pub local_addr: SocketAddr,
75
76  /// Indicates if the connection is encrypted.
77  pub encrypted: bool,
78}
79
80/// Data related to an HTTP request
81#[derive(Clone)]
82pub struct RequestData {
83  /// The authenticated username
84  pub auth_user: Option<String>,
85
86  /// The original URL (before URL rewriting)
87  pub original_url: Option<Uri>,
88
89  /// The error status code, when the error handler is executed
90  #[allow(dead_code)]
91  pub error_status_code: Option<StatusCode>,
92}
93
94/// Data related to an HTTP response
95pub struct ResponseData {
96  /// The passed HTTP request
97  pub request: Option<Request<BoxBody<Bytes, std::io::Error>>>,
98
99  /// The HTTP response with a body
100  pub response: Option<Response<BoxBody<Bytes, std::io::Error>>>,
101
102  /// The HTTP response status code (when the response with a body isn't set)
103  pub response_status: Option<StatusCode>,
104
105  /// The HTTP response headers
106  pub response_headers: Option<HeaderMap>,
107
108  /// The new client address
109  pub new_remote_address: Option<SocketAddr>,
110}