-
-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathframes.rs
More file actions
418 lines (347 loc) · 10.6 KB
/
Copy pathframes.rs
File metadata and controls
418 lines (347 loc) · 10.6 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::convert::TryInto;
use std::fmt;
use bytes::Bytes;
use http::{HeaderMap, StatusCode};
use h2::frame::{self, Frame, StreamId};
pub const SETTINGS: &[u8] = &[0, 0, 0, 4, 0, 0, 0, 0, 0];
pub const SETTINGS_ACK: &[u8] = &[0, 0, 0, 4, 1, 0, 0, 0, 0];
// ==== helper functions to easily construct h2 Frames ====
pub fn headers<T>(id: T) -> Mock<frame::Headers>
where
T: Into<StreamId>,
{
Mock(frame::Headers::new(
id.into(),
frame::Pseudo::default(),
HeaderMap::default(),
))
}
pub fn data<T, B>(id: T, buf: B) -> Mock<frame::Data>
where
T: Into<StreamId>,
B: AsRef<[u8]>,
{
let buf = Bytes::copy_from_slice(buf.as_ref());
Mock(frame::Data::new(id.into(), buf))
}
pub fn push_promise<T1, T2>(id: T1, promised: T2) -> Mock<frame::PushPromise>
where
T1: Into<StreamId>,
T2: Into<StreamId>,
{
Mock(frame::PushPromise::new(
id.into(),
promised.into(),
frame::Pseudo::default(),
HeaderMap::default(),
))
}
pub fn window_update<T>(id: T, sz: u32) -> frame::WindowUpdate
where
T: Into<StreamId>,
{
frame::WindowUpdate::new(id.into(), sz)
}
pub fn go_away<T>(id: T) -> Mock<frame::GoAway>
where
T: Into<StreamId>,
{
Mock(frame::GoAway::new(id.into(), frame::Reason::NO_ERROR))
}
pub fn reset<T>(id: T) -> Mock<frame::Reset>
where
T: Into<StreamId>,
{
Mock(frame::Reset::new(id.into(), frame::Reason::NO_ERROR))
}
pub fn settings() -> Mock<frame::Settings> {
Mock(frame::Settings::default())
}
pub fn settings_ack() -> Mock<frame::Settings> {
Mock(frame::Settings::ack())
}
pub fn ping(payload: [u8; 8]) -> Mock<frame::Ping> {
Mock(frame::Ping::new(payload))
}
// === Generic helpers of all frame types
pub struct Mock<T>(T);
impl<T: fmt::Debug> fmt::Debug for Mock<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl<T> From<Mock<T>> for Frame
where
T: Into<Frame>,
{
fn from(src: Mock<T>) -> Self {
src.0.into()
}
}
// Headers helpers
impl Mock<frame::Headers> {
pub fn request<M, U>(self, method: M, uri: U) -> Self
where
M: TryInto<http::Method>,
M::Error: fmt::Debug,
U: TryInto<http::Uri>,
U::Error: fmt::Debug,
{
let method = method.try_into().unwrap();
let uri = uri.try_into().unwrap();
let (id, _, fields) = self.into_parts();
let extensions = Default::default();
let pseudo = frame::Pseudo::request(method, uri, extensions);
let frame = frame::Headers::new(id, pseudo, fields);
Mock(frame)
}
pub fn method<M>(self, method: M) -> Self
where
M: TryInto<http::Method>,
M::Error: fmt::Debug,
{
let method = method.try_into().unwrap();
let (id, pseudo, fields) = self.into_parts();
let frame = frame::Headers::new(
id,
frame::Pseudo {
method: Some(method),
..pseudo
},
fields,
);
Mock(frame)
}
pub fn pseudo(self, pseudo: frame::Pseudo) -> Self {
let (id, _, fields) = self.into_parts();
let frame = frame::Headers::new(id, pseudo, fields);
Mock(frame)
}
pub fn response<S>(self, status: S) -> Self
where
S: TryInto<http::StatusCode>,
S::Error: fmt::Debug,
{
let status = status.try_into().unwrap();
let (id, _, fields) = self.into_parts();
let frame = frame::Headers::new(id, frame::Pseudo::response(status), fields);
Mock(frame)
}
pub fn fields(self, fields: HeaderMap) -> Self {
let (id, pseudo, _) = self.into_parts();
let frame = frame::Headers::new(id, pseudo, fields);
Mock(frame)
}
pub fn field<K, V>(self, key: K, value: V) -> Self
where
K: TryInto<http::header::HeaderName>,
K::Error: fmt::Debug,
V: TryInto<http::header::HeaderValue>,
V::Error: fmt::Debug,
{
let (id, pseudo, mut fields) = self.into_parts();
fields.insert(key.try_into().unwrap(), value.try_into().unwrap());
let frame = frame::Headers::new(id, pseudo, fields);
Mock(frame)
}
pub fn status(self, value: StatusCode) -> Self {
let (id, mut pseudo, fields) = self.into_parts();
pseudo.set_status(value);
Mock(frame::Headers::new(id, pseudo, fields))
}
pub fn scheme(self, value: &str) -> Self {
let (id, mut pseudo, fields) = self.into_parts();
let value = value.parse().unwrap();
pseudo.set_scheme(value);
Mock(frame::Headers::new(id, pseudo, fields))
}
pub fn eos(mut self) -> Self {
self.0.set_end_stream();
self
}
pub fn into_fields(self) -> HeaderMap {
self.0.into_parts().1
}
fn into_parts(self) -> (StreamId, frame::Pseudo, HeaderMap) {
assert!(!self.0.is_end_stream(), "eos flag will be lost");
assert!(self.0.is_end_headers(), "unset eoh will be lost");
let id = self.0.stream_id();
let parts = self.0.into_parts();
(id, parts.0, parts.1)
}
}
impl From<Mock<frame::Headers>> for frame::Headers {
fn from(src: Mock<frame::Headers>) -> Self {
src.0
}
}
// Data helpers
impl Mock<frame::Data> {
pub fn padded(mut self) -> Self {
self.0.set_padded();
self
}
pub fn eos(mut self) -> Self {
self.0.set_end_stream(true);
self
}
}
// PushPromise helpers
impl Mock<frame::PushPromise> {
pub fn request<M, U>(self, method: M, uri: U) -> Self
where
M: TryInto<http::Method>,
M::Error: fmt::Debug,
U: TryInto<http::Uri>,
U::Error: fmt::Debug,
{
let method = method.try_into().unwrap();
let uri = uri.try_into().unwrap();
let (id, promised, _, fields) = self.into_parts();
let extensions = Default::default();
let pseudo = frame::Pseudo::request(method, uri, extensions);
let frame = frame::PushPromise::new(id, promised, pseudo, fields);
Mock(frame)
}
pub fn pseudo(self, pseudo: frame::Pseudo) -> Self {
let (id, promised, _, fields) = self.into_parts();
let frame = frame::PushPromise::new(id, promised, pseudo, fields);
Mock(frame)
}
pub fn fields(self, fields: HeaderMap) -> Self {
let (id, promised, pseudo, _) = self.into_parts();
let frame = frame::PushPromise::new(id, promised, pseudo, fields);
Mock(frame)
}
pub fn field<K, V>(self, key: K, value: V) -> Self
where
K: TryInto<http::header::HeaderName>,
K::Error: fmt::Debug,
V: TryInto<http::header::HeaderValue>,
V::Error: fmt::Debug,
{
let (id, promised, pseudo, mut fields) = self.into_parts();
fields.insert(key.try_into().unwrap(), value.try_into().unwrap());
let frame = frame::PushPromise::new(id, promised, pseudo, fields);
Mock(frame)
}
fn into_parts(self) -> (StreamId, StreamId, frame::Pseudo, HeaderMap) {
assert!(self.0.is_end_headers(), "unset eoh will be lost");
let id = self.0.stream_id();
let promised = self.0.promised_id();
let parts = self.0.into_parts();
(id, promised, parts.0, parts.1)
}
}
// GoAway helpers
impl Mock<frame::GoAway> {
pub fn protocol_error(self) -> Self {
self.reason(frame::Reason::PROTOCOL_ERROR)
}
pub fn internal_error(self) -> Self {
self.reason(frame::Reason::INTERNAL_ERROR)
}
pub fn flow_control(self) -> Self {
self.reason(frame::Reason::FLOW_CONTROL_ERROR)
}
pub fn frame_size(self) -> Self {
self.reason(frame::Reason::FRAME_SIZE_ERROR)
}
pub fn calm(self) -> Self {
self.reason(frame::Reason::ENHANCE_YOUR_CALM)
}
pub fn no_error(self) -> Self {
self.reason(frame::Reason::NO_ERROR)
}
pub fn data<I>(self, debug_data: I) -> Self
where
I: Into<Bytes>,
{
Mock(frame::GoAway::with_debug_data(
self.0.last_stream_id(),
self.0.reason(),
debug_data.into(),
))
}
pub fn reason(self, reason: frame::Reason) -> Self {
Mock(frame::GoAway::with_debug_data(
self.0.last_stream_id(),
reason,
self.0.debug_data().clone(),
))
}
}
// ==== Reset helpers
impl Mock<frame::Reset> {
pub fn protocol_error(self) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, frame::Reason::PROTOCOL_ERROR))
}
pub fn flow_control(self) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, frame::Reason::FLOW_CONTROL_ERROR))
}
pub fn refused(self) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, frame::Reason::REFUSED_STREAM))
}
pub fn cancel(self) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, frame::Reason::CANCEL))
}
pub fn stream_closed(self) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, frame::Reason::STREAM_CLOSED))
}
pub fn internal_error(self) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, frame::Reason::INTERNAL_ERROR))
}
pub fn reason(self, reason: frame::Reason) -> Self {
let id = self.0.stream_id();
Mock(frame::Reset::new(id, reason))
}
}
// ==== Settings helpers
impl Mock<frame::Settings> {
pub fn max_concurrent_streams(mut self, max: u32) -> Self {
self.0.set_max_concurrent_streams(Some(max));
self
}
pub fn max_frame_size(mut self, val: u32) -> Self {
self.0.set_max_frame_size(Some(val));
self
}
pub fn initial_window_size(mut self, val: u32) -> Self {
self.0.set_initial_window_size(Some(val));
self
}
pub fn max_header_list_size(mut self, val: u32) -> Self {
self.0.set_max_header_list_size(Some(val));
self
}
pub fn disable_push(mut self) -> Self {
self.0.set_enable_push(false);
self
}
pub fn enable_connect_protocol(mut self, val: u32) -> Self {
self.0.set_enable_connect_protocol(Some(val));
self
}
pub fn header_table_size(mut self, val: u32) -> Self {
self.0.set_header_table_size(Some(val));
self
}
}
impl From<Mock<frame::Settings>> for frame::Settings {
fn from(src: Mock<frame::Settings>) -> Self {
src.0
}
}
// ==== Ping helpers
impl Mock<frame::Ping> {
pub fn pong(self) -> Self {
let payload = self.0.into_payload();
Mock(frame::Ping::pong(payload))
}
}