socket_flow/
config.rs

1use crate::extensions::Extensions;
2use rustls::ServerConfig as RustlsConfig;
3use std::sync::Arc;
4
5/// Used for spawning a websockets server, including the general websocket
6/// connection configuration, and a tls_config, which is basically a TLS config
7/// in the case you want to have TLS enabled for your server.
8#[derive(Debug, Clone, Default)]
9pub struct ServerConfig {
10    pub web_socket_config: Option<WebSocketConfig>,
11    /// We currently support tokio-rustls/rustls for enabling TLS on server-side
12    /// This config holds information about the TLS certificate-chain and everything
13    /// that should be taken into consideration over the TLS setup.
14    pub tls_config: Option<Arc<RustlsConfig>>,
15}
16
17/// Used for connecting over websocket endpoints as a client
18/// including the general websocket connection configuration
19/// with the addition of a ca_file, in the case the server you
20/// are trying to connect uses a self-signed certificate.
21#[derive(Debug, Clone, Default)]
22pub struct ClientConfig {
23    pub web_socket_config: WebSocketConfig,
24    /// In some cases, the server you are trying to connect would use
25    /// a self-signed certificate.
26    /// On these cases, the client would not be able to verify the CA signature of that
27    /// certificate,
28    /// only by looking over our browser/operational system certificate store of trusted known CAs.
29    /// The client would need to provide the CA file used to sign the server certificate
30    /// otherwise the TLS handshake would fail.
31    /// This TLS setup is mostly used for development,
32    /// and we don't recommend for production purposes
33    pub ca_file: Option<String>,
34}
35
36// TODO - Remove extensions, and only add an option named compression_enabled
37// and a second option for compression_threshold
38// then, behind the hood, set extensions all to true, and max window of 15
39/// Stores general configurations, to replace some default websockets connection parameters
40#[derive(Debug, Clone)]
41pub struct WebSocketConfig {
42    /// Maximum value for Frame payload size, not counting the underlying basic frame components.
43    /// By default, the maximum value is set as 16 MiB(Mebibyte) = 16 * 1024 * 1024
44    /// Increasing it over these limits, may impact the performance of the application, as well as
45    /// the security, since malicious user can constantly send huge Frames.
46    pub max_frame_size: Option<usize>,
47    /// A message may be compounded by multiple Frames.
48    /// Therefore, this config variable denotes the
49    /// maximum payload size a message can have.
50    /// The default is 64 MiB, which is reasonably big.
51    pub max_message_size: Option<usize>,
52    /// This represents the extensions that will be applied, enabling compression and
53    /// modifying relevant specs about server and client compression.
54    pub extensions: Option<Extensions>,
55}
56
57impl Default for WebSocketConfig {
58    fn default() -> Self {
59        WebSocketConfig {
60            max_message_size: Some(64 << 20),
61            max_frame_size: Some(16 << 20),
62            extensions: None,
63        }
64    }
65}