xref: /aosp_15_r20/external/openscreen/util/url.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "util/url.h"
6 
7 #include <limits.h>
8 
9 #include <utility>
10 
11 #include "third_party/mozilla/url_parse.h"
12 #include "third_party/mozilla/url_parse_internal.h"
13 
14 namespace openscreen {
15 
Url(const std::string & source)16 Url::Url(const std::string& source) {
17   Parsed parsed;
18   Component scheme;
19   const char* url = source.c_str();
20   size_t length = source.size();
21   if (length > INT_MAX) {
22     return;
23   }
24   int url_length = static_cast<int>(length);
25 
26   if (!ExtractScheme(url, url_length, &scheme)) {
27     return;
28   }
29 
30   if (CompareSchemeComponent(url, scheme, kFileScheme) ||
31       CompareSchemeComponent(url, scheme, kFileSystemScheme) ||
32       CompareSchemeComponent(url, scheme, kMailtoScheme)) {
33     // NOTE: Special schemes that are unsupported.
34     return;
35   } else if (IsStandard(url, scheme)) {
36     ParseStandardURL(url, url_length, &parsed);
37     if (!parsed.host.is_valid()) {
38       return;
39     }
40   } else {
41     ParsePathURL(url, url_length, true, &parsed);
42   }
43 
44   if (!parsed.scheme.is_nonempty()) {
45     return;
46   }
47   scheme_ = std::string(url + parsed.scheme.begin, url + parsed.scheme.end());
48 
49   if (parsed.host.is_valid()) {
50     has_host_ = true;
51     host_ = std::string(url + parsed.host.begin, url + parsed.host.end());
52   }
53 
54   if (parsed.port.is_nonempty()) {
55     int parse_result = ParsePort(url, parsed.port);
56     if (parse_result == PORT_INVALID) {
57       return;
58     } else if (parse_result >= 0) {
59       has_port_ = true;
60       port_ = parse_result;
61     }
62   }
63 
64   if (parsed.path.is_nonempty()) {
65     has_path_ = true;
66     path_ = std::string(url + parsed.path.begin, url + parsed.path.end());
67   }
68 
69   if (parsed.query.is_nonempty()) {
70     has_query_ = true;
71     query_ = std::string(url + parsed.query.begin, url + parsed.query.end());
72   }
73 
74   is_valid_ = true;
75 }
76 
77 Url::Url(const Url&) = default;
78 
Url(Url && other)79 Url::Url(Url&& other) noexcept
80     : is_valid_(other.is_valid_),
81       has_host_(other.has_host_),
82       has_port_(other.has_port_),
83       has_path_(other.has_path_),
84       has_query_(other.has_query_),
85       scheme_(std::move(other.scheme_)),
86       host_(std::move(other.host_)),
87       port_(other.port_),
88       path_(std::move(other.path_)),
89       query_(std::move(other.query_)) {
90   other.is_valid_ = false;
91 }
92 
93 Url::~Url() = default;
94 
95 Url& Url::operator=(const Url&) = default;
96 
operator =(Url && other)97 Url& Url::operator=(Url&& other) {
98   is_valid_ = other.is_valid_;
99   has_host_ = other.has_host_;
100   has_port_ = other.has_port_;
101   has_path_ = other.has_path_;
102   has_query_ = other.has_query_;
103   scheme_ = std::move(other.scheme_);
104   host_ = std::move(other.host_);
105   port_ = other.port_;
106   path_ = std::move(other.path_);
107   query_ = std::move(other.query_);
108   other.is_valid_ = false;
109   return *this;
110 }
111 
112 }  // namespace openscreen
113