/src/serenity/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/GenericLexer.h> |
8 | | #include <AK/String.h> |
9 | | #include <AK/StringBuilder.h> |
10 | | #include <LibWeb/Fetch/Infrastructure/HTTP.h> |
11 | | |
12 | | namespace Web::Fetch::Infrastructure { |
13 | | |
14 | | // https://fetch.spec.whatwg.org/#collect-an-http-quoted-string |
15 | | String collect_an_http_quoted_string(GenericLexer& lexer, HttpQuotedStringExtractValue extract_value) |
16 | 0 | { |
17 | | // To collect an HTTP quoted string from a string input, given a position variable position and optionally an extract-value flag, run these steps: |
18 | | // 1. Let positionStart be position. |
19 | 0 | auto position_start = lexer.tell(); |
20 | | |
21 | | // 2. Let value be the empty string. |
22 | 0 | StringBuilder value; |
23 | | |
24 | | // 3. Assert: the code point at position within input is U+0022 ("). |
25 | 0 | VERIFY(lexer.peek() == '"'); |
26 | | |
27 | | // 4. Advance position by 1. |
28 | 0 | lexer.ignore(1); |
29 | | |
30 | | // 5. While true: |
31 | 0 | while (true) { |
32 | | // 1. Append the result of collecting a sequence of code points that are not U+0022 (") or U+005C (\) from input, given position, to value. |
33 | 0 | auto value_part = lexer.consume_until([](char ch) { |
34 | 0 | return ch == '"' || ch == '\\'; |
35 | 0 | }); |
36 | |
|
37 | 0 | value.append(value_part); |
38 | | |
39 | | // 2. If position is past the end of input, then break. |
40 | 0 | if (lexer.is_eof()) |
41 | 0 | break; |
42 | | |
43 | | // 3. Let quoteOrBackslash be the code point at position within input. |
44 | | // 4. Advance position by 1. |
45 | 0 | char quote_or_backslash = lexer.consume(); |
46 | | |
47 | | // 5. If quoteOrBackslash is U+005C (\), then: |
48 | 0 | if (quote_or_backslash == '\\') { |
49 | | // 1. If position is past the end of input, then append U+005C (\) to value and break. |
50 | 0 | if (lexer.is_eof()) { |
51 | 0 | value.append('\\'); |
52 | 0 | break; |
53 | 0 | } |
54 | | |
55 | | // 2. Append the code point at position within input to value. |
56 | | // 3. Advance position by 1. |
57 | 0 | value.append(lexer.consume()); |
58 | 0 | } |
59 | | |
60 | | // 6. Otherwise: |
61 | 0 | else { |
62 | | // 1. Assert: quoteOrBackslash is U+0022 ("). |
63 | 0 | VERIFY(quote_or_backslash == '"'); |
64 | | |
65 | | // 2. Break. |
66 | 0 | break; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | // 6. If the extract-value flag is set, then return value. |
71 | 0 | if (extract_value == HttpQuotedStringExtractValue::Yes) |
72 | 0 | return MUST(value.to_string()); |
73 | | |
74 | | // 7. Return the code points from positionStart to position, inclusive, within input. |
75 | 0 | return MUST(String::from_utf8(lexer.input().substring_view(position_start, lexer.tell() - position_start))); |
76 | 0 | } |
77 | | |
78 | | } |