cleanup
This commit is contained in:
parent
fdbd3903ce
commit
3683f37943
1 changed files with 20 additions and 15 deletions
33
src/main.rs
33
src/main.rs
|
@ -1,11 +1,13 @@
|
|||
use std::{net::{TcpStream, TcpListener}, io::prelude::*, fs, env};
|
||||
use std::io::prelude::*;
|
||||
use std::{fs, env};
|
||||
use std::net::{TcpStream, TcpListener};
|
||||
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
let host = if args.len() < 2 {
|
||||
"127.0.0.1:6666"
|
||||
"127.0.0.1:55566"
|
||||
} else {
|
||||
&args[1]
|
||||
};
|
||||
|
@ -25,23 +27,14 @@ fn main() {
|
|||
|
||||
|
||||
fn handle_connection(mut stream: TcpStream) {
|
||||
let mut buffer = vec![0; 1024];
|
||||
let mut buffer = vec![0; 2048];
|
||||
let size = stream.read(&mut buffer).unwrap();
|
||||
buffer.resize(size, 0);
|
||||
|
||||
let contents = fs::read_to_string("src/main.rs").unwrap() + "\n\n";
|
||||
|
||||
let response = format!(
|
||||
"HTTP/1.1 200 OK\nContent-Length: {}\n\n{}",
|
||||
contents.len() + buffer.len(),
|
||||
contents
|
||||
);
|
||||
stream.write_all(response.as_bytes()).unwrap();
|
||||
stream.write_all(&buffer).unwrap();
|
||||
stream.flush().unwrap();
|
||||
send_file(&mut stream, "src/main.rs");
|
||||
|
||||
let peer_addr = stream.peer_addr().unwrap();
|
||||
println!("Received {} bytes from {}\n\n{}\n", size, peer_addr,
|
||||
println!("Received {} bytes from {}\n\n[{}]\n", size, peer_addr,
|
||||
String::from_utf8_lossy(&buffer)
|
||||
.escape_debug()
|
||||
.collect::<String>()
|
||||
|
@ -49,3 +42,15 @@ fn handle_connection(mut stream: TcpStream) {
|
|||
.replace("\\n", "\n")
|
||||
);
|
||||
}
|
||||
|
||||
fn send_file(stream: &mut TcpStream, path: &str) {
|
||||
let contents = fs::read_to_string(path).unwrap() + "\n\n";
|
||||
|
||||
let response = format!(
|
||||
"HTTP/1.1 200 OK\nContent-Length: {}\n\n{}",
|
||||
contents.len(),
|
||||
contents
|
||||
);
|
||||
stream.write_all(response.as_bytes()).unwrap();
|
||||
stream.flush().unwrap();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue