-
Notifications
You must be signed in to change notification settings - Fork 126
/
promptness.rs
195 lines (171 loc) · 6.51 KB
/
promptness.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
extern crate tiny_http;
use std::io::{copy, Read, Write};
use std::net::Shutdown;
use std::ops::Deref;
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread::spawn;
use std::time::Duration;
use tiny_http::Response;
#[allow(dead_code)]
mod support;
use support::{new_one_server_one_client, SlowByteSrc};
/// crude impl of http `Transfer-Encoding: chunked`
fn encode_chunked(data: &mut dyn Read, output: &mut dyn Write) {
let mut buf = [0u8; 4096];
loop {
let l = data.read(&mut buf).unwrap();
write!(output, "{:X}\r\n", l).unwrap();
output.write_all(&buf[..l]).unwrap();
write!(output, "\r\n").unwrap();
if l == 0 {
break;
}
}
}
mod prompt_pipelining {
use std::time::Duration;
use super::*;
/// Check that pipelined requests on the same connection are received promptly.
fn assert_requests_parsed_promptly(
req_cnt: usize,
req_body: &'static [u8],
timeout: Duration,
req_writer: impl FnOnce(&mut dyn Write) + Send + 'static,
) {
let resp_body = SlowByteSrc {
sleep_time: Duration::from_millis(100),
val: 42,
len: 1000_000,
}; // very slow response body
let (server, mut client) = new_one_server_one_client();
let (svr_send, svr_rcv) = channel();
spawn(move || {
for _ in 0..req_cnt {
let mut req = server.recv().unwrap();
// read the whole body of the request
let mut body = Vec::new();
req.as_reader().read_to_end(&mut body).unwrap();
assert_eq!(req_body, body.as_slice());
// The next pipelined request should now be available for parsing,
// while we send the (possibly slow) response in another thread
spawn(move || {
req.respond(Response::empty(200).with_data(resp_body, Some(resp_body.len)))
});
}
svr_send.send(()).unwrap();
});
spawn(move || req_writer(&mut client));
// requests must be sent and received quickly (before timeout expires)
svr_rcv
.recv_timeout(timeout)
.expect("Server did not finish reading pipelined requests quickly enough");
}
#[test]
fn empty() {
assert_requests_parsed_promptly(5, &[], Duration::from_millis(200), move |wr| {
for _ in 0..5 {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Connection: keep-alive\r\n\r\n").unwrap();
}
});
}
#[test]
fn content_length_short() {
let body = &[65u8; 100]; // short but not trivial
assert_requests_parsed_promptly(5, body, Duration::from_millis(200), move |wr| {
for _ in 0..5 {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Connection: keep-alive\r\n").unwrap();
write!(wr, "Content-Length: {}\r\n\r\n", body.len()).unwrap();
wr.write_all(body).unwrap();
}
});
}
#[test]
fn content_length_long() {
let body = &[65u8; 10000]; // long enough that it won't be buffered
assert_requests_parsed_promptly(5, body, Duration::from_millis(200), move |wr| {
for _ in 0..5 {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Connection: keep-alive\r\n").unwrap();
write!(wr, "Content-Length: {}\r\n\r\n", body.len()).unwrap();
wr.write_all(body).unwrap();
}
});
}
#[test]
fn chunked() {
let body = &[65u8; 10000];
assert_requests_parsed_promptly(5, body, Duration::from_millis(200), move |wr| {
for _ in 0..5 {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Connection: keep-alive\r\n").unwrap();
write!(wr, "Transfer-Encoding: chunked\r\n\r\n").unwrap();
encode_chunked(&mut &body[..], wr);
}
});
}
}
mod prompt_responses {
use super::*;
/// Check that response is sent promptly without waiting for full request body.
fn assert_responds_promptly(
timeout: Duration,
req_writer: impl FnOnce(&mut dyn Write) + Send + 'static,
) {
let (server, client) = new_one_server_one_client();
spawn(move || loop {
// server attempts to respond immediately
let req = server.recv().unwrap();
req.respond(Response::empty(400)).unwrap();
});
let client = Arc::new(client);
let client_write = Arc::clone(&client);
// request written (possibly very slowly) in another thread
spawn(move || req_writer(&mut client_write.deref()));
// response should arrive quickly (before timeout expires)
client.set_read_timeout(Some(timeout)).unwrap();
let resp = client.deref().read(&mut [0u8; 4096]);
client.shutdown(Shutdown::Both).unwrap();
assert!(resp.is_ok(), "Server response was not sent promptly");
}
static SLOW_BODY: SlowByteSrc = SlowByteSrc {
sleep_time: Duration::from_millis(100),
val: 65,
len: 1000_000,
};
#[test]
fn content_length_http11() {
assert_responds_promptly(Duration::from_millis(200), move |wr| {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Content-Length: {}\r\n\r\n", SLOW_BODY.len).unwrap();
copy(&mut SLOW_BODY.clone(), wr).unwrap();
});
}
#[test]
fn content_length_http10() {
assert_responds_promptly(Duration::from_millis(200), move |wr| {
write!(wr, "GET / HTTP/1.0\r\n").unwrap();
write!(wr, "Content-Length: {}\r\n\r\n", SLOW_BODY.len).unwrap();
copy(&mut SLOW_BODY.clone(), wr).unwrap();
});
}
#[test]
fn expect_continue() {
assert_responds_promptly(Duration::from_millis(200), move |wr| {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Expect: 100 continue\r\n").unwrap();
write!(wr, "Content-Length: {}\r\n\r\n", SLOW_BODY.len).unwrap();
copy(&mut SLOW_BODY.clone(), wr).unwrap();
});
}
#[test]
fn chunked() {
assert_responds_promptly(Duration::from_millis(200), move |wr| {
write!(wr, "GET / HTTP/1.1\r\n").unwrap();
write!(wr, "Transfer-Encoding: chunked\r\n\r\n").unwrap();
encode_chunked(&mut SLOW_BODY.clone(), wr);
});
}
}