hurl 8.0.0

Hurl, run and test HTTP requests
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * Hurl (https://hurl.dev)
 * Copyright (C) 2026 Orange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
use std::{collections::HashMap, sync::LazyLock};

use regex::{Captures, Regex};

use crate::html::entities::HTML5_ENTITIES_REF;

// Ref https://html.spec.whatwg.org/#decimal-character-reference-start-state

static INVALID_CHAR: [(u32, &str); 34] = [
    (0x00, "\u{fffd}"), // REPLACEMENT CHARACTER
    (0x0d, "\r"),       // CARRIAGE RETURN
    (0x80, "\u{20ac}"), // EURO SIGN
    (0x81, "\u{81}"),   // <control>
    (0x82, "\u{201a}"), // SINGLE LOW-9 QUOTATION MARK
    (0x83, "\u{0192}"), // LATIN SMALL LETTER F WITH HOOK
    (0x84, "\u{201e}"), // DOUBLE LOW-9 QUOTATION MARK
    (0x85, "\u{2026}"), // HORIZONTAL ELLIPSIS
    (0x86, "\u{2020}"), // DAGGER
    (0x87, "\u{2021}"), // DOUBLE DAGGER
    (0x88, "\u{02c6}"), // MODIFIER LETTER CIRCUMFLEX ACCENT
    (0x89, "\u{2030}"), // PER MILLE SIGN
    (0x8a, "\u{0160}"), // LATIN CAPITAL LETTER S WITH CARON
    (0x8b, "\u{2039}"), // SINGLE LEFT-POINTING ANGLE QUOTATION MARK
    (0x8c, "\u{0152}"), // LATIN CAPITAL LIGATURE OE
    (0x8d, "\u{8d}"),   // <control>
    (0x8e, "\u{017d}"), // LATIN CAPITAL LETTER Z WITH CARON
    (0x8f, "\u{8f}"),   // <control>
    (0x90, "\u{90}"),   // <control>
    (0x91, "\u{2018}"), // LEFT SINGLE QUOTATION MARK
    (0x92, "\u{2019}"), // RIGHT SINGLE QUOTATION MARK
    (0x93, "\u{201c}"), // LEFT DOUBLE QUOTATION MARK
    (0x94, "\u{201d}"), // RIGHT DOUBLE QUOTATION MARK
    (0x95, "\u{2022}"), // BULLET
    (0x96, "\u{2013}"), // EN DASH
    (0x97, "\u{2014}"), // EM DASH
    (0x98, "\u{02dc}"), // SMALL TILDE
    (0x99, "\u{2122}"), // TRADE MARK SIGN
    (0x9a, "\u{0161}"), // LATIN SMALL LETTER S WITH CARON
    (0x9b, "\u{203a}"), // SINGLE RIGHT-POINTING ANGLE QUOTATION MARK
    (0x9c, "\u{0153}"), // LATIN SMALL LIGATURE OE
    (0x9d, "\u{9d}"),   // <control>
    (0x9e, "\u{017e}"), // LATIN SMALL LETTER Z WITH CARON
    (0x9f, "\u{0178}"), // LATIN CAPITAL LETTER Y WITH DIAERESIS
];

static INVALID_CHAR_REF: LazyLock<HashMap<u32, &'static str>> =
    LazyLock::new(|| INVALID_CHAR.iter().copied().collect());

static INVALID_CODEPOINTS: [u32; 126] = [
    // 0x0001 to 0x0008
    0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, // 0x000E to 0x001F
    0xe, 0xf, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
    0x1e, 0x1f, // 0x007F to 0x009F
    0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e,
    0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e,
    0x9f, // 0xFDD0 to 0xFDEF
    0xfdd0, 0xfdd1, 0xfdd2, 0xfdd3, 0xfdd4, 0xfdd5, 0xfdd6, 0xfdd7, 0xfdd8, 0xfdd9, 0xfdda, 0xfddb,
    0xfddc, 0xfddd, 0xfdde, 0xfddf, 0xfde0, 0xfde1, 0xfde2, 0xfde3, 0xfde4, 0xfde5, 0xfde6, 0xfde7,
    0xfde8, 0xfde9, 0xfdea, 0xfdeb, 0xfdec, 0xfded, 0xfdee, 0xfdef, // Others
    0xb, 0xfffe, 0xffff, 0x1fffe, 0x1ffff, 0x2fffe, 0x2ffff, 0x3fffe, 0x3ffff, 0x4fffe, 0x4ffff,
    0x5fffe, 0x5ffff, 0x6fffe, 0x6ffff, 0x7fffe, 0x7ffff, 0x8fffe, 0x8ffff, 0x9fffe, 0x9ffff,
    0xafffe, 0xaffff, 0xbfffe, 0xbffff, 0xcfffe, 0xcffff, 0xdfffe, 0xdffff, 0xefffe, 0xeffff,
    0xffffe, 0xfffff, 0x10fffe, 0x10ffff,
];

static CHAR_REF: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(concat!(
        r"&(#\d+;?",
        r"|#[xX][\da-fA-F]+;?",
        r"|[^\t\n\f <&#;]{1,32};?)",
    ))
    .unwrap()
});

/// Convert all named and numeric character references (e.g. &gt;, &#62;,
/// &x3e;) in the string `text` to the corresponding unicode characters.
/// This function uses the rules defined by the HTML 5 standard
/// for both valid and invalid character references, and the list of
/// HTML 5 named character references defined in html.entities.html5.
///
/// The code is adapted from the Python standard library:
/// <https://github.com/python/cpython/blob/main/Lib/html/__init__.py>
///
/// See MDN decoder tool: <https://mothereff.in/html-entities>
pub fn html_unescape(text: &str) -> String {
    if text.chars().any(|c| c == '&') {
        CHAR_REF
            .replace_all(text, |caps: &Captures| {
                let s = &caps[1];
                let s0 = s.chars().next().unwrap();
                if s0 == '#' {
                    // Numeric charref
                    let s1 = s.chars().nth(1).unwrap();
                    let num = if s1 == 'x' || s1 == 'X' {
                        let val = s[2..].trim_end_matches(';');
                        match u32::from_str_radix(val, 16) {
                            Ok(val) => val,
                            Err(_) => return "\u{FFFD}".to_string(),
                        }
                    } else {
                        let val = s[1..].trim_end_matches(';');
                        match val.parse::<u32>() {
                            Ok(val) => val,
                            Err(_) => return "\u{FFFD}".to_string(),
                        }
                    };
                    if let Some(char) = INVALID_CHAR_REF.get(&num) {
                        return char.to_string();
                    }
                    if (0xD800..=0xDFFF).contains(&num) || num > 0x10FFFF {
                        return "\u{FFFD}".to_string();
                    }
                    if INVALID_CODEPOINTS.contains(&num) {
                        return String::new();
                    }
                    char::from_u32(num).unwrap().to_string()
                } else {
                    if let Some(entity) = HTML5_ENTITIES_REF.get(s) {
                        return entity.to_string();
                    }
                    // Find the longest matching name (as defined by the standard)
                    for x in (1..s.len()).rev() {
                        let name = &s[..x];
                        if let Some(entity) = HTML5_ENTITIES_REF.get(name) {
                            return format!("{}{}", entity, &s[x..]);
                        }
                    }
                    format!("&{s}")
                }
            })
            .to_string()
    } else {
        text.to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::html_unescape;

    /// Extracts from Python test suites: https://github.com/python/cpython/blob/main/Lib/test/test_html.py
    #[test]
    fn test_html_unescape() {
        fn check(text: &str, expected: &str) {
            assert_eq!(html_unescape(text), expected.to_string());
        }

        fn check_num(num: usize, expected: &str) {
            let text = format!("&#{num}");
            check(&text, expected);
            let text = format!("&#{num};");
            check(&text, expected);
            let text = format!("&#x{num:x}");
            check(&text, expected);
            let text = format!("&#x{num:x};");
            check(&text, expected);
        }

        check("Hurl&rlarr;", "Hurl⇄");

        // Check simple
        check(
            "Foo &#xA9; bar &#x1D306; baz &#x2603; qux",
            "Foo © bar 𝌆 baz ☃ qux",
        );

        // Check text with no character references
        check("no character references", "no character references");

        // Check & followed by invalid chars
        check("&\n&\t& &&", "&\n&\t& &&");

        // Check & followed by numbers and letters
        check("&0 &9 &a &0; &9; &a;", "&0 &9 &a &0; &9; &a;");

        // Check incomplete entities at the end of the string
        for x in ["&", "&#", "&#x", "&#X", "&#y", "&#xy", "&#Xy"].iter() {
            check(x, x);
            check(&format!("{x};"), &format!("{x};"));
        }

        // Check several combinations of numeric character references,
        // possibly followed by different characters

        // Format &#1234 (without ending semi-colon)
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#{num}"), &format!("{char}"));
            check(&format!("&#{num} "), &format!("{char} "));
            check(&format!("&#{num}X"), &format!("{char}X"));
        }

        // Format &#0001234 (without ending semi-colon)
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#{num:07}"), &format!("{char}"));
            check(&format!("&#{num:07} "), &format!("{char} "));
            check(&format!("&#{num:07}X"), &format!("{char}X"));
        }

        // Format &#1234;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#{num};"), &format!("{char}"));
            check(&format!("&#{num}; "), &format!("{char} "));
            check(&format!("&#{num};X"), &format!("{char}X"));
        }

        // Format &#0001234;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#{num:07};"), &format!("{char}"));
            check(&format!("&#{num:07}; "), &format!("{char} "));
            check(&format!("&#{num:07};X"), &format!("{char}X"));
        }

        // Format &#x1abc
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:x}"), &format!("{char}"));
            check(&format!("&#x{num:x} "), &format!("{char} "));
            check(&format!("&#x{num:x}X"), &format!("{char}X"));
        }

        // Format &#x001abc
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:06x}"), &format!("{char}"));
            check(&format!("&#x{num:06x} "), &format!("{char} "));
            check(&format!("&#x{num:06x}X"), &format!("{char}X"));
        }

        // Format &#x1abc;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:x};"), &format!("{char}"));
            check(&format!("&#x{num:x}; "), &format!("{char} "));
            check(&format!("&#x{num:x};X"), &format!("{char}X"));
        }

        // Format &#x001abc;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:06x};"), &format!("{char}"));
            check(&format!("&#x{num:06x}; "), &format!("{char} "));
            check(&format!("&#x{num:06x};X"), &format!("{char}X"));
        }

        // Format &#x1ABC
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:X}"), &format!("{char}"));
            check(&format!("&#x{num:X} "), &format!("{char} "));
            check(&format!("&#x{num:X}X"), &format!("{char}X"));
        }

        // Format &#x001ABC
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:06X}"), &format!("{char}"));
            check(&format!("&#x{num:06X} "), &format!("{char} "));
            check(&format!("&#x{num:06X}X"), &format!("{char}X"));
        }

        // Format &#x1ABC;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:X};"), &format!("{char}"));
            check(&format!("&#x{num:X}; "), &format!("{char} "));
            check(&format!("&#x{num:X};X"), &format!("{char}X"));
        }

        // Format &#x001ABC;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#x{num:06X};"), &format!("{char}"));
            check(&format!("&#x{num:06X}; "), &format!("{char} "));
            check(&format!("&#x{num:06X};X"), &format!("{char}X"));
        }

        // Format &#X1abc;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#X{num:x};"), &format!("{char}"));
            check(&format!("&#X{num:x}; "), &format!("{char} "));
            check(&format!("&#X{num:x};X"), &format!("{char}X"));
        }

        // Format &#X001abc;
        for (num, char) in [
            (65, 'A'),
            (97, 'a'),
            (34, '"'),
            (38, '&'),
            (0x2603, '\u{2603}'),
            (0x101234, '\u{101234}'),
        ]
        .iter()
        {
            check(&format!("&#X{num:06x};"), &format!("{char}"));
            check(&format!("&#X{num:06x}; "), &format!("{char} "));
            check(&format!("&#X{num:06x};X"), &format!("{char}X"));
        }

        // Check invalid code points
        for cp in [0xD800, 0xDB00, 0xDC00, 0xDFFF, 0x110000] {
            check_num(cp, "\u{FFFD}");
        }

        // Check more invalid code points
        for cp in [0x1, 0xb, 0xe, 0x7f, 0xfffe, 0xffff, 0x10fffe, 0x10ffff] {
            check_num(cp, "");
        }

        // Check invalid numbers
        for (num, ch) in [(0x0d, "\r"), (0x80, "\u{20ac}"), (0x95, "\u{2022}")] {
            check_num(num, ch);
        }

        // Check small numbers
        check_num(0, "\u{FFFD}");
        check_num(9, "\t");

        // Check a big number
        check_num(1000000000000000000, "\u{FFFD}");

        // Check that multiple trailing semicolons are handled correctly
        for e in ["&quot;;", "&#34;;", "&#x22;;", "&#X22;;"] {
            check(e, "\";");
        }

        // Check that semicolons in the middle don't create problems
        for e in ["&quot;quot;", "&#34;quot;", "&#x22;quot;", "&#X22;quot;"] {
            check(e, "\"quot;");
        }

        // Check triple adjacent charrefs
        for e in ["&quot", "&#34", "&#x22", "&#X22"] {
            // check(&e.repeat(3), "\"\"\"");
            check(&format!("{e};").repeat(3), "\"\"\"");
        }

        // Check that the case is respected
        for e in ["&amp", "&amp;", "&AMP", "&AMP;"] {
            check(e, "&");
        }
        for e in ["&Amp", "&Amp;"] {
            check(e, e);
        }

        // Check that nonexistent named entities are returned unchanged
        check("&svadilfari;", "&svadilfari;");

        // The following examples are in the html5 specs
        check("&notit", "¬it");
        check("&notit;", "¬it;");
        check("&notin", "¬in");
        check("&notin;", "");

        // A similar example with a long name
        check(
            "&notReallyAnExistingNamedCharacterReference;",
            "¬ReallyAnExistingNamedCharacterReference;",
        );

        // Longest valid name
        check("&CounterClockwiseContourIntegral;", "");

        // Check a charref that maps to two unicode chars
        check("&acE;", "\u{223e}\u{333}");
        check("&acE", "&acE");

        // See Python #12888
        check(&"&#123; ".repeat(1050), &"{ ".repeat(1050));
        // See Python #15156
        check(
            "&Eacuteric&Eacute;ric&alphacentauri&alpha;centauri",
            "ÉricÉric&alphacentauriαcentauri",
        );
        check("&co;", "&co;");
    }
}