feat: get to a normal reverse proxy

This commit is contained in:
javalsai 2025-07-02 02:31:41 +02:00
parent 4fc49dd3b8
commit 3c27eb55d7
Signed by: javalsai
SSH Key Fingerprint: SHA256:3G83yKhBUWVABVX/vPWH88xnK4+ptMtHkZGCRXD4Mk8
9 changed files with 829 additions and 1 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target
config.toml

408
Cargo.lock generated
View File

@ -2,6 +2,414 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "anstream"
version = "0.6.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "301af1932e46185686725e0fad2f8f2aa7da69dd70bf6ecc44d6b703844a3933"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "862ed96ca487e809f1c8e5a8447f6ee2cf102f846893800b20cebdf541fc6bbd"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8bdeb6047d8983be085bab0ba1472e6dc604e7041dbf6fcd5e71523014fae9"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "403f75924867bb1033c59fbf0797484329750cfbe3c4325cd33127941fabc882"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "clap"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40b6887a1d8685cebccf115538db5c0efe625ccac9696ad45c409d96566e910f"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0c66c08ce9f0c698cbce5c0279d0bb6ac936d8674174fe48f736533b964f59e"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2c7947ae4cc3d851207c1adb5b5e260ff0cca11446b1d6d1423788e442257ce"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "ctrlc"
version = "3.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46f93780a459b7d656ef7f071fe699c4d3d2cb201c4b24d085b6ddc505276e73"
dependencies = [
"nix",
"windows-sys",
]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "hashbrown"
version = "0.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "httparse"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
[[package]]
name = "indexmap"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe4cd85333e22411419a0bcae1297d25e58c9443848b11dc6a86fefe8c78a661"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "memchr"
version = "2.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
[[package]]
name = "nix"
version = "0.30.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "once_cell_polyfill"
version = "1.70.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4895175b425cb1f87721b59f0f286c2092bd4af812243672510e1ac53e2e0ad"
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_spanned"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
dependencies = [
"serde",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "toml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"toml_edit",
]
[[package]]
name = "toml_datetime"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
dependencies = [
"serde",
]
[[package]]
name = "toml_edit"
version = "0.22.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
dependencies = [
"indexmap",
"serde",
"serde_spanned",
"toml_datetime",
"toml_write",
"winnow",
]
[[package]]
name = "toml_write"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
[[package]]
name = "trpha"
version = "0.1.0"
dependencies = [
"anstyle",
"clap",
"ctrlc",
"httparse",
"serde",
"shlex",
"toml",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "winnow"
version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74c7b26e3480b707944fc872477815d29a8e429d2f93a1ce000f5fa84a15cbcd"
dependencies = [
"memchr",
]

View File

@ -4,3 +4,14 @@ version = "0.1.0"
edition = "2024"
[dependencies]
anstyle = "1"
clap = { version = "4", features = ["derive", "string"] }
ctrlc = "3"
httparse = "1"
serde = { version = "1", features = ["derive"] }
shlex = "1"
toml = "0"
[features]
default = ["ipc"]
ipc = []

4
config.default.toml Normal file
View File

@ -0,0 +1,4 @@
listen_at = "127.0.0.1:8080"
[hosts]
"git.tuxcord.net" = "127.0.0.1:3000"

2
rust-toolchain.toml Normal file
View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

61
src/args/mod.rs Normal file
View File

@ -0,0 +1,61 @@
use std::path::PathBuf;
use clap::{Parser, Subcommand};
/// Tuxcord Reverse Proxy Header Authenthication
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
#[command(styles = get_clap_styles())]
pub struct Args {
/// Path to the config file
#[arg(short, default_value = "./config.toml")]
pub config: PathBuf,
#[clap(subcommand)]
pub command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Connects to the selected IPC
#[cfg(feature = "ipc")]
Ipc,
}
fn get_clap_styles() -> clap::builder::Styles {
clap::builder::Styles::styled()
.usage(
anstyle::Style::new()
.bold()
.underline()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
)
.header(
anstyle::Style::new()
.bold()
.underline()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Green))),
)
.literal(
anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
)
.invalid(
anstyle::Style::new()
.bold()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red))),
)
.error(
anstyle::Style::new()
.bold()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Red))),
)
.valid(
anstyle::Style::new()
.bold()
.underline()
.fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Yellow))),
)
.placeholder(
anstyle::Style::new().fg_color(Some(anstyle::Color::Ansi(anstyle::AnsiColor::Blue))),
)
}

11
src/config/mod.rs Normal file
View File

@ -0,0 +1,11 @@
use std::{collections::HashMap, net::SocketAddr, path::PathBuf, sync::RwLock};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Schema {
pub listen_at: SocketAddr,
#[cfg(feature = "ipc")]
pub ipc: Option<PathBuf>,
pub hosts: RwLock<HashMap<String, SocketAddr>>,
}

186
src/ipc/mod.rs Normal file
View File

@ -0,0 +1,186 @@
use core::net;
use std::{
fs,
io::{self, BufRead, BufReader, Write, stdout},
os::unix::net::{UnixListener, UnixStream},
process,
sync::{Arc, RwLockWriteGuard},
thread::{self, JoinHandle},
};
use crate::config;
pub fn start_client(config: config::Schema) {
let mut stream = UnixStream::connect(config.ipc.expect("no ipc socket specified"))
.expect("failed to connect to unix socket");
let stream2 = stream.try_clone().expect("failed to clone stream");
let mut reader = BufReader::new(stream2);
let stdin = io::stdin();
print!("> ");
_ = stdout().flush();
for line in stdin.lines() {
let Ok(line) = line else {
break;
};
_ = stream.write_all(line.as_bytes());
_ = stream.write_all(b"\n");
for line in (&mut reader).lines() {
let Ok(line) = line else {
return;
};
if line == "EOF" {
break;
}
println!("{line}");
}
print!("> ");
_ = stdout().flush();
}
}
pub fn handle_daemon_client(mut stream: UnixStream, config: Arc<config::Schema>) {
let Ok(stream2) = stream.try_clone() else {
return;
};
for line in BufReader::new(stream2).lines() {
let Ok(line) = line else {
return;
};
let args = shlex::Shlex::new(line.trim()).collect::<Vec<_>>();
// i hate it too
if let [arg0, args @ ..] = &args[..] {
match arg0.as_str() {
"help" => {
_ = writeln!(
stream,
"avaliable commands:\n help\n configcount\n confdump\n hosts <list|create|delete> ..."
);
}
"configcount" => {
_ = writeln!(
stream,
"weak {}, strong {}",
Arc::weak_count(&config),
Arc::strong_count(&config)
);
}
"confdump" => {
_ = writeln!(stream, "{:#?}", config.as_ref());
}
"hosts" => {
if let [arg0, args @ ..] = args {
match arg0.as_str() {
"list" => {
_ = writeln!(stream, "aquiring read lock");
let rlock = config.hosts.read();
match rlock {
Ok(lock) => {
_ = writeln!(stream, "{lock:#?}");
}
Err(err) => {
_ = writeln!(stream, "err with read lock: {err:?}");
}
};
}
"create" => {
if let [name, value] = args {
match value.parse::<net::SocketAddr>() {
Ok(addr) => {
_ = writeln!(stream, "aquiring write lock");
let wlock = config.hosts.write();
match wlock {
Ok(mut lock) => {
lock.insert(name.to_string(), addr);
let lock = RwLockWriteGuard::downgrade(lock);
_ = writeln!(stream, "{lock:#?}");
}
Err(err) => {
_ = writeln!(
stream,
"err with write lock: {err:?}"
);
}
};
}
Err(err) => {
_ = writeln!(
stream,
"err: parsing value as socket addres {err:?}"
);
}
};
} else {
_ = writeln!(
stream,
"invalid arg count, expected name and value"
);
}
}
"delete" => {
if let [name] = args {
_ = writeln!(stream, "aquiring write lock");
let wlock = config.hosts.write();
match wlock {
Ok(mut lock) => {
lock.remove(name);
let lock = RwLockWriteGuard::downgrade(lock);
_ = writeln!(stream, "{lock:#?}");
}
Err(err) => {
_ = writeln!(stream, "err with write lock: {err:?}");
}
};
} else {
_ = writeln!(stream, "invalid arg count, expected a name");
}
}
_ => _ = writeln!(stream, "err: invalid argument {arg0:?}"),
}
} else {
_ = writeln!(stream, "err: not enough arguments");
};
}
_ => {
_ = writeln!(stream, "command {arg0:?} doesn't exist, type \"help\"");
}
};
};
_ = stream.write_all(b"EOF\n"); // BEL char
}
}
pub fn handle_daemon(config: Arc<config::Schema>) -> Option<JoinHandle<()>> {
if let Some(ipc_path) = config.ipc.as_ref() {
println!("starting ipc daemon at {ipc_path:#?}");
let listener = UnixListener::bind(ipc_path.clone()).expect("failed to bind to ipc socket");
let ipc_path_clone = ipc_path.clone();
ctrlc::try_set_handler(move || {
_ = fs::remove_file(&ipc_path_clone);
process::exit(2);
})
.expect("failed to set exit handler");
println!("ipc daemon started");
Some(thread::spawn(move || {
for stream in listener.incoming() {
match stream {
Ok(stream) => {
handle_daemon_client(stream, config.clone());
}
Err(err) => eprintln!("ipc daemon error: {err:#?}"),
}
}
}))
} else {
None
}
}

View File

@ -1,5 +1,149 @@
//! # Tuxcord Reverse Proxy Header Authenthication
#![feature(rwlock_downgrade, try_blocks)]
use std::{
fs::File,
io::{self, Read, Write},
net::{Shutdown, TcpListener, TcpStream},
sync::Arc,
thread,
};
use clap::Parser;
pub mod args;
pub mod config;
#[cfg(feature = "ipc")]
pub mod ipc;
use args::Args;
const DEFAULT_CONFIG: &[u8] = include_bytes!("../config.default.toml");
fn main() {
println!("Hello, world!");
let args = Args::parse();
if !args.config.exists() {
println!(
"{:?} doesn't exist, creating a default config",
&args.config
);
File::create(args.config)
.expect("failure creating the config file")
.write_all(DEFAULT_CONFIG)
.expect("failure writting the contents to the config file");
return;
}
let mut config_file = File::open(&args.config).expect("failure opening the config file");
let mut config = String::new();
config_file
.read_to_string(&mut config)
.expect("failure reading the config file");
let config: config::Schema = toml::from_str(&config).expect("invalid config file");
#[cfg(feature = "ipc")]
if let Some(args::Commands::Ipc) = args.command {
ipc::start_client(config);
return;
}
println!("config: {config:#?}");
let listener = TcpListener::bind(config.listen_at).expect("failure tcp listening");
let config_arc = Arc::new(config); // will also serve as a counter
#[cfg(feature = "ipc")]
ipc::handle_daemon(config_arc.clone());
for stream in listener.incoming() {
match stream {
Ok(mut client) => {
let config_arc = config_arc.clone();
thread::spawn(|| {
if let Err(err) = handle_client(&mut client, config_arc.as_ref()) {
eprintln!("err: invalid req head ({err:?}), closing...");
_ = client.shutdown(Shutdown::Both);
}
drop(client);
drop(config_arc);
});
}
Err(err) => eprintln!("error with an incoming listener {err:#?}"),
}
}
unreachable!("listener had to be killed unexpectedly");
}
fn handle_client(client: &mut TcpStream, config: &config::Schema) -> Result<(), &'static str> {
let mut header_buf = [0u8; 1024 * 8];
let mut read_pos = 0usize;
let pos = loop {
let Ok(n) = client.read(&mut header_buf[read_pos..]) else {
return Err("error reading stream");
};
read_pos += n;
let pos = header_buf
.windows(b"\r\n\r\n".len())
.position(|w| w == b"\r\n\r\n");
if let Some(pos) = pos {
break pos;
};
};
let mut headers = [httparse::EMPTY_HEADER; 16];
let mut req = httparse::Request::new(&mut headers);
let Ok(httparse::Status::Complete(_)) =
httparse::Request::parse(&mut req, &header_buf[0..(pos + b"\r\n\r\n".len())])
else {
return Err("parsing request was not complete");
};
let theres_body = req
.headers
.iter()
.any(|header| header.name.to_lowercase() == "content-length")
|| req
.headers
.iter()
.any(|header| header.name.to_lowercase() == "transfer-encoding");
let Some(header) = req
.headers
.iter()
.find(|header| header.name.to_lowercase() == "host")
else {
return Err("failed to find \"host\" header");
};
let Ok(host_header) = String::from_utf8(header.value.to_vec()) else {
return Err("\"host\" header is not valid UTF-8");
};
// Now find that header and pas everything
let Ok(read_hosts) = config.hosts.read() else {
return Err("poisoned RwLock");
};
let Some(addr) = read_hosts.get(&host_header) else {
return Err("host not in hashmap");
};
let Ok(mut stream) = TcpStream::connect(addr) else {
return Err("failed to connect to the hashmap address");
};
drop(read_hosts);
let Ok(_): io::Result<()> = (try {
stream.write_all(&header_buf[0..pos])?;
// here we sent all original headers, append our own (TODO)
stream.write_all(&header_buf[pos..read_pos])?; // send our overhead
if theres_body {
io::copy(client, &mut stream)?;
}
io::copy(&mut stream, client)?;
}) else {
return Err("io error exchanging head and/or body");
};
Ok(())
}