Skip to content

Commit

Permalink
fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Az107 committed Jun 6, 2024
1 parent da377cb commit 5eb1d21
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "HTeaPot"
name = "hteapot"
version = "0.1.0"
exclude = [
"hteapot.toml"
Expand All @@ -14,5 +14,5 @@ categories = ["network-programming", "web-programming"]
authors = ["Alb Ruiz G. <me@albruiz.dev>"]

[lib]
name = "HTeaPot"
name = "HTeapot"
path = "src/hteapot.rs"
8 changes: 4 additions & 4 deletions src/brew.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Written by Alberto Ruiz 2024-04-08
// This is the HTTP client module, it will handle the requests and responses

use std::{fs, io::{Read, Write}, net::TcpStream};
use std::{io::{Read, Write}, net::TcpStream};


struct url {
struct Url {
scheme: String,
domain: String,
port: String
}


fn parse_url(url: &str) -> Result<url,&str> {
fn parse_url(url: &str) -> Result<Url,&str> {
let url_parts = url.split(":").collect::<Vec<&str>>();
let prefix = url_parts[0];
let domain = url_parts[1].trim_start_matches("//");
Expand All @@ -24,7 +24,7 @@ fn parse_url(url: &str) -> Result<url,&str> {
_ => "80"
}
};
Ok(url {
Ok(Url {
scheme: prefix.to_string(),
domain: domain.to_string(),
port: port.to_string()
Expand Down
40 changes: 20 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,55 +47,55 @@ pub fn toml_parser(content: &str) -> HashMap<String,HashMap<String,String>> {
}


pub struct config {
pub struct Config {
pub port: u16, // Port number to listen
pub host: String, // Host name or IP
pub root: String, // Root directory to serve files
pub index: String, // Index file to serve by default
pub error: String, // Error file to serve when a file is not found
pub proxyRules: HashMap<String, String>
pub proxy_rules: HashMap<String, String>
}

impl config {
pub fn new(port: u16, host: String, root: String, index: String, error: String) -> config {
config {
port: port,
host: host,
root: root,
index: index,
error: error,
proxyRules: HashMap::new()
}
}
impl Config {
// pub fn new(port: u16, host: String, root: String, index: String, error: String) -> Config {
// Config {
// port: port,
// host: host,
// root: root,
// index: index,
// error: error,
// proxy_rules: HashMap::new()
// }
// }

pub fn new_default() -> config {
config {
pub fn new_default() -> Config {
Config {
port: 8080,
host: "localhost".to_string(),
root: "./".to_string(),
index: "index.html".to_string(),
error: "error.html".to_string(),
proxyRules: HashMap::new()
proxy_rules: HashMap::new()
}
}

pub fn load_config(path: &str) -> config {
pub fn load_config(path: &str) -> Config {
let content = fs::read_to_string(path);
if content.is_err() {
return config::new_default();
return Config::new_default();
}
let content = content.unwrap();
let map = toml_parser(&content);
println!("{:?}", map);
let proxy_rules = map.get("proxy").unwrap_or(&HashMap::new()).clone();
let map = map.get("HTEAPOT").unwrap();
config {
Config {
port: map.get("port").unwrap_or(&"8080".to_string()).parse::<u16>().unwrap(),
host: map.get("host").unwrap_or(&"".to_string()).to_string(),
root: map.get("root").unwrap_or(&"./".to_string()).to_string(),
index: map.get("index").unwrap_or(&"index.html".to_string()).to_string(),
error: map.get("error").unwrap_or(&"error.html".to_string()).to_string(),
proxyRules: proxy_rules
proxy_rules
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/hteapot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// This is the HTTP server module, it will handle the requests and responses
// Also provide utilities to parse the requests and build the responses


use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::thread;


#[derive(Debug)]
#[derive(PartialEq)]
#[derive(Eq)]
Expand Down Expand Up @@ -145,18 +145,18 @@ pub struct HttpRequest {
}


pub struct HteaPot {
pub struct Hteapot {
port: u16,
address: String,
// this will store a map from path to their actions
// path_table: HashMap<HttpMethod, HashMap<String, HashMap<HttpMethod, fn(HttpRequest) -> String>>>,
}

impl HteaPot {
impl Hteapot {

// Constructor
pub fn new(address: &str, port: u16) -> Self {
HteaPot {
Hteapot {
port: port,
address: address.to_string(),
// path_table: HashMap::new(),
Expand All @@ -180,7 +180,7 @@ impl HteaPot {
Ok(stream) => {
let action_clone = action_clone.clone();
thread::spawn(move || {
HteaPot::handle_client(stream, |req| {
Hteapot::handle_client(stream, |req| {
action_clone(req)
});
});
Expand Down Expand Up @@ -306,7 +306,7 @@ impl HteaPot {
#[test]
fn test_http_parser() {
let request = "GET / HTTP/1.1\r\nHost: localhost:8080\r\nUser-Agent: curl/7.68.0\r\nAccept: */*\r\n\r\n";
let parsed_request = HteaPot::request_parser(request);
let parsed_request = Hteapot::request_parser(request);
assert_eq!(parsed_request.method, HttpMethod::GET);
assert_eq!(parsed_request.path, "/");
assert_eq!(parsed_request.args.len(), 0);
Expand All @@ -316,7 +316,7 @@ fn test_http_parser() {

#[test]
fn test_http_response_maker() {
let response = HteaPot::response_maker(HttpStatus::IAmATeapot, "Hello, World!", None);
let response = Hteapot::response_maker(HttpStatus::IAmATeapot, "Hello, World!", None);
let expected_response = "HTTP/1.1 418 I'm a teapot\r\nContent-Length: 13\r\n\r\nHello, World!";
assert_eq!(response, expected_response);
}
Expand Down
26 changes: 14 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
mod hteapot;
pub mod hteapot;
mod config;
mod brew;


use std::fs;

use hteapot::HteaPot;
use hteapot::Hteapot;
use hteapot::HttpStatus;
use brew::fetch;

use crate::hteapot::HttpStatus;




fn main() {
let args = std::env::args().collect::<Vec<String>>();
let config = if args.len() > 1 {
config::config::load_config(&args[1])
config::Config::load_config(&args[1])
} else {
config::config::new_default()
config::Config::new_default()
};
let server = HteaPot::new(config.host.as_str(), config.port);
let server = Hteapot::new(config.host.as_str(), config.port);
println!("Server started at http://{}:{}", config.host, config.port);
server.listen(move |req| {
println!("Request: {:?}", req.path);
Expand All @@ -29,26 +31,26 @@ fn main() {
} else {
req.path.clone()
};
if config.proxyRules.contains_key(&req.path) {
println!("Proxying to: {}", config.proxyRules.get(&req.path).unwrap());
let url = config.proxyRules.get(&req.path).unwrap();
if config.proxy_rules.contains_key(&req.path) {
println!("Proxying to: {}", config.proxy_rules.get(&req.path).unwrap());
let url = config.proxy_rules.get(&req.path).unwrap();
return match fetch(url) {
Ok(response) => {
response
},
Err(err) => {
HteaPot::response_maker(HttpStatus::InternalServerError, err, None)
Hteapot::response_maker(HttpStatus::InternalServerError, err, None)
}
}
}
let path = format!("./{}/{}",config.root, path);
let content = fs::read_to_string(path);
match content {
Ok(content) => {
return HteaPot::response_maker(HttpStatus::OK, &content, None);
return Hteapot::response_maker(HttpStatus::OK, &content, None);
},
Err(_) => {
return HteaPot::response_maker(HttpStatus::NotFound, "<h1> 404 Not Found </h1>", headers!("Content-Type" => "text/html", "Server" => "HteaPot"));
return Hteapot::response_maker(HttpStatus::NotFound, "<h1> 404 Not Found </h1>", headers!("Content-Type" => "text/html", "Server" => "HteaPot"));
}
}
});
Expand Down

0 comments on commit 5eb1d21

Please sign in to comment.