rig-http 0.47.0

Rig's transport contracts: the HTTP client and websocket traits a transport implements, and the wasm-compatible bounds they are written with.
Documentation
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
use super::{Framing, NdjsonFramer, SseFramer, TooLarge, WireFrame};

/// Every event a framer dispatches over one whole body.
fn sse_all(body: &[u8]) -> Vec<(String, String, String)> {
    let mut framer = SseFramer::new();
    framer
        .push(body)
        .flatten()
        .map(|event| (event.event.into_owned(), event.data, event.id))
        .collect()
}

#[test]
fn every_line_ending_terminates_a_line() {
    let message = |data: &str| ("message".to_string(), data.to_string(), String::new());
    assert_eq!(sse_all(b"data: one\n\n"), [message("one")]);
    assert_eq!(sse_all(b"data: one\r\n\r\n"), [message("one")]);
    // A CR ends its line at once, so a body ending on one dispatches.
    assert_eq!(
        sse_all(b"data: one\r\rdata: two\r\r"),
        [message("one"), message("two")]
    );
}

#[test]
fn a_leading_bom_is_not_data() {
    assert_eq!(
        sse_all("\u{feff}data: one\n\n".as_bytes()),
        vec![("message".into(), "one".into(), String::new())]
    );
}

#[test]
fn a_bom_split_across_chunks_is_still_ignored() {
    let mut framer = SseFramer::new();
    assert_eq!(framer.push(&[0xef]).len(), 0);
    assert_eq!(framer.push(&[0xbb]).len(), 0);
    let events: Vec<_> = framer.push(&[0xbf]).collect();
    assert!(events.is_empty());
    let events: Vec<_> = framer.push(b"data: one\n\n").flatten().collect();
    assert_eq!(events.first().map(|event| event.data.as_str()), Some("one"));
}

#[test]
fn a_leading_colon_is_a_comment() {
    assert_eq!(
        sse_all(b": keep-alive\ndata: one\n\n"),
        vec![("message".into(), "one".into(), String::new())]
    );
}

#[test]
fn an_id_persists_across_events() {
    assert_eq!(
        sse_all(b"id: 7\ndata: one\n\ndata: two\n\n"),
        vec![
            ("message".into(), "one".into(), "7".into()),
            ("message".into(), "two".into(), "7".into()),
        ]
    );
}

#[test]
fn a_retry_does_not_persist_across_events() {
    let mut framer = SseFramer::new();
    let retries: Vec<_> = framer
        .push(b"retry: 500\ndata: one\n\ndata: two\n\n")
        .flatten()
        .map(|event| event.retry)
        .collect();
    assert_eq!(retries, vec![Some(500), None]);
}

#[test]
fn a_blank_line_without_data_dispatches_nothing_and_resets_the_type() {
    assert_eq!(
        sse_all(b"event: ping\n\ndata: one\n\n"),
        vec![("message".into(), "one".into(), String::new())]
    );
}

#[test]
fn a_truncated_trailing_event_is_pending_bytes_never_a_frame() {
    let mut framer = SseFramer::new();
    let events: Vec<_> = framer
        .push(b"data: one\n\nevent: message_stop\ndata: {}")
        .collect();
    assert_eq!(events.len(), 1);
    assert_eq!(framer.pending(), b"event: message_stop\ndata: {}".len());
}

/// The two cassette-derived shapes: a body whose last event has no trailing
/// blank line delivers one event fewer than it spells, on any chunking.
#[test]
fn a_body_whose_last_event_lacks_its_blank_line_delivers_one_event_fewer() {
    let body = concat!(
        "event: message_start\ndata: {\"type\":\"message_start\"}\n\n",
        "event: message_stop\ndata: {\"type\":\"message_stop\"}"
    );
    assert_eq!(sse_all(body.as_bytes()).len(), 1);
    let terminated = format!("{body}\n\n");
    assert_eq!(sse_all(terminated.as_bytes()).len(), 2);
}

#[test]
fn splitting_a_body_at_every_offset_yields_the_same_events() {
    let body = concat!(
        "\u{feff}: hello\r\n",
        "event: a\r\ndata: {\"n\":1}\r\n\r\n",
        "id: x\ndata: line one\ndata: line two\n\n",
        "\rdata: after a bare cr\r\r",
        "retry: 12\nevent: b\r\ndata: last\r\n\r\n",
        "data: open\r\n",
    )
    .as_bytes();
    let mut whole = SseFramer::new();
    let events = |framer: &mut SseFramer, chunk: &[u8]| -> Vec<_> {
        framer
            .push(chunk)
            .flatten()
            .map(|event| (event.event.into_owned(), event.data, event.id))
            .collect()
    };
    let expected = events(&mut whole, body);
    assert_eq!(expected.len(), 4);
    assert_eq!(whole.pending(), b"data: open\r\n".len());
    for split in 0..=body.len() {
        let (head, tail) = body.split_at(split);
        let mut framer = SseFramer::new();
        let mut split_events = events(&mut framer, head);
        split_events.extend(events(&mut framer, b""));
        split_events.extend(events(&mut framer, tail));
        assert_eq!(split_events, expected, "split at {split}");
        assert_eq!(framer.pending(), whole.pending(), "split at {split}");
    }
    let mut framer = SseFramer::new();
    let mut bytewise = Vec::new();
    for byte in body {
        bytewise.extend(events(&mut framer, &[*byte]));
    }
    assert_eq!(bytewise, expected, "one byte at a time");
    assert_eq!(framer.pending(), whole.pending(), "one byte at a time");
}

#[test]
fn ndjson_yields_terminated_lines_and_flushes_the_last_one() {
    let mut framer = NdjsonFramer::new();
    let lines: Vec<_> = framer.push(b"{\"a\":1}\n\n{\"b\":2}\n{\"c\"").collect();
    assert_eq!(
        lines,
        vec![Ok(b"{\"a\":1}".to_vec()), Ok(b"{\"b\":2}".to_vec())]
    );
    assert_eq!(framer.pending(), 4);
    assert_eq!(framer.finish(), Some(b"{\"c\"".to_vec()));
    assert_eq!(framer.finish(), None);
}

#[test]
fn ndjson_lines_split_across_chunks_rejoin() {
    let mut framer = NdjsonFramer::new();
    assert_eq!(framer.push(b"{\"a\":").len(), 0);
    let lines: Vec<_> = framer.push(b"1}\r\n").flatten().collect();
    assert_eq!(lines, vec![b"{\"a\":1}".to_vec()]);
    let mut lines = Vec::new();
    for byte in b"{\"b\":2}\r\n\n{\"c\":3}\n{\"d\"" {
        lines.extend(framer.push(&[*byte]).flatten());
    }
    assert_eq!(lines, vec![b"{\"b\":2}".to_vec(), b"{\"c\":3}".to_vec()]);
    assert_eq!(framer.finish(), Some(b"{\"d\"".to_vec()));
}

#[test]
fn ndjson_finish_drops_a_trailing_cr() {
    let mut framer = NdjsonFramer::new();
    assert_eq!(framer.push(b"{}\n{\"a\":1}\r").count(), 1);
    assert_eq!(framer.finish(), Some(b"{\"a\":1}".to_vec()));
    let mut framer = NdjsonFramer::new();
    assert_eq!(framer.push(b"{}\n\r").count(), 1);
    assert_eq!(framer.finish(), None);
}

#[test]
fn a_whitespace_only_event_is_a_heartbeat_and_never_a_frame() {
    let Some(mut framer) = Framing::Sse.framer() else {
        panic!("SSE is framed incrementally");
    };
    let frames: Vec<_> = framer
        .push(b"data:  \n\ndata: {}\n\ndata: {")
        .flatten()
        .collect();
    assert_eq!(frames, [WireFrame::Text("{}".into())]);
    assert_eq!(framer.pending(), b"data: {".len());
    assert_eq!(framer.finish(), None);
}

#[test]
fn a_retry_beyond_u64_saturates() {
    let mut framer = SseFramer::new();
    let retries: Vec<_> = framer
        .push(b"retry: 500\nretry: 99999999999999999999\ndata: one\n\n")
        .flatten()
        .map(|event| event.retry)
        .collect();
    assert_eq!(retries, [Some(u64::MAX)]);
}

#[test]
fn recording_ranges_and_tail_use_the_normal_grammar_at_every_chunk_boundary() {
    let body = "\u{feff}id: 7\rdata: one\r\rdata: two\r\n\r\nevent: last\ndata: tail";
    for split in 0..=body.len() {
        let (head, tail) = body.as_bytes().split_at(split);
        let mut framer = SseFramer::new();
        let mut events = Vec::new();
        let mut lines = Vec::new();
        for chunk in [head, tail] {
            let batch = framer.push_recording(chunk);
            events.extend(batch.events);
            lines.extend(batch.lines);
        }
        let batch = framer.finish_recording();
        events.extend(batch.events);
        lines.extend(batch.lines);
        let payloads: Vec<_> = events
            .iter()
            .map(|frame| (frame.event.data.as_str(), frame.event.id.as_str()))
            .collect();
        assert_eq!(
            payloads,
            [("one", "7"), ("two", "7"), ("tail", "7")],
            "split {split}"
        );
        assert_eq!(events.last().map(|frame| frame.end), Some(body.len()));
        assert_eq!(
            events.last().map(|frame| frame.event.event.as_ref()),
            Some("last")
        );
        assert_eq!(lines.first().map(|line| line.content.clone()), Some(3..8));
        let data: Vec<_> = lines
            .iter()
            .filter(|line| line.field == super::SseField::Data)
            .filter_map(|line| line.value.clone().and_then(|range| body.get(range)))
            .collect();
        assert_eq!(data, ["one", "two", "tail"]);
        for line in lines {
            assert!(body.get(line.span.clone()).is_some());
            assert!(line.content.start >= line.span.start);
            assert!(line.content.end <= line.span.end);
        }
    }
    assert_eq!(sse_all(body.as_bytes()).len(), 2);
}

/// An SSE event that holds exactly the limit passes, on any chunking; one
/// byte more is refused once, with the events before it, and nothing after.
#[test]
fn an_sse_event_past_the_limit_is_refused_without_growing_past_it() {
    let line = "data: 0123456789";
    let limit = line.len();
    let fits = format!(": ping\n\n{line}\n\n");
    for size in [1, 3, fits.len()] {
        let mut framer = SseFramer::with_limit(limit);
        let mut events = Vec::new();
        for chunk in fits.as_bytes().chunks(size) {
            events.extend(framer.push(chunk));
        }
        let data: Vec<_> = events
            .into_iter()
            .map(|event| event.map(|event| event.data))
            .collect();
        assert_eq!(data, [Ok("0123456789".to_owned())], "chunks of {size}");
    }
    let over = format!("data: one\n\n{line}X\n\ndata: two\n\n");
    let mut framer = SseFramer::with_limit(limit);
    let events: Vec<_> = framer
        .push(over.as_bytes())
        .map(|event| event.map(|event| event.data))
        .collect();
    let refused = TooLarge {
        limit,
        asked: limit + 1,
    };
    assert_eq!(events, [Ok("one".to_owned()), Err(refused)]);
    assert!(framer.pending() <= limit + 1);
    assert_eq!(framer.push(b"data: three\n\n").count(), 0);
}

/// Heartbeats between events hold nothing, however many come.
#[test]
fn sse_heartbeats_never_fill_the_limit() {
    let mut framer = SseFramer::with_limit(16);
    for _ in 0..100 {
        assert_eq!(framer.push(b": keep-alive\n").count(), 0);
    }
    let events: Vec<_> = framer.push(b"data: {}\n\n").collect();
    assert_eq!(events.len(), 1);
    assert!(events.iter().all(Result::is_ok));
}

/// An NDJSON line of exactly the limit passes; a longer one is refused in
/// its place, without holding more than one byte past the limit, and the
/// lines after it still come.
#[test]
fn an_ndjson_line_past_the_limit_is_refused_and_reading_goes_on() {
    let mut framer = NdjsonFramer::with_limit(4);
    let lines: Vec<_> = framer.push(b"abcd\nabcde").collect();
    assert_eq!(
        lines,
        [Ok(b"abcd".to_vec()), Err(TooLarge { limit: 4, asked: 5 })]
    );
    assert_eq!(framer.pending(), 0);
    for byte in b"fghij" {
        assert_eq!(framer.push(&[*byte]).count(), 0);
        assert_eq!(framer.pending(), 0);
    }
    let lines: Vec<_> = framer.push(b"k\nnext\n").collect();
    assert_eq!(lines, [Ok(b"next".to_vec())]);
}

/// A line of exactly the limit passes with a CRLF end, its CR in a chunk of
/// its own; a CR inside a line counts.
#[test]
fn an_ndjson_crlf_end_does_not_count_against_the_limit() {
    let mut framer = NdjsonFramer::with_limit(4);
    assert_eq!(framer.push(b"null\r").count(), 0);
    let lines: Vec<_> = framer.push(b"\n").collect();
    assert_eq!(lines, [Ok(b"null".to_vec())]);
    let lines: Vec<_> = framer.push(b"nu\rll\n").collect();
    assert_eq!(lines, [Err(TooLarge { limit: 4, asked: 5 })]);
    // A CR that ends a chunk but not the line counts once the line goes on.
    assert_eq!(framer.push(b"{  \r").count(), 0);
    let lines: Vec<_> = framer.push(b"}\n").collect();
    assert_eq!(lines, [Err(TooLarge { limit: 4, asked: 5 })]);
}

/// The last id is copied into every event it precedes, so it counts once
/// for each event a push holds: a large id cannot multiply in memory.
#[test]
fn an_sse_id_counts_for_each_event_it_is_copied_into() {
    let id = "x".repeat(10);
    let mut body = format!("id: {id}\n");
    for _ in 0..4 {
        body.push_str("data: a\n\n");
    }
    let mut framer = SseFramer::with_limit(32);
    let events: Vec<_> = framer.push(body.as_bytes()).collect();
    assert_eq!(events.iter().filter(|event| event.is_ok()).count(), 2);
    assert!(events.last().is_some_and(Result::is_err));
    let mut framer = SseFramer::with_limit(64);
    assert_eq!(framer.push(body.as_bytes()).flatten().count(), 4);
    // An id set after the events of a push was copied into none of them.
    let mut framer = SseFramer::with_limit(32);
    let events: Vec<_> = framer
        .push(b"data:a\n\ndata:b\n\nid:abcdefghijk\n")
        .collect();
    assert_eq!(events.len(), 2);
    assert!(events.iter().all(Result::is_ok));
    // Many events in one chunk stop at the limit, not after all their copies.
    let mut many = format!("id: {}\n", "x".repeat(1000));
    many.push_str(&"data: a\n\n".repeat(10_000));
    let mut framer = SseFramer::with_limit(4096);
    let events: Vec<_> = framer.push(many.as_bytes()).collect();
    assert!(events.len() <= 5, "{}", events.len());
    assert!(events.last().is_some_and(Result::is_err));
}

/// A byte that is not UTF-8 decodes to three: a line that would pass the
/// limit that way is refused before it is decoded.
#[test]
fn an_sse_line_refuses_what_its_decoding_would_grow_past_the_limit() {
    let mut line = b"data:".to_vec();
    line.extend([0xff; 10]);
    line.extend(b"\n\n");
    let mut framer = SseFramer::with_limit(16);
    let events: Vec<_> = framer.push(&line).collect();
    assert_eq!(
        events,
        [Err(TooLarge {
            limit: 16,
            asked: 35
        })]
    );
    let mut framer = SseFramer::with_limit(64);
    let events: Vec<_> = framer.push(&line).flatten().collect();
    assert_eq!(
        events.first().map(|event| event.data.chars().count()),
        Some(10)
    );
}

/// An event's copy of the last id counts before it is made: a single event
/// whose id would pass the limit is refused, never queued.
#[test]
fn an_sse_id_copy_past_the_limit_is_refused_before_it_is_made() {
    let body = format!("id: {}\ndata:a\n\n", "x".repeat(20));
    let mut framer = SseFramer::with_limit(32);
    let events: Vec<_> = framer.push(body.as_bytes()).collect();
    assert!(
        matches!(events.as_slice(), [Err(TooLarge { limit: 32, .. })]),
        "{events:?}"
    );
}

/// A recording parses text in memory already: no limit drops its events.
#[test]
fn a_recording_keeps_every_event_whatever_its_ids_copy() {
    let mut body = format!("id: {}\n", "x".repeat(64));
    for _ in 0..10 {
        body.push_str("data: {}\n\n");
    }
    let mut framer = SseFramer::with_limit(128);
    let mut events = framer.push_recording(body.as_bytes()).events;
    events.extend(framer.finish_recording().events);
    assert_eq!(events.len(), 10);
}