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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
use std::collections::HashMap;
use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use std::str;
use itertools::Itertools;
enum StatusCode {
// 2xx Success
Ok,
// Created = 201,
// Accepted = 202,
// NonAuthoritative = 203,
// NoContent = 204,
// ResetContent = 205,
// PartialContent = 206,
// 4xx Client Error
BadRequest,
NotFound,
}
#[derive(Debug)]
enum HTTPMethod {
Get(String),
Post(String),
Put(String),
}
impl From<StatusCode> for String {
fn from(val: StatusCode) -> Self {
use StatusCode::*;
match val {
Ok => "200 OK".to_string(),
BadRequest => "400 Bad Request".to_string(),
NotFound => "404 Not Found".to_string(),
}
}
}
impl From<HTTPMethod> for String {
fn from(val: HTTPMethod) -> Self {
use HTTPMethod::*;
match val {
Get(target) => "GET".to_string() + &target,
Post(target) => "POST".to_string() + &target,
Put(target) => "PUT".to_string() + &target,
}
}
}
#[derive(Debug)]
struct Headers(HashMap<String, String>);
impl From<&[&str]> for Headers {
fn from(value: &[&str]) -> Self {
let mut header_map = HashMap::new();
for header in value.iter().filter(|val| !val.is_empty()) {
let (key, val) = header.split_once(':').expect("Should be splitable by :");
header_map.insert(key.to_string(), val.to_string());
}
Headers(header_map)
}
}
impl From<&str> for HTTPMethod {
fn from(val: &str) -> Self {
use HTTPMethod::*;
let request_line = val.split(' ').collect_vec();
let (method, target) = (request_line[0], request_line[1]);
match method {
"GET" => Get(target.to_string()),
"POST" => Post(target.to_string()),
_ => {
eprintln!("{method} Not Supported Yet");
unreachable!()
}
}
}
}
#[derive(Debug)]
struct Request {
method: HTTPMethod,
headers: Option<Headers>,
body: Option<String>,
}
impl Request {
fn new(method: HTTPMethod, headers: Headers, body: String) -> Self {
let headers = if headers.0.len() == 0 {
None
} else {
Some(headers)
};
let body = if body.is_empty() { None } else { Some(body) };
Request {
method,
headers,
body,
}
}
}
impl From<&str> for Request {
fn from(val: &str) -> Self {
let request: Vec<&str> = val.split("\r\n").collect();
match &request[..] {
[request_line, headers @ .., body] => {
let (method, headers, body) = (
HTTPMethod::from(*request_line),
Headers::from(headers),
body.to_string(),
);
Request::new(method, headers, body)
}
_ => {
unreachable!();
}
}
}
}
struct Response {
version: String,
status: StatusCode,
body: Option<String>,
}
impl Response {
fn new(version: String, status: StatusCode, body: Option<String>) -> Self {
Response {
version,
status,
body,
}
}
}
impl Into<String> for Response {
fn into(self) -> String {
format!(
"HTTP/{} {}\r\n{}\r\n",
self.version,
String::from(self.status),
self.body.unwrap_or("".to_string())
)
}
}
fn handle_client(mut stream: TcpStream) {
// Buffer to store the data received from the client
let mut buffer = [0; 512];
// Read data from the stream
match stream.read(&mut buffer) {
Ok(_) => {
// Convert buffer to a string and print the received data
if let Ok(request) = str::from_utf8(&buffer) {
println!("Received request: {}", request);
let request = Request::from(request);
println!("Request after parsing: {:?}", request);
let succses: String = Response::new("1.1".to_string(), StatusCode::Ok, None).into();
let succses = succses.as_bytes();
let not_found: String =
Response::new("1.1".to_string(), StatusCode::NotFound, None).into();
let not_found = not_found.as_bytes();
match request.method {
HTTPMethod::Get(target) => match target.as_str() {
"/" => match stream.write(succses) {
Ok(_) => println!("Response sent successfully"),
Err(e) => eprintln!("Failed to send response: {}", e),
},
_ => match stream.write(not_found) {
Ok(_) => println!("Response sent successfully"),
Err(e) => eprintln!("Failed to send response: {}", e),
},
},
HTTPMethod::Post(target) => todo!(),
HTTPMethod::Put(target) => todo!(),
}
}
// Prepare a response
// let response = b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, world!";
//
}
Err(e) => {
eprintln!("Failed to read from stream: {}", e);
}
}
}
fn main() {
let listener = TcpListener::bind("127.0.0.1:4221").unwrap();
for stream in listener.incoming() {
match stream {
Ok(stream) => {
// println!("accepted new connection");
handle_client(stream)
}
Err(e) => {
eprintln!("error: {}", e);
}
}
}
}
|