Code Dump
This commit is contained in:
parent
4c6f42251d
commit
7b5f7c5d4b
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "miniweb"
|
||||
version = "0.1.0"
|
||||
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "miniweb"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@ -1 +1,3 @@
|
||||
# miniweb
|
||||
A light weight rust web server I am working on for a bit of fun.
|
||||
FOR EDUCATION PURPOSE ONLY, NOT RECCOMENDED FOR USE AT THIS TIME.
|
||||
3
index.html
Normal file
3
index.html
Normal file
@ -0,0 +1,3 @@
|
||||
<html>
|
||||
<h1>Hello World</h1>
|
||||
</html>
|
||||
59
src/main.rs
Normal file
59
src/main.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::{println, io::{Read}};
|
||||
// use std::time::Duration;
|
||||
mod methods;
|
||||
|
||||
fn method_handler(mut stream: TcpStream) {
|
||||
let mut packet = [0; 65535];
|
||||
let bytes_read = stream.read(&mut packet[..]);
|
||||
match bytes_read {
|
||||
Ok(n) if n >= 7 => {
|
||||
if packet[0..3] == [0x47, 0x45, 0x54] {
|
||||
println!("GET Method");
|
||||
|
||||
methods::get::handler(stream, &packet);
|
||||
|
||||
} else if packet[0..4] == [0x48, 0x45, 0x41, 0x44] {
|
||||
println!("HEAD Method");
|
||||
} else if packet[0..6] == [0x4F, 0x50, 0x49, 0x4F, 0x4E, 0x53] {
|
||||
println!("OPTIONS Method");
|
||||
} else if packet[0..5] == [0x54, 0x52, 0x41, 0x43, 0x45] {
|
||||
println!("TRACE Method");
|
||||
} else if packet[0..3] == [0x50, 0x55, 0x54] {
|
||||
println!("PUT Method");
|
||||
} else if packet[0..6] == [0x44, 0x45, 0x4C, 0x45, 0x54, 0x45] {
|
||||
println!("DELETE Method");
|
||||
} else if packet[0..4] == [0x50, 0x4F, 0x53, 0x54] {
|
||||
println!("POST Method");
|
||||
} else if packet[0..5] == [0x50, 0x41, 0x54, 0x43, 0x48] {
|
||||
println!("PATCH Method");
|
||||
} else if packet[0..7] == [0x43, 0x4F, 0x4E, 0x4E, 0x45, 0x43, 0x54] {
|
||||
println!("CONNECT method");
|
||||
} else {
|
||||
println!("Unknown Method");
|
||||
}
|
||||
},
|
||||
Ok(_) => {
|
||||
println!("Packet appears too small");
|
||||
},
|
||||
Err(_e) => {
|
||||
println!("No data recieved");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let listener = TcpListener::bind("0.0.0.0:80").unwrap();
|
||||
|
||||
for stream in listener.incoming() {
|
||||
match stream {
|
||||
Ok(stream) => {
|
||||
method_handler(stream);
|
||||
}
|
||||
Err(_e) => {
|
||||
println!("Couldn't conenct to client...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/methods.rs
Normal file
13
src/methods.rs
Normal file
@ -0,0 +1,13 @@
|
||||
enum Methods{
|
||||
GET,
|
||||
// HEAD,
|
||||
// OPTIONS,
|
||||
// TRACE,
|
||||
// PUT,
|
||||
// DELETE,
|
||||
// POST,
|
||||
// PATCH,
|
||||
// CONNECT,
|
||||
}
|
||||
|
||||
pub mod get;
|
||||
59
src/methods/get.rs
Normal file
59
src/methods/get.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use std::net::TcpStream;
|
||||
use std::io::{Write};
|
||||
use super::Methods;
|
||||
use std::fs;
|
||||
|
||||
struct Packet {
|
||||
method: Methods,
|
||||
path: String,
|
||||
protocol_version: String,
|
||||
}
|
||||
|
||||
pub fn handler(stream: TcpStream, binary_packet: &[u8]) {
|
||||
let string_packet = String::from_utf8_lossy(&binary_packet);
|
||||
let packet_breakdown: Vec<&str> = string_packet.lines().collect();
|
||||
let mut header = packet_breakdown[0].split_whitespace();
|
||||
|
||||
let _ = header.next();
|
||||
|
||||
|
||||
let this_packet = Packet {
|
||||
method: Methods::GET,
|
||||
path: String::from(header.next().unwrap_or("/")),
|
||||
protocol_version: String::from(header.next().unwrap_or("/")),
|
||||
};
|
||||
|
||||
println!("{}",string_packet);
|
||||
|
||||
response(stream, this_packet);
|
||||
}
|
||||
|
||||
fn response(mut stream: TcpStream, this_packet: Packet){
|
||||
let mut content = Vec::new();
|
||||
let code = get_content(this_packet.path, &mut content);
|
||||
match code{
|
||||
200 => { stream.write(&content); },
|
||||
404 => { stream.write("404 Page not found".as_bytes()); },
|
||||
_ => println!("Unknown Error Code"),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
fn get_content(path: String, content: &mut Vec<u8>) -> i32 {
|
||||
let mut this_path = ".".to_string() + &path;
|
||||
if this_path == "./" {
|
||||
this_path = "./index.html".to_string();
|
||||
}
|
||||
|
||||
let data = match fs::read(this_path) {
|
||||
Ok(vec) => vec,
|
||||
Err(e) => return 404
|
||||
};
|
||||
|
||||
content.extend_from_slice(data.as_slice());
|
||||
return 200;
|
||||
}
|
||||
|
||||
// fn gen_header(){
|
||||
//
|
||||
// }
|
||||
0
src/methods/test.html
Normal file
0
src/methods/test.html
Normal file
Loading…
x
Reference in New Issue
Block a user