initial commit
This commit is contained in:
commit
8ff1aca4de
5 changed files with 58 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
/target
|
||||||
|
/Cargo.lock
|
6
Cargo.toml
Normal file
6
Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "whitehole"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
6
README.md
Normal file
6
README.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
# whitehole
|
||||||
|
A no-bullshit broccoli-like file hosting server.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
In development environment: `cargo r -- [targetdir]` where `[targetdir]` is the directory you want to host.
|
||||||
|
In production: `whitehole [targetdir]` where `[targetdir]` is the directory you want to host.
|
25
src/lib.rs
Normal file
25
src/lib.rs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
use std::path::Path;
|
||||||
|
use std::net::{TcpStream, Shutdown};
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
pub fn handle_client(mut connection: TcpStream, target_dir: &String) -> std::io::Result<()> {
|
||||||
|
let mut buffer = vec![0 as u8; 1024]; // Little bit of wiggle room.
|
||||||
|
connection.read(&mut buffer)?;
|
||||||
|
let header = String::from_utf8_lossy(&buffer).to_string();
|
||||||
|
println!("{header}");
|
||||||
|
if !header.starts_with("GET") {
|
||||||
|
connection.shutdown(Shutdown::Both)?;
|
||||||
|
println!("ion wanna hear it");
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
let client_desired_file_path = format!("{}/{}", target_dir, &header.split(" ").collect::<Vec<&str>>()[1][1..]); // Your code is not optimised if it doesn't make an inexperienced rustdev have a heart attack.
|
||||||
|
dbg!(&client_desired_file_path);
|
||||||
|
// let file_size: usize = fs::metadata(client_desired_file_path)?.len().try_into().unwrap();
|
||||||
|
// let mut buffer = vec![0 as u8; file_size]; // Nuke the buffer.
|
||||||
|
let mut response: Vec<u8> = String::from("HTTP/1.1 200 OK").into_bytes();
|
||||||
|
let mut file_contents = fs::read(&client_desired_file_path)?;
|
||||||
|
response.append(&mut file_contents);
|
||||||
|
connection.write(&response)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
19
src/main.rs
Normal file
19
src/main.rs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
use std::net;
|
||||||
|
use std::path::Path;
|
||||||
|
use whitehole::handle_client;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
let args = &std::env::args().collect::<Vec<String>>()[1..];
|
||||||
|
let target_dir = Path::new(&args[0]);
|
||||||
|
if !target_dir.is_dir() {
|
||||||
|
panic!("Target is not a directory");
|
||||||
|
}
|
||||||
|
|
||||||
|
let listener = net::TcpListener::bind("0.0.0.0:23898")?; // Alphanumerical for "WHIH" or
|
||||||
|
// "Whitehole". No idea why I did
|
||||||
|
// this.
|
||||||
|
for connection in listener.incoming() {
|
||||||
|
handle_client(connection?, &args[0])?; // See lib.rs for handle_client()
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in a new issue