Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ src/.DS_Store
.idea/vcs.xml
.idea/modules.xml
.idea/HTeaPot.iml
*.log
5 changes: 0 additions & 5 deletions .idea/.gitignore

This file was deleted.

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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hteapot"
version = "0.4.2"
version = "0.5.0" # release candidate
exclude = ["hteapot.toml", "public/", "readme.md"]
license = "MIT"
keywords = ["HTTP", "HTTP-SERVER"]
Expand Down
151 changes: 76 additions & 75 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,87 +1,88 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTeaPot Server</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>HTeaPot Server</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
margin: 0;
}

.container {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
width: 300px;
}
.container {
background-color: white;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
padding: 20px;
text-align: center;
width: 300px;
}

h1 {
font-size:24px;
margin: 10px 0;
}
h1 {
font-size: 24px;
margin: 10px 0;
}

p {
color: #555;
}
p {
color: #555;
}

button {
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
padding: 10px 15px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
}
button {
background-color: #4caf50;
color: white;
border: none;
border-radius: 4px;
padding: 10px 15px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
}

button:hover {
background-color: #45a049;
}
button:hover {
background-color: #45a049;
}

.image-container {
margin-top: 15px;
}
.image-container {
margin-top: 15px;
}

img {
width: 64px;
height: 64px;
border-radius: 50%;
margin-top: 10px;
}
img {
width: 64px;
height: 64px;
border-radius: 50%;
margin-top: 10px;
}

.app-info {
margin-top: 20px;
text-align: center;
}
.app-info {
margin-top: 20px;
text-align: center;
}
</style>
<script src="index.js"></script>
</head>

</style>
<script src="index.js"></script>
</head>
<body>
<div class="container">
<h1>HTeaPot Server</h1>
<p>
Congratulations! HTeaPot, the HTTP server, is correctly
installed and configured.
</p>
<p>You are now ready to serve your web applications.</p>
<button onclick="brew()">Brew!</button>
</div>

<body>

<div class="container">
<h1>HTeaPot Server</h1>
<p>Congratulations! HTeaPot, the HTTP server, is correctly installed and configured.</p>
<p>You are now ready to serve your web applications.</p>
<button onclick="brew()">Brew!</button>
</div>

<div class="app-info">
<div class="image-container">
<img src="favicon.ico" alt="tea"/>
</div>
</div>
</body>
<div class="app-info">
<div class="image-container">
<img src="favicon.ico" alt="🍵" />
</div>
</div>
</body>
</html>
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ pub struct Config {
pub cache: bool,
pub cache_ttl: u16,
pub threads: u16,
pub log_file: Option<String>,
pub index: String, // Index file to serve by default
//pub error: String, // Error file to serve when a file is not found
pub proxy_rules: HashMap<String, String>,
Expand All @@ -127,6 +128,7 @@ impl Config {
host: "localhost".to_string(),
root: "./".to_string(),
index: "index.html".to_string(),
log_file: None,
//error: "error.html".to_string(),
threads: 1,
cache: false,
Expand Down Expand Up @@ -166,6 +168,7 @@ impl Config {
cache: map.get2("cache").unwrap_or(false),
cache_ttl: map.get2("cache_ttl").unwrap_or(3600),
index: map.get2("index").unwrap_or("index.html".to_string()),
log_file: map.get2("log_file"),
//error: map.get2("error").unwrap_or("error.html".to_string()),
proxy_rules,
}
Expand Down
81 changes: 31 additions & 50 deletions src/hteapot/brew.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,16 @@
// Written by Alberto Ruiz 2024-04-08
// This is the HTTP client module, it will handle the requests and responses

use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::{TcpStream, ToSocketAddrs};
use std::time::Duration;

use super::methods::HttpMethod;
use super::request::HttpRequest;
use super::response::HttpResponse;
// use super::status::HttpStatus;
// use std::net::{IpAddr, Ipv4Addr, SocketAddr};

impl HttpRequest {
pub fn new(method: HttpMethod, path: &str) -> HttpRequest {
let path = path.to_string();
HttpRequest {
method,
path,
args: HashMap::new(),
headers: HashMap::new(),
body: String::new(),
}
}

pub fn arg(&mut self, key: &str, value: &str) -> &mut HttpRequest {
self.args.insert(key.to_string(), value.to_string());
return self;
Expand All @@ -34,11 +21,6 @@ impl HttpRequest {
return self;
}

pub fn body(&mut self, body: String) -> &mut HttpRequest {
self.body = body;
return self;
}

pub fn to_string(&self) -> String {
let path = if self.args.is_empty() {
self.path.clone()
Expand All @@ -62,14 +44,14 @@ impl HttpRequest {
for (k, v) in self.headers.iter() {
result.push_str(format!("{}: {}\r\n", k, v).as_str());
}
if !self.body.is_empty() {
result.push_str(self.body.as_str());
}

result.push_str(self.text().unwrap_or(String::new()).as_str());

result.push_str("\r\n\r\n");
result
}

pub fn brew(&self, addr: &str) -> Result<HttpResponse, &'static str> {
pub fn brew(&self, addr: &str) -> Result<Box<HttpResponse>, &'static str> {
let mut addr = addr.to_string();
if addr.starts_with("http://") {
addr = addr.strip_prefix("http://").unwrap().to_string();
Expand Down Expand Up @@ -97,11 +79,11 @@ impl HttpRequest {
let mut raw: Vec<u8> = Vec::new();
let _ = stream.read_to_end(&mut raw);

Ok(HttpResponse::new_raw(raw))
Ok(Box::new(HttpResponse::new_raw(raw)))
}
}

pub fn brew(direction: &str, request: HttpRequest) -> Result<HttpResponse, &'static str> {
pub fn brew(direction: &str, request: &mut HttpRequest) -> Result<Box<HttpResponse>, &'static str> {
return request.brew(direction);
}

Expand All @@ -111,16 +93,16 @@ pub fn brew(direction: &str, request: HttpRequest) -> Result<HttpResponse, &'sta

#[cfg(test)]
mod tests {
use super::super::methods::HttpMethod;
use super::*;

#[test]
fn test_http_request_new() {
let request = HttpRequest::new(HttpMethod::GET, "/example");
assert_eq!(request.method, HttpMethod::GET);
assert_eq!(request.path, "/example");
assert!(request.args.is_empty());
assert!(request.headers.is_empty());
assert_eq!(request.body, "");
assert_eq!(request.text(), None);
}

#[test]
Expand All @@ -140,40 +122,39 @@ mod tests {
);
}

#[test]
fn test_http_request_body() {
let mut request = HttpRequest::new(HttpMethod::POST, "/upload");
request.body("Test body content".to_string());
assert_eq!(request.body, "Test body content");
}
// #[test]
// fn test_http_request_body() {
// let mut request = HttpRequest::new(HttpMethod::POST, "/upload");
// request.body("Test body content".to_string());
// assert_eq!(request.body, "Test body content");
// }

#[test]
fn test_http_request_to_string() {
let mut request = HttpRequest::new(HttpMethod::POST, "/resource");
request
.header("Content-Type", "application/json")
.body("{\"data\":\"test\"}".to_string());
request.header("Content-Type", "application/json");
//.body("{\"data\":\"test\"}".to_string());

let request_string = request.to_string();
assert!(request_string.contains("POST /resource HTTP/1.1"));
assert!(request_string.contains("Content-Type: application/json"));
assert!(request_string.contains("{\"data\":\"test\"}"));
//assert!(request_string.contains("{\"data\":\"test\"}"));
}

#[test]
fn test_http_request_to_string_with_args() {
let mut request = HttpRequest::new(HttpMethod::POST, "/resource");
let _ = request
.header("Content-Type", "application/json")
.arg("key", "value")
.body("{\"data\":\"test\"}".to_string())
.brew("localhost:8080");

let request_string = request.to_string();
assert!(request_string.contains("POST /resource?key=value HTTP/1.1"));
assert!(request_string.contains("Content-Type: application/json"));
assert!(request_string.contains("{\"data\":\"test\"}"));
}
// #[test]
// fn test_http_request_to_string_with_args() {
// let mut request = HttpRequest::new(HttpMethod::POST, "/resource");
// let _ = request
// .header("Content-Type", "application/json")
// .arg("key", "value")
// .body("{\"data\":\"test\"}".to_string())
// .brew("localhost:8080");

// let request_string = request.to_string();
// assert!(request_string.contains("POST /resource?key=value HTTP/1.1"));
// assert!(request_string.contains("Content-Type: application/json"));
// assert!(request_string.contains("{\"data\":\"test\"}"));
// }

#[test]
fn test_http_request() {
Expand Down
Loading