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() {
|
fn main() {
|
||||||
let args: Vec<String> = env::args().collect();
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
let host = if args.len() < 2 {
|
let host = if args.len() < 2 {
|
||||||
"127.0.0.1:6666"
|
"127.0.0.1:55566"
|
||||||
} else {
|
} else {
|
||||||
&args[1]
|
&args[1]
|
||||||
};
|
};
|
||||||
|
@ -25,23 +27,14 @@ fn main() {
|
||||||
|
|
||||||
|
|
||||||
fn handle_connection(mut stream: TcpStream) {
|
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();
|
let size = stream.read(&mut buffer).unwrap();
|
||||||
buffer.resize(size, 0);
|
buffer.resize(size, 0);
|
||||||
|
|
||||||
let contents = fs::read_to_string("src/main.rs").unwrap() + "\n\n";
|
send_file(&mut stream, "src/main.rs");
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
let peer_addr = stream.peer_addr().unwrap();
|
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)
|
String::from_utf8_lossy(&buffer)
|
||||||
.escape_debug()
|
.escape_debug()
|
||||||
.collect::<String>()
|
.collect::<String>()
|
||||||
|
@ -49,3 +42,15 @@ fn handle_connection(mut stream: TcpStream) {
|
||||||
.replace("\\n", "\n")
|
.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