diff --git a/Cargo.lock b/Cargo.lock index 293d7ab3..657abb50 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,15 +13,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "aho-corasick" -version = "0.7.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] - [[package]] name = "async-trait" version = "0.1.61" @@ -114,24 +105,6 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" -[[package]] -name = "file-matcher" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54a429ce9698e7d0de61e7f570477cc2d2f8b3f48f976b2edcfdbd797c085eb" -dependencies = [ - "fs_extra", - "regex", - "serde", - "wildmatch", -] - -[[package]] -name = "fs_extra" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394" - [[package]] name = "generic-array" version = "0.14.6" @@ -167,7 +140,6 @@ name = "hello" version = "0.1.0" dependencies = [ "config", - "file-matcher", ] [[package]] @@ -311,23 +283,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "regex" -version = "1.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.28" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" - [[package]] name = "ron" version = "0.7.1" @@ -467,12 +422,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "wildmatch" -version = "2.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee583bdc5ff1cf9db20e9db5bb3ff4c3089a8f6b8b31aff265c9aba85812db86" - [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index 69dc1e73..2c6490dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,3 @@ edition = "2021" [dependencies] config = "0.13.3" -file-matcher = "0.7.0" diff --git a/README.md b/README.md index 9c8f0245..e0f0e3dc 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,6 @@ This is a simple web server expanding on the example given in the Rust book. At the present time, it answers with static html. TODO: -* Also answer css requests +* Also answer css requests, pictures, etc => OK +* Fully integrate the config path => More or less * Gather info about the client (IP, User-Agent, etc) to do funny stuff with it diff --git a/html/css/main.css b/html/css/main.css new file mode 100644 index 00000000..e965047a --- /dev/null +++ b/html/css/main.css @@ -0,0 +1 @@ +Hello diff --git a/html/index.html b/html/index.html index 8eee6017..fcd70b35 100644 --- a/html/index.html +++ b/html/index.html @@ -1,361 +1,14 @@ - - - - - Welcome - - - - - - - - - -
- - - - - -
- - -
-

Intro

- -

Aenean ornare velit lacus, ac varius enim ullamcorper eu. Proin aliquam facilisis ante interdum congue. Integer mollis, nisl amet convallis, porttitor magna ullamcorper, amet egestas mauris. Ut magna finibus nisi nec lacinia. Nam maximus erat id euismod egestas. By the way, check out my awesome work.

-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis dapibus rutrum facilisis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Etiam tristique libero eu nibh porttitor fermentum. Nullam venenatis erat id vehicula viverra. Nunc ultrices eros ut ultricies condimentum. Mauris risus lacus, blandit sit amet venenatis non, bibendum vitae dolor. Nunc lorem mauris, fringilla in aliquam at, euismod in lectus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In non lorem sit amet elit placerat maximus. Pellentesque aliquam maximus risus, vel sed vehicula.

-
- - -
-

Work

- -

Adipiscing magna sed dolor elit. Praesent eleifend dignissim arcu, at eleifend sapien imperdiet ac. Aliquam erat volutpat. Praesent urna nisi, fringila lorem et vehicula lacinia quam. Integer sollicitudin mauris nec lorem luctus ultrices.

-

Nullam et orci eu lorem consequat tincidunt vivamus et sagittis libero. Mauris aliquet magna magna sed nunc rhoncus pharetra. Pellentesque condimentum sem. In efficitur ligula tate urna. Maecenas laoreet massa vel lacinia pellentesque lorem ipsum dolor. Nullam et orci eu lorem consequat tincidunt. Vivamus et sagittis libero. Mauris aliquet magna magna sed nunc rhoncus amet feugiat tempus.

-
- - -
-

About

- -

Lorem ipsum dolor sit amet, consectetur et adipiscing elit. Praesent eleifend dignissim arcu, at eleifend sapien imperdiet ac. Aliquam erat volutpat. Praesent urna nisi, fringila lorem et vehicula lacinia quam. Integer sollicitudin mauris nec lorem luctus ultrices. Aliquam libero et malesuada fames ac ante ipsum primis in faucibus. Cras viverra ligula sit amet ex mollis mattis lorem ipsum dolor sit amet.

-
- - - - - -
-

Elements

- -
-

Text

-

This is bold and this is strong. This is italic and this is emphasized. - This is superscript text and this is subscript text. - This is underlined and this is code: for (;;) { ... }. Finally, this is a link.

-
-

Heading Level 2

-

Heading Level 3

-

Heading Level 4

-
Heading Level 5
-
Heading Level 6
-
-

Blockquote

-
Fringilla nisl. Donec accumsan interdum nisi, quis tincidunt felis sagittis eget tempus euismod. Vestibulum ante ipsum primis in faucibus vestibulum. Blandit adipiscing eu felis iaculis volutpat ac adipiscing accumsan faucibus. Vestibulum ante ipsum primis in faucibus lorem ipsum dolor sit amet nullam adipiscing eu felis.
-

Preformatted

-
i = 0;
-
-while (!deck.isInOrder()) {
-    print 'Iteration ' + i;
-    deck.shuffle();
-    i++;
-}
-
-print 'It took ' + i + ' iterations to sort the deck.';
-
- -
-

Lists

- -

Unordered

-
    -
  • Dolor pulvinar etiam.
  • -
  • Sagittis adipiscing.
  • -
  • Felis enim feugiat.
  • -
- -

Alternate

-
    -
  • Dolor pulvinar etiam.
  • -
  • Sagittis adipiscing.
  • -
  • Felis enim feugiat.
  • -
- -

Ordered

-
    -
  1. Dolor pulvinar etiam.
  2. -
  3. Etiam vel felis viverra.
  4. -
  5. Felis enim feugiat.
  6. -
  7. Dolor pulvinar etiam.
  8. -
  9. Etiam vel felis lorem.
  10. -
  11. Felis enim et feugiat.
  12. -
-

Icons

- - -

Actions

- - -
- -
-

Table

-

Default

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameDescriptionPrice
Item OneAnte turpis integer aliquet porttitor.29.99
Item TwoVis ac commodo adipiscing arcu aliquet.19.99
Item Three Morbi faucibus arcu accumsan lorem.29.99
Item FourVitae integer tempus condimentum.19.99
Item FiveAnte turpis integer aliquet porttitor.29.99
100.00
-
- -

Alternate

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NameDescriptionPrice
Item OneAnte turpis integer aliquet porttitor.29.99
Item TwoVis ac commodo adipiscing arcu aliquet.19.99
Item Three Morbi faucibus arcu accumsan lorem.29.99
Item FourVitae integer tempus condimentum.19.99
Item FiveAnte turpis integer aliquet porttitor.29.99
100.00
-
-
- -
-

Buttons

- - - -
    -
  • Disabled
  • -
  • Disabled
  • -
-
- -
-

Form

-
-
- - -
-
- - -
-
- -
- -
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
    -
  • -
  • -
-
-
- -
- -
- - - - -
- - -
- - - - - - - - + + + + + Hi! + + +

Hello

+ Ferris +

Hi from Rust

+ NSFW ? + Mode + - diff --git a/html/pictures/bondage.jpg b/html/pictures/bondage.jpg new file mode 100644 index 00000000..c11f9e7d Binary files /dev/null and b/html/pictures/bondage.jpg differ diff --git a/html/pictures/ferris.jpg b/html/pictures/ferris.jpg new file mode 100644 index 00000000..87e9987b Binary files /dev/null and b/html/pictures/ferris.jpg differ diff --git a/html/pictures/modes.png b/html/pictures/modes.png new file mode 100644 index 00000000..c19ed1c9 Binary files /dev/null and b/html/pictures/modes.png differ diff --git a/html/crabe.html b/html/test/crabe.html similarity index 54% rename from html/crabe.html rename to html/test/crabe.html index 2b518584..0099ca5c 100644 --- a/html/crabe.html +++ b/html/test/crabe.html @@ -2,10 +2,11 @@ - CRAB! + Naughty! -

I'm a crab

+

NSFW

Hi from Rust

+ diff --git a/src/main.rs b/src/main.rs index 20574cf8..96319029 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,3 @@ -extern crate file_matcher; - -use file_matcher::FileNamed; use hello::{ ThreadPool, }; @@ -9,15 +6,18 @@ use std::{ io::{prelude::*, BufReader}, net::{TcpListener, TcpStream}, collections::HashMap, - path::Path, }; use config::Config; +//CONSTS +static ROOT_FOLDER: &str = "./html"; +static CONF_FILE: &str = "websrv_conf"; + fn main() { //building config let cfg = Config::builder() - .add_source(config::File::with_name("websrv_conf")) + .add_source(config::File::with_name(CONF_FILE)) .build() .unwrap(); @@ -39,52 +39,46 @@ fn main() { .unwrap_or(&default_bind_addr) .to_string(); - let default_webpage = String::from("index.html"); - let webpage = cfg - .get("webpage") - .unwrap_or(&default_webpage) - .to_string(); - let listener = TcpListener::bind(bind_addr).unwrap(); let pool = ThreadPool::new(nbr_of_threads); for stream in listener.incoming() { let stream = stream.unwrap(); - let webp = webpage.clone(); pool.execute(move || { - handle_connection(stream, webp); + handle_connection(stream); }); } println!("Shutting down."); } -fn search_page(page: &String) -> Option { - if page == "/" { - match FileNamed::regex(r"index\.htm.*").within("./html").find() { - Ok(f) => { - return Some(fs::read_to_string(f).unwrap()); - }, - Err(_) => return None, - }; +fn search_page(page: &String) -> Option> { + println!("Looking for {page} in {ROOT_FOLDER}"); + //regular page + if !page.contains('.') { + let possible_pagenames = vec![format!("{page}.html"), format!("{page}.htm")]; + for p in possible_pagenames { + match fs::read(format!("{ROOT_FOLDER}/{p}")) { + Ok(c) => return Some(c), + Err(_) => (), + } + } + return None; + //css, jpg, etc } else { - match FileNamed::regex(format!(r"{}\.htm.*", page)).within("./html").find() { - Ok(f) => { - let fpath = format!("./html/{:?}", f); - return Some(fs::read_to_string(f).unwrap()); - }, + match fs::read(format!("{ROOT_FOLDER}/{page}")) { + Ok(c) => return Some(c), Err(_) => return None, }; } } - - + ///Analyze the request and answer it -///Returns the complete answer -fn analyze_request_line(line: Vec<&str>) -> String { - let req = String::from(line[0]); +///Returns the complete answer, as a vector of bytes +fn analyze_request_line(line: Vec<&str>) -> Vec { + let _req = String::from(line[0]); let mut page = String::from(line[1]); let proto = String::from(line[2]); let mut ret_code = "404 NOT FOUND"; @@ -92,34 +86,59 @@ fn analyze_request_line(line: Vec<&str>) -> String { //Removing the leading / from the page if need be if &page != "/" { page.remove(0); + } else { + page = String::from("index"); } - let content = match search_page(&page) { + let mut content = match search_page(&page) { Some(c) => { *&mut ret_code = "200 OK"; c }, None => { - fs::read_to_string("./html/err_pages/404.html").unwrap_or(String::from("404 not found")) + fs::read("./html/err_pages/404.html").unwrap_or(String::from("404 NOT FOUND GET LOST").as_bytes().to_vec()) }, }; let length = content.len(); - let returned_line = format!( - "{proto} {ret_code}\r\nContent-Length: {length}\r\n\r\n{content}" - ); - return returned_line; + //Try to read content + match String::from_utf8(content.clone()) { + Ok(c) => { + let length = c.len(); + let returned_line = format!( + "{proto} {ret_code}\r\nContent-Length: {length}\r\n\r\n{c}" + ); + let returned_line = returned_line.as_bytes().to_vec(); + return returned_line; + }, + Err(_e) => { + println!("Got {_e} since we are sending a picture"); + }, + }; + + let img_format = if page.ends_with(".jpg") || page.ends_with(".jpeg") { + "jpg" + } else { + "png" + }; + + let returned_line = format!( + "{proto} {ret_code}\r\ncontent-type: image/{img_format}\r\ncontent-length: {length}\r\n\r\n" + ); + let mut returned_line = returned_line.as_bytes().to_vec(); + returned_line.append(&mut content); + return returned_line; } -fn handle_connection(mut stream: TcpStream, webpage: String) { +fn handle_connection(mut stream: TcpStream) { let buf_reader = BufReader::new(&mut stream); //for i in buf_reader.lines() { // println!("{:?}", i); //} let mut req_lines = buf_reader.lines(); - let mut request = match req_lines.next() { + let request = match req_lines.next() { Some(c) => c, None => Ok(String::from("RIEN")), }; @@ -132,7 +151,8 @@ fn handle_connection(mut stream: TcpStream, webpage: String) { let request_analyze: Vec<&str> = request.split_whitespace().collect(); let ret_line = analyze_request_line(request_analyze); - stream.write_all(ret_line.as_bytes()).unwrap(); + let payload: &[u8] = &ret_line; + stream.write_all(payload).unwrap(); } } diff --git a/websrv_conf.toml b/websrv_conf.toml index c1178703..9b05ccdf 100644 --- a/websrv_conf.toml +++ b/websrv_conf.toml @@ -1,3 +1,3 @@ threads = 10 -webpage = "index.html" bind_addr = "0.0.0.0:7878" +root_folder = "/home/justine/Sandbox/Rust/websrv/html"