-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
108 lines (97 loc) · 4.13 KB
/
main.rs
File metadata and controls
108 lines (97 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use std::fs::File;
use std::boxed::Box;
use std::io::{Read, Write};
use std::thread;
use std::net::{TcpListener}; // TcpStream
use std::str::from_utf8;
// 编译
// brew install rust
// rustc main.rs
// ./main
static SERVER_ERROR: &'static str = "HTTP/1.1 500 Server Error\r\n\
Server: Rust\r\n\
Content-Type: text/html\r\n\
Content-Length: 18\r\n\r\n\
Server Error - 500";
fn main() {
let address = "0.0.0.0:8000";
let listener = TcpListener::bind(address).unwrap();
println!("HTTP Server running on {}", address);
// index.html
// let response = match File::open("index.html") {
// Ok(mut file) => {
// let mut body = String::new();
// let _ = file.read_to_string(&mut body);
// format!("HTTP/1.1 200 OK\r\n\
// Server: Rust/1.0\r\n\
// Content-Type: text/html\r\n\
// Content-Length: {}\r\n\r\n\
// {}", body.len(), body)
// },
// Err(e) => {
// SERVER_ERROR.to_string()
// }
// };
// let body = Box::new(response);
// accept connections and process them, spawning a new thread for each one
for stream in listener.incoming() {
// let body_clone = body.clone();
match stream {
Ok(mut stream) => {
thread::spawn(move|| {
let mut buff = [0u8; 4096];
match stream.read(&mut buff){
Ok(size) => {
let request = from_utf8(&buff[0..size]).unwrap();
if request.starts_with("GET /assets") {
let filename = ".".to_string()
+ request.split(" ").collect::<Vec<&str>>()[1];
println!("Request: GET {}", filename);
let content = match File::open(&filename) {
Ok(mut file) => {
let mut body = String::new();
let _ = file.read_to_string(&mut body);
body
},
Err(e) => {
println!("Error: {:?}", e);
stream.write_all(SERVER_ERROR.as_bytes());
return;
}
};
stream.write_all(content.as_bytes());
return;
}
println!("{:?} -> {:?}", stream.peer_addr().unwrap(),
stream.local_addr().unwrap() );
let index_html = match File::open("index.html") {
Ok(mut file) => {
let mut body = String::new();
let _ = file.read_to_string(&mut body);
format!("HTTP/1.1 200 OK\r\n\
Server: Rust/1.0\r\n\
Content-Type: text/html\r\n\
Content-Length: {}\r\n\r\n\
{}", body.len(), body)
},
Err(e) => {
SERVER_ERROR.to_string()
}
};
stream.write_all(index_html.as_bytes());
},
Err(e) => {
println!("Error: {:?}", e);
stream.write_all(SERVER_ERROR.as_bytes());
}
};
});
}
Err(e) => {
println!("Error: connection failed {:?}", e);
}
}
}
// close the socket server
drop(listener);
}