rsweb 0.9.12

library for creating multithreaded web servers in rust
Documentation
//! # rsweb
//! ## library for interacting with the http protocol and creating a multithreaded web server
//! To create a simple server just use the following code:
//! ```rust
//!
//! use rsweb::resource::ResourceLoader;
//! use rsweb::route::Router;
//! use rsweb::server::Server;
//! use rsweb::config::Config;
//!
//! let conf = Config {http: None, ssl: None}; // just a config so this example works. In reality you would load a config
//! let mut server = Server::new(
//!     10, // number of threads
//!     ResourceLoader::new(10, ".".to_string(), true), // create a new resource loader with capacity 10
//!     // and caching enabled
//!     Router::new(), // create a new router with index at index.html
//!     8080, // port
//!     std::net::IpAddr::V4(std::net::Ipv4Addr::new(127,0,0,1)), // ip (localhost in this case)
//!     conf,
//! );
//! ```
//!
//! `rsweb` also supports ssl. To create a simple server that uses ssl use:
//! ```rust
//! use rsweb::resource::ResourceLoader;
//! use rsweb::route::Router;
//! use rsweb::ssl::SSLServer;
//! use rsweb::config::Config;
//!
//! let conf = Config {http: None, ssl: None}; // just a config so this example works.
//!
//! let mut server = SSLServer::new(
//!     10, // number of threads
//!     ResourceLoader::new(10, ".".to_string(), true), // create a new resource loader with capacity 10 and caching enabled
//!     Router::new(), // create a new router with index at index.html
//!     8080, // port
//!     std::net::IpAddr::V4(std::net::Ipv4Addr::new(127,0,0,1)), // ip (localhost in this case)
//!     String::from("key.pem"), // private key file
//!     String::from("certs.pem"), // certificate chain file
//!     conf,
//! );
//! ```

/// the cli of `rsweb`
#[cfg(feature = "full")]
pub mod cli;

/// the configuration reading functionality of `rsweb`
#[cfg(feature = "full")]
pub mod config;

/// a dynamic buffer implementation used to read from streams
#[cfg(feature = "base")]
pub mod dbuffer;

/// errors for `rsweb`
#[cfg(feature = "base")]
pub mod error;
#[cfg(feature = "base")]
pub mod http;

/// logging functions for `rsweb`
#[cfg(feature = "base")]
pub mod log;

/// resource handler and cache storage
#[cfg(feature = "resource")]
pub mod resource;

/// router for requests
#[cfg(feature = "resource")]
pub mod route;

/// basic HTTP server implementation
#[cfg(feature = "server")]
pub mod server;

/// basic HTTPS server implementation
#[cfg(feature = "ssl")]
pub mod ssl;

/// Threadpool implementation included in the logs produced by [`log`]
#[cfg(feature = "base")]
pub mod tp;

pub use tp::ThreadPool;
/// version str of rsweb. Used for logging and CLI
pub const RSWEB_VERSION: &str = "0.8.11";
/// version str of rsweb used in the `Server` response header
pub const RSWEB_SERVER_STR: &str = "rsweb/0.8.11";

#[cfg(test)]
mod tests {
    mod thread_pool {
        use crate::tp::ThreadPool;
        #[test]
        fn thread_pool_create() {
            let _ = ThreadPool::new(1);
        }

        #[test]
        #[should_panic]
        fn empty_thread_pool() {
            let _ = ThreadPool::new(0);
        }
    }

    mod dbuffer {
        use crate::dbuffer::DBuffer;

        #[test]
        fn dbuffer_create() {
            let _ = DBuffer::new();
        }

        #[test]
        fn dbuffer_create_with_cap() {
            let _ = DBuffer::with_capacity(6);
        }

        #[test]
        fn dbuffer_read() {
            let mut dbuffer = DBuffer::new();
            let s = String::from("hello");
            assert!(dbuffer.read_until_zero(&mut s.as_bytes()).is_ok());
        }

        #[test]
        fn dbuffer_to_string() {
            let mut dbuffer = DBuffer::new();
            let s = String::from("hello");
            assert!(dbuffer.read_until_zero(&mut s.as_bytes()).is_ok());
            assert!(dbuffer.to_string().is_ok());
        }
    }
    mod router {
        use crate::route::Route;
        use crate::route::Router;

        #[test]
        fn router_alias() {
            let mut router = Router::new();
            router.alias(String::from("/"), String::from("/index.html"));
            assert_eq!(
                router.lookup("/"),
                Some(Route::Alias("/index.html".to_string()))
            );
        }
        #[test]
        fn router_route() {
            let mut router = Router::new();
            router.route(String::from("/"), String::from("/index.html"));
            assert!(router.lookup("/").is_some());
        }
    }

    mod http {

        #[test]
        fn request_from_string() {
            let request_string = "GET / HTTP/1.1\r\nHost: examplehost.com\r\n\r\nbody";
            assert!(crate::http::HTTPRequest::from_string(request_string.to_string()).is_ok());
            let request =
                crate::http::request::HTTPRequest::from_string(request_string.to_string()).unwrap();
            assert_eq!(request.get_path(), "/".to_string());
            assert_eq!(request.get_body(), &Some("body".to_string()));
        }
    }
}