-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.rs
More file actions
189 lines (156 loc) · 4.82 KB
/
Copy pathrequest.rs
File metadata and controls
189 lines (156 loc) · 4.82 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
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
use std::collections::HashMap;
use std::io::{BufRead, Read};
use std::str::SplitWhitespace;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Method {
Get,
Post,
Unknown,
}
impl Method {
pub fn as_str(&self) -> &'static str {
match self {
Method::Get => "GET",
Method::Post => "POST",
Method::Unknown => "",
}
}
}
impl From<&str> for Method {
fn from(value: &str) -> Self {
match value {
"GET" => Method::Get,
"POST" => Method::Post,
_ => Method::Unknown,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Version {
Http10,
Http11,
Unknown,
}
impl From<&str> for Version {
fn from(s: &str) -> Self {
match s {
"HTTP/1.0" => Version::Http10,
"HTTP/1.1" => Version::Http11,
_ => Version::Unknown,
}
}
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct HttpRequest {
pub method: Method,
pub version: Version,
pub path: String,
pub headers: HashMap<String, String>,
pub body: Vec<u8>,
pub keep_alive: bool,
}
const MAX_HTTP_LINE_SIZE: usize = 8 * 1024;
const MAX_HEADER_SIZE: usize = 8 * 1024;
const MAX_BODY_SIZE: usize = 10 * 1024 * 1024;
#[allow(dead_code)]
pub enum RequestParseError {
ConnectionClosed,
InvalidRequestLine,
HeadersTooLarge,
BodyTooLarge,
UnsupportedMethod,
UnsupportedVersion,
MalformedRequestLine,
Io(std::io::Error),
}
impl From<std::io::Error> for RequestParseError {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
pub type RequestResult<T> = Result<T, RequestParseError>;
fn parse_method(parts: &mut SplitWhitespace) -> RequestResult<Method> {
let method_str = parts.next().ok_or(RequestParseError::InvalidRequestLine)?;
let method = Method::from(method_str);
if method == Method::Unknown {
return Err(RequestParseError::UnsupportedMethod);
}
Ok(method)
}
fn parse_path<'a>(parts: &mut SplitWhitespace<'a>) -> RequestResult<&'a str> {
let path = parts.next().ok_or(RequestParseError::InvalidRequestLine)?;
Ok(path)
}
fn parse_version(parts: &mut SplitWhitespace) -> RequestResult<Version> {
let version_str = parts.next().ok_or(RequestParseError::InvalidRequestLine)?;
let version = Version::from(version_str);
if version == Version::Unknown {
return Err(RequestParseError::UnsupportedVersion);
}
Ok(version)
}
impl HttpRequest {
pub fn parse<R: BufRead>(reader: &mut R) -> RequestResult<HttpRequest> {
let mut http_line = String::new();
reader
.take(MAX_HTTP_LINE_SIZE as u64 + 1)
.read_line(&mut http_line)?;
if http_line.is_empty() {
return Err(RequestParseError::ConnectionClosed);
}
if http_line.len() > MAX_HTTP_LINE_SIZE {
return Err(RequestParseError::InvalidRequestLine);
}
let mut parts = http_line.split_whitespace();
let method = parse_method(&mut parts)?;
let path = parse_path(&mut parts)?;
let version = parse_version(&mut parts)?;
if parts.next().is_some() {
return Err(RequestParseError::MalformedRequestLine);
}
let mut headers: HashMap<String, String> = HashMap::new();
let mut total_header_size = 0;
let mut header_line = String::new();
loop {
header_line.clear();
reader
.take(MAX_HEADER_SIZE as u64 + 1)
.read_line(&mut header_line)?;
total_header_size += header_line.len();
if total_header_size > MAX_HEADER_SIZE {
return Err(RequestParseError::HeadersTooLarge);
}
if header_line == "\r\n" || header_line == "\n" {
break;
}
if let Some(colon_pos) = header_line.find(':') {
let name = header_line[..colon_pos].trim().to_lowercase();
let value = header_line[colon_pos + 1..].trim().to_string();
headers.insert(name, value);
}
}
let content_length = headers
.get("content-length")
.and_then(|v| v.parse::<usize>().ok())
.unwrap_or(0);
if content_length > MAX_BODY_SIZE {
return Err(RequestParseError::BodyTooLarge);
}
let mut body = Vec::new();
if content_length > 0 {
body = vec![0u8; content_length];
reader.read_exact(&mut body)?;
}
let keep_alive =
!headers.get("connection").is_some_and(|v| v == "close") && version == Version::Http11;
Ok(Self {
method,
version,
path: path.to_string(),
headers,
body,
keep_alive,
})
}
}