-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.rs
353 lines (326 loc) · 11.4 KB
/
tcp.rs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
use std::net::*;
use std::io::{Read, Write};
const ECHO: &'static [u8] =
b"HTTP/1.1 200 Connection Established\r\n\r\n";
struct HttpRequest {
starter: Vec<u8>,
headers: Vec<u8>,
stream: TcpStream,
}
use std::fmt::{Formatter, Debug, Result as FmtResult};
fn escape_bytestring(s: &[u8]) -> String {
s.iter().map(|&x| (x as char).escape_default().collect::<String>()).collect()
}
impl Debug for HttpRequest {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
try!(write!(f, "HttpRequest {{ starter: "));
for &c in &self.starter {
try!(write!(f, "{}", (c as char).escape_default().collect::<String>()));
}
try!(write!(f, " , headers: "));
for &c in &self.headers {
try!(write!(f, "{}", (c as char).escape_default().collect::<String>()));
}
write!(f, " }}")
}
}
fn handle_stream(mut stream: TcpStream) -> Result<HttpRequest, ()> {
let mut buf = [0u8; 5];
const CRLF_CRLF: [u8; 4] = [b'\r', b'\n', b'\r', b'\n'];
let mut match_count = 0;
let mut history = Vec::<u8>::new();
let mut first_line = None;
'main_loop: loop {
let n = if let Ok(n) = stream.read(&mut buf) {
n
} else {
continue;
};
for (idx, &c) in buf[..n].iter().enumerate() {
if c == CRLF_CRLF[match_count] {
match_count += 1;
if match_count == 4 {
if let Some(starter) = first_line {
let mut headers = Vec::new();
headers.extend(&history);
headers.extend(&buf[..idx + 1]);
return Ok(HttpRequest {
starter: starter,
headers: headers,
stream: stream,
});
} else {
return Err(());
}
}
else if match_count == 2 && first_line.is_none() {
let mut content = Vec::new();
content.extend(&history);
content.extend(&buf[..idx + 1]);
first_line = Some(content);
history.clear();
history.extend(&buf[idx + 1..]);
continue 'main_loop;
}
} else {
match_count = 0;
}
}
history.extend(&buf[..n]);
}
}
struct HeaderCatcher {
match_count: usize,
}
impl HeaderCatcher {
pub fn new() -> Self {
HeaderCatcher { match_count: 0 }
}
pub fn handle(&mut self, s: &[u8]) -> Option<usize> {
const CRLF_CRLF: [u8; 4] = [b'\r', b'\n', b'\r', b'\n'];
for (idx, &c) in s.iter().enumerate() {
if c == CRLF_CRLF[self.match_count] {
self.match_count += 1;
if self.match_count == 4 {
return Some(idx);
}
} else {
self.match_count = 0;
}
}
return None;
}
}
#[derive(PartialEq, Debug)]
enum HeaderCollector {
None,
StartingReturn,
Start,
Content(usize),
WaitColon,
Coloned,
Collecting {
collect: Vec<u8>
},
Returned {
collect: Vec<u8>
},
Done {
collect: Vec<u8>
},
}
impl HeaderCollector {
pub fn new() -> Self {
HeaderCollector::None
}
pub fn put(&mut self, c: u8) {
const CONTENT: &'static [u8] = b"content-length";
use HeaderCollector::*;
use std::mem::replace;
let this = replace(self, None);
replace(self, match this {
None if c == b'\r' => StartingReturn,
StartingReturn if c == b'\n' => Start,
StartingReturn if c == b'\r' => None,
Start => if (c as char).to_lowercase().next() == Some(CONTENT[0] as char) {
Content(1)
} else {
None
},
Content(offset) => if Some(CONTENT[offset] as char) == (c as char).to_lowercase().next() {
if offset == CONTENT.len() - 1 {
WaitColon
} else {
Content(offset + 1)
}
} else {
None
},
WaitColon if c == b' ' => WaitColon,
WaitColon if c == b':' => Coloned,
Coloned => if c == b' ' { Coloned } else {
Collecting { collect: vec![c] }
},
Collecting { ref collect } if c == b'\r' => {
Returned { collect: collect.clone() }
},
Collecting { mut collect } => {
collect.push(c);
Collecting { collect: collect }
},
Returned { mut collect } => if c == b'\n'{
Done { collect: collect }
} else {
collect.push(b'\r');
if c == b'\r' {
Returned { collect: collect }
} else {
Collecting { collect: collect }
}
},
_ => None,
});
}
}
fn proxy(mut req: HttpRequest) {
let port = req.port().map(Vec::from);
println!("port {:?}", port.as_ref().map(|x| escape_bytestring(x)));
let mut stream = TcpStream::connect((std::str::from_utf8(&port.expect("host not found")).expect("non-UTF8 host"), 80)).expect("connection failed");
stream.write(req.starter()).unwrap();
stream.write(req.headers()).unwrap();
let mut buf = [0; 7];
let mut length: Option<usize> = None;
let mut header_catcher = HeaderCatcher::new();
let mut header_collector = HeaderCollector::new();
let mut length_count = 0;
loop {
if let Ok(n) = stream.read(&mut buf) {
req.stream.write(&buf[..n]).unwrap();
if length_count == 0 {
if let Some(offset) = header_catcher.handle(&buf[..n]) {
length_count = n - offset;
}
} else {
length_count += n;
}
if let Some(c) = length {
if length_count >= c {
break;
}
} else {
for &c in &buf[..n] {
header_collector.put(c);
if let HeaderCollector::Done { ref collect } = header_collector {
if let Ok(s) = std::str::from_utf8(collect) {
length = s.parse().ok();
println!("content length {:?}", length)
}
}
}
}
}
}
}
impl HttpRequest {
pub fn port(&self) -> Option<&[u8]> {
fn find(x: &[u8], c: u8) -> Option<usize> {
for (idx, &x) in x.iter().enumerate() {
if x == c {
return Some(idx);
}
}
return None;
}
match self.host() {
x @ Some(_) => x,
_ => match self.path() {
Some(p) => {
let len = "http://".len();
let start = &p[len..];
if let Some(offset) = find(start, b'/') {
Some(&start[..offset])
} else {
None
}
},
x => x
}
}
}
pub fn host(&self) -> Option<&[u8]> {
#[derive(PartialEq, Debug)]
enum Status {
None,
StartingReturn,
Start,
First,
Second,
Third,
Fourth,
Coloned,
Collecting,
Returned,
}
let mut start = None;
let mut matched = Status::None;
for (idx, &c) in self.headers.iter().enumerate() {
if matched == Status::Start && !(c == b'H' || c == b'h') {
matched = Status::None;
} else if matched == Status::None && c == b'\r' {
matched = Status::StartingReturn;
} else if matched == Status::StartingReturn && c == b'\n' {
matched = Status::Start;
} else if matched == Status::StartingReturn && c != b'\r' {
matched = Status::None;
} else if matched == Status::Start && (c == b'H' || c == b'h') {
matched = Status::First;
} else if matched == Status::First && (c == b'O' || c == b'o') {
matched = Status::Second;
} else if matched == Status::Second && (c == b'S' || c == b's') {
matched = Status::Third;
} else if matched == Status::Third && (c == b'T' || c == b't') {
matched = Status::Fourth;
} else if matched == Status::Fourth && c == b':' {
matched = Status::Coloned;
} else if matched == Status::Coloned && c != b' ' {
start = Some(idx);
matched = Status::Collecting;
} else if matched == Status::Collecting && c == b'\r' {
matched = Status::Returned;
} else if matched == Status::Returned && c == b'\n' {
return Some(&self.headers[start.expect("null-start") .. idx - 1]);
} else if !(c == b' ' && (matched == Status::Fourth || matched == Status::Coloned)) && matched != Status::Collecting && matched != Status::Returned {
matched = Status::None;
} else if matched == Status::Returned && c != b'\r' {
matched = Status::Collecting;
}
}
None
}
pub fn path(&self) -> Option<&[u8]> {
let mut gap = None;
for (idx, &c) in self.starter.iter().enumerate() {
if (c as char).is_whitespace() {
if let Some(s) = gap {
return Some(&self.starter[s..idx]);
}
gap = Some(idx + 1);
}
}
return None;
}
pub fn method(&self) -> Option<&[u8]> {
for (idx, &c) in self.starter.iter().enumerate() {
if (c as char).is_whitespace() {
return Some(&self.starter[..idx]);
}
}
return None;
}
fn starter(&self) -> &[u8] {
&self.starter
}
fn headers(&self) -> &[u8] {
&self.headers
}
}
fn main() {
let listener = TcpListener::bind("0.0.0.0:3386").expect("connection failed");
for stream in listener.incoming() {
println!("incoming.");
if let Ok(mut r) = handle_stream(stream.unwrap()) {
println!("method: {:?}", r.method().map(escape_bytestring));
println!("path: {:?}", r.path().map(escape_bytestring));
println!("host: {:?}", r.host().map(escape_bytestring));
if r.method() == Some(b"CONNECT") {
r.stream.write(ECHO).expect("write failed");
println!("replied CONNECT");
} else {
println!("going proxy");
proxy(r);
println!("done proxy");
}
}
println!("done.");
}
}