-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
312 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
[HTEAPOT] | ||
port = 8081 | ||
host = "0.0.0.0" | ||
host = "0.0.0.0" | ||
root = "public" | ||
threads = 5 | ||
cache = true | ||
cache_ttl = 3600 | ||
cache_ttl = 3600 | ||
[proxy] | ||
"/test" = "http://example.com" | ||
"/google" = "http://google.com" | ||
# "/" = "http://ifconfig.co" # this will override all the proxys and local request | ||
# "/" = "http://ifconfig.co" # this will override all the proxys and local request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,74 @@ | ||
// Written by Alberto Ruiz 2024-04-08 | ||
// This is the HTTP client module, it will handle the requests and responses | ||
|
||
use std::{io::{Read, Write}, net::TcpStream, vec}; | ||
|
||
use std::{ | ||
io::{Read, Write}, | ||
net::TcpStream, | ||
}; | ||
|
||
struct Url { | ||
scheme: String, | ||
domain: String, | ||
path: String, | ||
port: String | ||
scheme: String, | ||
domain: String, | ||
path: String, | ||
port: String, | ||
} | ||
|
||
fn parse_url(url: &str) -> Result<Url, &str> { | ||
let url_parts = url.split(":").collect::<Vec<&str>>(); | ||
let prefix = url_parts[0]; | ||
let domain_path = url_parts[1].trim_start_matches("//"); | ||
let port = if url_parts.len() == 3 { | ||
url_parts[2] | ||
} else { | ||
match prefix { | ||
"tea" => "1234", | ||
"https" => "443", | ||
"http" => "80", | ||
_ => "80", | ||
} | ||
}; | ||
let (domain, path) = domain_path.split_once('/').unwrap(); | ||
Ok(Url { | ||
scheme: prefix.to_string(), | ||
domain: domain.to_string(), | ||
path: path.to_string(), | ||
port: port.to_string(), | ||
}) | ||
} | ||
|
||
fn parse_url(url: &str) -> Result<Url,&str> { | ||
let url_parts = url.split(":").collect::<Vec<&str>>(); | ||
let prefix = url_parts[0]; | ||
let domain_path = url_parts[1].trim_start_matches("//"); | ||
let port = if url_parts.len() == 3 { | ||
url_parts[2] | ||
} else { | ||
match prefix { | ||
"https" => "443", | ||
"http" => "80", | ||
_ => "80" | ||
pub fn fetch(url: &str) -> Result<Vec<u8>, &str> { | ||
let url = parse_url(url); | ||
if url.is_err() { | ||
return Err("Error parsing url"); | ||
} | ||
let url = url.unwrap(); | ||
if url.scheme == "https" { | ||
return Err("not supported yet"); | ||
} | ||
}; | ||
let (domain,path) = domain_path.split_once('/').unwrap(); | ||
Ok(Url { | ||
scheme: prefix.to_string(), | ||
domain: domain.to_string(), | ||
path: path.to_string(), | ||
port: port.to_string() | ||
}) | ||
} | ||
|
||
pub fn fetch(url: &str) -> Result<Vec<u8>,&str> { | ||
let url = parse_url(url); | ||
if url.is_err() { return Err("Error parsing url")} | ||
let url = url.unwrap(); | ||
if url.scheme == "https" { | ||
return Err("not supported yet"); | ||
} | ||
|
||
let client = TcpStream::connect(format!("{}:{}",url.domain,url.port)); | ||
if client.is_err() { | ||
return Err("Error fetching"); | ||
} | ||
let mut client = client.unwrap(); | ||
let http_request = format!("GET /{} HTTP/1.1\r\nHost: {}\r\n\r\n",url.path, url.domain); | ||
client.write(http_request.as_bytes()).unwrap(); | ||
let mut response = String::new(); | ||
let mut full_buffer: Vec<u8> = Vec::new(); | ||
let mut buffer = [0; 1024]; | ||
loop { | ||
match client.read(&mut buffer) { | ||
Ok(0) => break, | ||
Ok(n) => { | ||
if n == 0 {break;} | ||
full_buffer.extend(buffer.iter().cloned()); | ||
if buffer.last().unwrap() == &0 {break;} | ||
}, | ||
Err(_) => break | ||
} | ||
} | ||
Ok(full_buffer) | ||
let client = TcpStream::connect(format!("{}:{}", url.domain, url.port)); | ||
if client.is_err() { | ||
return Err("Error fetching"); | ||
} | ||
let mut client = client.unwrap(); | ||
let http_request = format!("GET /{} HTTP/1.1\r\nHost: {}\r\n\r\n", url.path, url.domain); | ||
client.write(http_request.as_bytes()).unwrap(); | ||
let mut full_buffer: Vec<u8> = Vec::new(); | ||
let mut buffer = [0; 1024]; | ||
loop { | ||
match client.read(&mut buffer) { | ||
Ok(0) => break, | ||
Ok(n) => { | ||
if n == 0 { | ||
break; | ||
} | ||
full_buffer.extend(buffer.iter().cloned()); | ||
if buffer.last().unwrap() == &0 { | ||
break; | ||
} | ||
} | ||
Err(_) => break, | ||
} | ||
} | ||
Ok(full_buffer) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.