|
|
|
@ -1,9 +1,16 @@
|
|
|
|
|
mod routes;
|
|
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::net::{TcpListener, TcpStream};
|
|
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use std::str;
|
|
|
|
|
|
|
|
|
|
use structopt::StructOpt;
|
|
|
|
|
use threadpool::ThreadPool;
|
|
|
|
|
use http::request::Request;
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use anyhow::Context;
|
|
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
|
struct Opt {
|
|
|
|
@ -17,8 +24,53 @@ struct Opt {
|
|
|
|
|
max_storage: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle(stream: TcpStream) {
|
|
|
|
|
todo!();
|
|
|
|
|
// very specific
|
|
|
|
|
// TODO less retarded implementation
|
|
|
|
|
fn parse_request(text: &str) -> Result<Request<String>> {
|
|
|
|
|
let request = Request::builder();
|
|
|
|
|
|
|
|
|
|
let mut iter = text.lines();
|
|
|
|
|
let mut first = iter.nth(0).unwrap()
|
|
|
|
|
.split_whitespace();
|
|
|
|
|
|
|
|
|
|
let method = first.next().unwrap();
|
|
|
|
|
let uri = first.next().unwrap();
|
|
|
|
|
|
|
|
|
|
// now headers
|
|
|
|
|
let mut headers: Vec<(&str, &str)> = vec![];
|
|
|
|
|
|
|
|
|
|
for line in &mut iter {
|
|
|
|
|
println!("{}", line);
|
|
|
|
|
if line.is_empty() {break};
|
|
|
|
|
let line: Vec<&str> = line.split(":").take(2).collect();
|
|
|
|
|
headers.push((line[0], line[1]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let body: String = iter.fold(String::from(""), |a, b| format!("{}{}", a, b));
|
|
|
|
|
|
|
|
|
|
let mut request = request
|
|
|
|
|
.method(method)
|
|
|
|
|
.uri(uri);
|
|
|
|
|
|
|
|
|
|
for header in headers {
|
|
|
|
|
request = request.header(header.0, header.1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
request.body(body).context("lmao")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn handle(mut stream: TcpStream) {
|
|
|
|
|
let ip = stream.peer_addr();
|
|
|
|
|
let mut buffer = [0; 1<<10]; // 2 to the power of 10
|
|
|
|
|
stream.read(&mut buffer).unwrap();
|
|
|
|
|
|
|
|
|
|
let text = str::from_utf8(&buffer)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.trim_matches(0 as char) // gets rid of null at the end of buffer
|
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
|
|
println!("{:?}", parse_request(text).unwrap());
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
@ -31,5 +83,4 @@ fn main() {
|
|
|
|
|
let stream = stream.unwrap();
|
|
|
|
|
pool.execute(|| handle(stream))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|