ureq-proto 0.7.1

ureq support crate
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
use http::{StatusCode, Version, header};

use crate::Error;
use crate::client::test::scenario::Scenario;
use crate::ext::HeaderIterExt;

// This is a complete response.
const RESPONSE: &[u8] = b"\
        HTTP/1.1 200 OK\r\n\
        Content-Length: 123\r\n\
        Content-Type: text/plain\r\n\
        \r\n";

#[test]
fn receive_incomplete_response() {
    // -1 to never reach the end
    for i in 14..RESPONSE.len() - 1 {
        let scenario = Scenario::builder().get("https://q.test").build();
        let mut call = scenario.to_recv_response();

        let (input_used, maybe_response) = call.try_response(&RESPONSE[..i], true).unwrap();
        assert_eq!(input_used, 0);
        assert!(maybe_response.is_none());
        assert!(!call.can_proceed());
    }
}

#[test]
fn receive_complete_response() {
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());

    let response = maybe_response.unwrap();

    assert_eq!(response.version(), Version::HTTP_11);
    assert_eq!(response.status(), StatusCode::OK);
    assert_eq!(
        response.headers().get(header::CONTENT_LENGTH).unwrap(),
        "123"
    );
    assert!(
        response
            .headers()
            .iter()
            .has(header::CONTENT_TYPE, "text/plain")
    );

    assert!(call.can_proceed());
}

#[test]
fn partial_redirect_with_empty_header_value() {
    // Broken servers may omit the final \r\n of a redirect. With
    // allow_partial_redirect we accept the redirect from the partial
    // headers. An empty header value before Location must not hide it.
    let input: &[u8] = b"\
        HTTP/1.1 302 Found\r\n\
        X-Empty:\r\n\
        Location: https://q.test/other\r\n";

    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let (input_used, maybe_response) = call.try_response(input, true).unwrap();
    assert_eq!(input_used, input.len());

    let response = maybe_response.expect("partial redirect detected");
    assert_eq!(response.status(), StatusCode::FOUND);
    assert_eq!(
        response.headers().get(header::LOCATION).unwrap(),
        "https://q.test/other"
    );
    assert!(response.headers().iter().has(header::CONNECTION, "close"));
    assert!(call.can_proceed());
}

#[test]
fn accepted_partial_redirect_does_not_wait_for_eof() {
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();
    let input = b"HTTP/1.1 302 Found\r\nLocation: /other\r\n";
    let (used, response) = call.try_response(input, true).unwrap();
    assert_eq!(used, input.len());
    assert!(response.is_some());

    // The opt-in partial-header workaround must follow immediately: a server
    // omitting the final CRLF may leave its socket open indefinitely. Since
    // the header boundary is missing, subsequent bytes cannot be read as body.
    let crate::client::RecvResponseResult::Redirect(call) = call.proceed().unwrap() else {
        panic!("accepted partial redirect must not wait for a response body or EOF");
    };
    assert!(call.must_close_connection());
}

#[test]
fn prepended_100_continue() {
    // In the case of expect-100-continue, there's a chance the 100-continue
    // arrives after we started sending the request body, in which case
    // we receive it before the actual response.
    let scenario = Scenario::builder()
        .post("https://q.test")
        .header("expect", "100-continue")
        .build();

    let mut call = scenario.to_recv_response();

    // incomplete 100-continue should be ignored.
    let (input_used, maybe_response) = call
        .try_response(b"HTTP/1.1 100 Continue\r\n", true)
        .unwrap();
    assert_eq!(input_used, 0);
    assert!(maybe_response.is_none());
    assert!(!call.can_proceed());

    // complete 100-continue should be consumed without producing a request
    let (input_used, maybe_response) = call
        .try_response(b"HTTP/1.1 100 Continue\r\n\r\n", true)
        .unwrap();
    assert_eq!(input_used, 25);
    assert!(maybe_response.is_none());
    assert!(!call.can_proceed());

    // full response after prepended 100-continue
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());
}

#[test]
fn expect_100_without_100_continue() {
    // In the case of expect-100-continue
    let scenario = Scenario::builder()
        .post("https://q.test")
        .header("expect", "100-continue")
        .build();

    let mut call = scenario.to_recv_response();

    // full response and no 100-continue
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());
}

#[test]
fn unsolicited_100_continue_on_get() {
    // Bug: server sends 100-continue on a regular GET request
    // without the client sending Expect: 100-continue header.
    // This reproduces the issue from https://github.com/algesten/ureq/issues/1137
    let scenario = Scenario::builder().get("https://q.test").build();

    let mut call = scenario.to_recv_response();

    // Server sends unsolicited 100-continue
    let (input_used, maybe_response) = call
        .try_response(b"HTTP/1.1 100 Continue\r\n\r\n", true)
        .unwrap();
    assert_eq!(input_used, 25);
    assert!(
        maybe_response.is_none(),
        "100-continue should be consumed, not returned"
    );
    assert!(!call.can_proceed());

    // Server then sends the actual response
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());

    let response = maybe_response.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

#[test]
fn unsolicited_102_processing() {
    // Server sends unsolicited 102 Processing response
    let scenario = Scenario::builder().get("https://q.test").build();

    let mut call = scenario.to_recv_response();

    // Server sends 102 Processing
    let processing = b"HTTP/1.1 102 Processing\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(processing, true).unwrap();
    assert_eq!(input_used, processing.len());
    assert!(
        maybe_response.is_none(),
        "102 Processing should be consumed, not returned"
    );

    // Server then sends the actual response
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());

    let response = maybe_response.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

#[test]
fn unsolicited_103_early_hints() {
    // Server sends unsolicited 103 Early Hints response
    let scenario = Scenario::builder().get("https://q.test").build();

    let mut call = scenario.to_recv_response();

    // Server sends 103 Early Hints with Link header
    let early_hints = b"HTTP/1.1 103 Early Hints\r\nLink: </style.css>; rel=preload\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(early_hints, true).unwrap();
    assert_eq!(input_used, early_hints.len());
    assert!(
        maybe_response.is_none(),
        "103 Early Hints should be consumed, not returned"
    );

    // Server then sends the actual response
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());

    let response = maybe_response.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

#[test]
fn multiple_1xx_responses_in_sequence() {
    // Server sends multiple 1xx responses before final response
    let scenario = Scenario::builder()
        .post("https://q.test")
        .header("expect", "100-continue")
        .build();

    let mut call = scenario.to_recv_response();

    // First: 103 Early Hints
    let early_hints = b"HTTP/1.1 103 Early Hints\r\nLink: </style.css>; rel=preload\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(early_hints, true).unwrap();
    assert_eq!(input_used, early_hints.len());
    assert!(maybe_response.is_none());

    // Second: 100 Continue
    let continue_resp = b"HTTP/1.1 100 Continue\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(continue_resp, true).unwrap();
    assert_eq!(input_used, continue_resp.len());
    assert!(maybe_response.is_none());

    // Third: 102 Processing
    let processing = b"HTTP/1.1 102 Processing\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(processing, true).unwrap();
    assert_eq!(input_used, processing.len());
    assert!(maybe_response.is_none());

    // Finally: actual response
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());

    let response = maybe_response.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

#[test]
fn switching_protocols_101_returned() {
    // 101 Switching Protocols should be returned to the caller, not consumed
    let scenario = Scenario::builder()
        .get("https://q.test")
        .header("upgrade", "websocket")
        .build();

    let mut call = scenario.to_recv_response();

    // Server sends 101 Switching Protocols
    let switching =
        b"HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(switching, true).unwrap();
    assert_eq!(input_used, switching.len());
    assert!(
        maybe_response.is_some(),
        "101 Switching Protocols should be returned, not consumed"
    );
    assert!(call.can_proceed());

    let response = maybe_response.unwrap();
    assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);
    assert_eq!(response.headers().get("upgrade").unwrap(), "websocket");

    // Verify connection is marked for protocol switch
    let result = call.proceed().unwrap();
    let has_protocol_switch = match &result {
        crate::client::RecvResponseResult::RecvBody(call) => call
            .inner
            .close_reason
            .iter()
            .any(|r| *r == crate::CloseReason::ProtocolSwitch),
        crate::client::RecvResponseResult::Redirect(call) => call
            .inner
            .close_reason
            .iter()
            .any(|r| *r == crate::CloseReason::ProtocolSwitch),
        crate::client::RecvResponseResult::Cleanup(call) => call
            .inner
            .close_reason
            .iter()
            .any(|r| *r == crate::CloseReason::ProtocolSwitch),
    };
    assert!(
        has_protocol_switch,
        "Connection should be marked with ProtocolSwitch"
    );
}

#[test]
fn multiple_103_before_final_response() {
    // Server sends multiple 103 Early Hints before final response
    let scenario = Scenario::builder().get("https://q.test").build();

    let mut call = scenario.to_recv_response();

    // First 103 with CSS preload hint
    let early_hints1 = b"HTTP/1.1 103 Early Hints\r\nLink: </style.css>; rel=preload\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(early_hints1, true).unwrap();
    assert_eq!(input_used, early_hints1.len());
    assert!(maybe_response.is_none());

    // Second 103 with JS preload hint
    let early_hints2 = b"HTTP/1.1 103 Early Hints\r\nLink: </script.js>; rel=preload\r\n\r\n";
    let (input_used, maybe_response) = call.try_response(early_hints2, true).unwrap();
    assert_eq!(input_used, early_hints2.len());
    assert!(maybe_response.is_none());

    // Final response
    let (input_used, maybe_response) = call.try_response(RESPONSE, true).unwrap();
    assert_eq!(input_used, 66);
    assert!(maybe_response.is_some());

    let response = maybe_response.unwrap();
    assert_eq!(response.status(), StatusCode::OK);
}

// Content-Length handling follows RFC 9110 §8.6 / RFC 9112 §6.3 the way
// libcurl does: repeated header lines and comma separated lists are fine as
// long as every value is the same number, differing values are an error, and
// a value must be plain digits.

#[test]
fn duplicate_identical_content_length_is_accepted() {
    let input: &[u8] = b"\
        HTTP/1.1 200 OK\r\n\
        Content-Length: 42\r\n\
        Content-Length: 042\r\n\
        \r\n";
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let (input_used, maybe_response) = call.try_response(input, false).unwrap();
    assert_eq!(input_used, input.len());
    assert!(maybe_response.is_some());
    assert!(call.can_proceed());
}

#[test]
fn duplicate_differing_content_length_is_rejected() {
    let input: &[u8] = b"\
        HTTP/1.1 200 OK\r\n\
        Content-Length: 42\r\n\
        Content-Length: 43\r\n\
        \r\n";
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let err = call.try_response(input, false).unwrap_err();
    assert_eq!(err, Error::TooManyContentLengthHeaders);
}

#[test]
fn content_length_list_with_differing_values_is_rejected() {
    let input: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: 42, 43\r\n\r\n";
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let err = call.try_response(input, false).unwrap_err();
    assert_eq!(err, Error::TooManyContentLengthHeaders);
}

#[test]
fn content_length_with_sign_is_rejected() {
    let input: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length: +42\r\n\r\n";
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let err = call.try_response(input, false).unwrap_err();
    assert_eq!(err, Error::BadContentLengthHeader);
}

#[test]
fn empty_content_length_is_rejected() {
    let input: &[u8] = b"HTTP/1.1 200 OK\r\nContent-Length:\r\n\r\n";
    let scenario = Scenario::builder().get("https://q.test").build();
    let mut call = scenario.to_recv_response();

    let err = call.try_response(input, false).unwrap_err();
    assert_eq!(err, Error::BadContentLengthHeader);
}