Spaces:
Runtime error
Runtime error
File size: 13,842 Bytes
9552aa0 | 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | // Import server module from "server.rs"
#[path = "server.rs"]
mod ferron_server;
// Import request handler module from "request_handler.rs"
#[path = "request_handler.rs"]
mod ferron_request_handler;
// Import resources from "res" directory
#[path = "res"]
mod ferron_res {
pub mod server_software;
}
// Import common modules from "common" directory
#[path = "common/mod.rs"]
mod ferron_common;
// Import utility modules from "util" directory
#[path = "util"]
mod ferron_util {
pub mod anti_xss;
#[cfg(feature = "asgi")]
pub mod asgi_messages;
#[cfg(feature = "asgi")]
pub mod asgi_structs;
#[cfg(any(feature = "cgi", feature = "scgi", feature = "fcgi"))]
pub mod cgi_response;
pub mod combine_config;
#[cfg(any(feature = "cgi", feature = "scgi", feature = "fcgi"))]
pub mod copy_move;
pub mod error_pages;
#[cfg(feature = "fcgi")]
pub mod fcgi_decoder;
#[cfg(feature = "fcgi")]
pub mod fcgi_encoder;
#[cfg(feature = "fcgi")]
pub mod fcgi_name_value_pair;
#[cfg(feature = "fcgi")]
pub mod fcgi_record;
pub mod generate_directory_listing;
pub mod ip_blocklist;
pub mod ip_match;
pub mod load_config;
pub mod load_tls;
pub mod match_hostname;
pub mod match_location;
#[cfg(any(feature = "rproxy", feature = "fauth"))]
pub mod no_server_verifier;
pub mod non_standard_code_structs;
#[cfg(all(unix, feature = "wsgid"))]
pub mod preforked_process_pool;
#[cfg(feature = "fcgi")]
pub mod read_to_end_move;
pub mod sizify;
pub mod sni;
#[cfg(feature = "fcgi")]
pub mod split_stream_by_map;
pub mod ttl_cache;
pub mod url_rewrite_structs;
pub mod url_sanitizer;
pub mod validate_config;
#[cfg(feature = "wsgi")]
pub mod wsgi_error_stream;
#[cfg(feature = "wsgi")]
pub mod wsgi_input_stream;
#[cfg(any(feature = "wsgi", feature = "wsgid"))]
pub mod wsgi_load_application;
#[cfg(feature = "wsgi")]
pub mod wsgi_structs;
#[cfg(feature = "wsgid")]
pub mod wsgid_body_reader;
#[cfg(feature = "wsgid")]
pub mod wsgid_error_stream;
#[cfg(feature = "wsgid")]
pub mod wsgid_input_stream;
#[cfg(feature = "wsgid")]
pub mod wsgid_message_structs;
#[cfg(feature = "wsgid")]
pub mod wsgid_structs;
}
// Import project modules from "modules" directory
#[path = "modules"]
mod ferron_modules {
pub mod blocklist;
pub mod default_handler_checks;
pub mod non_standard_codes;
pub mod redirect_trailing_slashes;
pub mod redirects;
pub mod static_file_serving;
pub mod url_rewrite;
pub mod x_forwarded_for;
}
// Import optional project modules from "modules" directory
#[path = "optional_modules"]
mod ferron_optional_modules {
#[cfg(feature = "asgi")]
pub mod asgi;
#[cfg(feature = "cache")]
pub mod cache;
#[cfg(feature = "cgi")]
pub mod cgi;
#[cfg(feature = "example")]
pub mod example;
#[cfg(feature = "fauth")]
pub mod fauth;
#[cfg(feature = "fcgi")]
pub mod fcgi;
#[cfg(feature = "fproxy")]
pub mod fproxy;
#[cfg(feature = "rproxy")]
pub mod rproxy;
#[cfg(feature = "scgi")]
pub mod scgi;
#[cfg(feature = "wsgi")]
pub mod wsgi;
#[cfg(feature = "wsgid")]
pub mod wsgid;
}
// Standard library imports
use std::sync::Arc;
use std::{error::Error, path::PathBuf};
// External crate imports
use clap::Parser;
use ferron_server::start_server;
use ferron_util::load_config::load_config;
use mimalloc::MiMalloc;
// Set the global allocator to use mimalloc for performance optimization
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
// Struct for command-line arguments
/// A fast, memory-safe web server written in Rust
#[derive(Parser, Debug)]
#[command(name = "Ferron")]
#[command(version, about, long_about = None)]
struct Args {
/// The path to the server configuration file
#[arg(short, long, default_value_t = String::from("./ferron.yaml"))]
config: String,
}
// Function to execute before starting the server
#[allow(clippy::type_complexity)]
fn before_starting_server(
args: &Args,
first_start: bool,
) -> Result<bool, Box<dyn Error + Send + Sync>> {
// Load the configuration
let yaml_config = load_config(PathBuf::from(args.config.clone()))?;
let mut module_error = None;
let mut module_libs = Vec::new();
// Load external modules defined in the configuration file
if let Some(modules) = yaml_config["global"]["loadModules"].as_vec() {
for module_name_yaml in modules.iter() {
if let Some(module_name) = module_name_yaml.as_str() {
module_libs.push(String::from(module_name));
}
}
}
let mut external_modules = Vec::new();
#[allow(unused_mut)]
let mut modules_optional_builtin = Vec::new();
// Iterate over loaded module libraries and initialize them
for module_name in module_libs.iter() {
match module_name as &str {
#[cfg(feature = "rproxy")]
"rproxy" => {
external_modules.push(
match ferron_optional_modules::rproxy::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "fproxy")]
"fproxy" => {
external_modules.push(
match ferron_optional_modules::fproxy::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "cache")]
"cache" => {
external_modules.push(
match ferron_optional_modules::cache::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "cgi")]
"cgi" => {
external_modules.push(
match ferron_optional_modules::cgi::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "scgi")]
"scgi" => {
external_modules.push(
match ferron_optional_modules::scgi::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "fcgi")]
"fcgi" => {
external_modules.push(
match ferron_optional_modules::fcgi::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "fauth")]
"fauth" => {
external_modules.push(
match ferron_optional_modules::fauth::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "example")]
"example" => {
external_modules.push(
match ferron_optional_modules::example::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "wsgi")]
"wsgi" => {
external_modules.push(
match ferron_optional_modules::wsgi::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "wsgid")]
"wsgid" => {
external_modules.push(
match ferron_optional_modules::wsgid::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
#[cfg(feature = "asgi")]
"asgi" => {
external_modules.push(
match ferron_optional_modules::asgi::server_module_init(&yaml_config) {
Ok(module) => module,
Err(err) => {
module_error = Some(anyhow::anyhow!(
"Cannot initialize optional built-in module \"{}\": {}",
module_name,
err
));
break;
}
},
);
modules_optional_builtin.push(module_name.clone());
}
_ => {
module_error = Some(anyhow::anyhow!(
"The optional built-in module \"{}\" doesn't exist",
module_name
));
break;
}
}
}
// Add modules (both built-in and loaded)
let mut modules = Vec::new();
match ferron_modules::x_forwarded_for::server_module_init() {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
match ferron_modules::redirects::server_module_init() {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
match ferron_modules::blocklist::server_module_init(&yaml_config) {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
match ferron_modules::url_rewrite::server_module_init(&yaml_config) {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
match ferron_modules::non_standard_codes::server_module_init(&yaml_config) {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
match ferron_modules::redirect_trailing_slashes::server_module_init() {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
modules.append(&mut external_modules);
match ferron_modules::default_handler_checks::server_module_init() {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
match ferron_modules::static_file_serving::server_module_init() {
Ok(module) => modules.push(module),
Err(err) => {
if module_error.is_none() {
module_error = Some(anyhow::anyhow!("Cannot load a built-in module: {}", err));
}
}
};
// Start the server with configuration and loaded modules
start_server(
Arc::new(yaml_config),
modules,
module_error,
modules_optional_builtin,
first_start,
)
}
// Entry point of the application
fn main() {
let args = &Args::parse(); // Parse command-line arguments
let mut first_start = true;
loop {
match before_starting_server(args, first_start) {
Ok(false) => break,
Ok(true) => {
first_start = false;
println!("Reloading the server configuration...");
}
Err(err) => {
eprintln!("FATAL ERROR: {}", err);
std::process::exit(1);
}
}
}
}
|