/src/serenity/Userland/Libraries/LibURL/Origin.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibURL/Origin.h> |
8 | | #include <LibURL/Parser.h> |
9 | | |
10 | | namespace URL { |
11 | | |
12 | | // https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin |
13 | | ByteString Origin::serialize() const |
14 | 0 | { |
15 | | // 1. If origin is an opaque origin, then return "null" |
16 | 0 | if (is_opaque()) |
17 | 0 | return "null"; |
18 | | |
19 | | // 2. Otherwise, let result be origin's scheme. |
20 | 0 | StringBuilder result; |
21 | 0 | result.append(scheme()); |
22 | | |
23 | | // 3. Append "://" to result. |
24 | 0 | result.append("://"sv); |
25 | | |
26 | | // 4. Append origin's host, serialized, to result. |
27 | 0 | result.append(Parser::serialize_host(host()).release_value_but_fixme_should_propagate_errors().to_byte_string()); |
28 | | |
29 | | // 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result. |
30 | 0 | if (port().has_value()) { |
31 | 0 | result.append(':'); |
32 | 0 | result.append(ByteString::number(*port())); |
33 | 0 | } |
34 | | // 6. Return result |
35 | 0 | return result.to_byte_string(); |
36 | 0 | } |
37 | | |
38 | | } |
39 | | |
40 | | namespace AK { |
41 | | |
42 | | unsigned Traits<URL::Origin>::hash(URL::Origin const& origin) |
43 | 0 | { |
44 | 0 | unsigned hash = origin.scheme().hash(); |
45 | |
|
46 | 0 | if (origin.port().has_value()) |
47 | 0 | hash = pair_int_hash(hash, *origin.port()); |
48 | |
|
49 | 0 | if (!origin.host().has<Empty>()) |
50 | 0 | hash = pair_int_hash(hash, URL::Parser::serialize_host(origin.host()).release_value_but_fixme_should_propagate_errors().hash()); |
51 | |
|
52 | 0 | return hash; |
53 | 0 | } |
54 | | |
55 | | } // namespace AK |