gurl.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // Copyright 2013 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. #include "url/gurl.h"
  5. #include <stddef.h>
  6. #include <algorithm>
  7. #include <memory>
  8. #include <ostream>
  9. #include <utility>
  10. #include "base/check_op.h"
  11. #include "base/no_destructor.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/strings/string_util.h"
  14. #include "base/trace_event/memory_usage_estimator.h"
  15. #include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
  16. #include "url/url_canon_stdstring.h"
  17. #include "url/url_util.h"
  18. GURL::GURL() : is_valid_(false) {
  19. }
  20. GURL::GURL(const GURL& other)
  21. : spec_(other.spec_),
  22. is_valid_(other.is_valid_),
  23. parsed_(other.parsed_) {
  24. if (other.inner_url_)
  25. inner_url_ = std::make_unique<GURL>(*other.inner_url_);
  26. // Valid filesystem urls should always have an inner_url_.
  27. DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_);
  28. }
  29. GURL::GURL(GURL&& other) noexcept
  30. : spec_(std::move(other.spec_)),
  31. is_valid_(other.is_valid_),
  32. parsed_(other.parsed_),
  33. inner_url_(std::move(other.inner_url_)) {
  34. other.is_valid_ = false;
  35. other.parsed_ = url::Parsed();
  36. }
  37. GURL::GURL(base::StringPiece url_string) {
  38. InitCanonical(url_string, true);
  39. }
  40. GURL::GURL(base::StringPiece16 url_string) {
  41. InitCanonical(url_string, true);
  42. }
  43. GURL::GURL(const std::string& url_string, RetainWhiteSpaceSelector) {
  44. InitCanonical(url_string, false);
  45. }
  46. GURL::GURL(const char* canonical_spec,
  47. size_t canonical_spec_len,
  48. const url::Parsed& parsed,
  49. bool is_valid)
  50. : spec_(canonical_spec, canonical_spec_len),
  51. is_valid_(is_valid),
  52. parsed_(parsed) {
  53. InitializeFromCanonicalSpec();
  54. }
  55. GURL::GURL(std::string canonical_spec, const url::Parsed& parsed, bool is_valid)
  56. : spec_(std::move(canonical_spec)), is_valid_(is_valid), parsed_(parsed) {
  57. InitializeFromCanonicalSpec();
  58. }
  59. template <typename T, typename CharT>
  60. void GURL::InitCanonical(T input_spec, bool trim_path_end) {
  61. url::StdStringCanonOutput output(&spec_);
  62. is_valid_ = url::Canonicalize(
  63. input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end,
  64. NULL, &output, &parsed_);
  65. output.Complete(); // Must be done before using string.
  66. if (is_valid_ && SchemeIsFileSystem()) {
  67. inner_url_ = std::make_unique<GURL>(spec_.data(), parsed_.Length(),
  68. *parsed_.inner_parsed(), true);
  69. }
  70. // Valid URLs always have non-empty specs.
  71. DCHECK(!is_valid_ || !spec_.empty());
  72. }
  73. void GURL::InitializeFromCanonicalSpec() {
  74. if (is_valid_ && SchemeIsFileSystem()) {
  75. inner_url_ = std::make_unique<GURL>(spec_.data(), parsed_.Length(),
  76. *parsed_.inner_parsed(), true);
  77. }
  78. #ifndef NDEBUG
  79. // For testing purposes, check that the parsed canonical URL is identical to
  80. // what we would have produced. Skip checking for invalid URLs have no meaning
  81. // and we can't always canonicalize then reproducibly.
  82. if (is_valid_) {
  83. DCHECK(!spec_.empty());
  84. url::Component scheme;
  85. // We can't do this check on the inner_url of a filesystem URL, as
  86. // canonical_spec actually points to the start of the outer URL, so we'd
  87. // end up with infinite recursion in this constructor.
  88. if (!url::FindAndCompareScheme(spec_.data(), spec_.length(),
  89. url::kFileSystemScheme, &scheme) ||
  90. scheme.begin == parsed_.scheme.begin) {
  91. // We need to retain trailing whitespace on path URLs, as the |parsed_|
  92. // spec we originally received may legitimately contain trailing white-
  93. // space on the path or components e.g. if the #ref has been
  94. // removed from a "foo:hello #ref" URL (see http://crbug.com/291747).
  95. GURL test_url(spec_, RETAIN_TRAILING_PATH_WHITEPACE);
  96. DCHECK_EQ(test_url.is_valid_, is_valid_);
  97. DCHECK_EQ(test_url.spec_, spec_);
  98. DCHECK_EQ(test_url.parsed_.scheme, parsed_.scheme);
  99. DCHECK_EQ(test_url.parsed_.username, parsed_.username);
  100. DCHECK_EQ(test_url.parsed_.password, parsed_.password);
  101. DCHECK_EQ(test_url.parsed_.host, parsed_.host);
  102. DCHECK_EQ(test_url.parsed_.port, parsed_.port);
  103. DCHECK_EQ(test_url.parsed_.path, parsed_.path);
  104. DCHECK_EQ(test_url.parsed_.query, parsed_.query);
  105. DCHECK_EQ(test_url.parsed_.ref, parsed_.ref);
  106. }
  107. }
  108. #endif
  109. }
  110. GURL::~GURL() = default;
  111. GURL& GURL::operator=(const GURL& other) {
  112. spec_ = other.spec_;
  113. is_valid_ = other.is_valid_;
  114. parsed_ = other.parsed_;
  115. if (!other.inner_url_)
  116. inner_url_.reset();
  117. else if (inner_url_)
  118. *inner_url_ = *other.inner_url_;
  119. else
  120. inner_url_ = std::make_unique<GURL>(*other.inner_url_);
  121. return *this;
  122. }
  123. GURL& GURL::operator=(GURL&& other) noexcept {
  124. spec_ = std::move(other.spec_);
  125. is_valid_ = other.is_valid_;
  126. parsed_ = other.parsed_;
  127. inner_url_ = std::move(other.inner_url_);
  128. other.is_valid_ = false;
  129. other.parsed_ = url::Parsed();
  130. return *this;
  131. }
  132. const std::string& GURL::spec() const {
  133. if (is_valid_ || spec_.empty())
  134. return spec_;
  135. DCHECK(false) << "Trying to get the spec of an invalid URL!";
  136. return base::EmptyString();
  137. }
  138. bool GURL::operator<(const GURL& other) const {
  139. return spec_ < other.spec_;
  140. }
  141. bool GURL::operator>(const GURL& other) const {
  142. return spec_ > other.spec_;
  143. }
  144. // Note: code duplicated below (it's inconvenient to use a template here).
  145. GURL GURL::Resolve(base::StringPiece relative) const {
  146. // Not allowed for invalid URLs.
  147. if (!is_valid_)
  148. return GURL();
  149. GURL result;
  150. url::StdStringCanonOutput output(&result.spec_);
  151. if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
  152. parsed_, relative.data(),
  153. static_cast<int>(relative.length()),
  154. nullptr, &output, &result.parsed_)) {
  155. // Error resolving, return an empty URL.
  156. return GURL();
  157. }
  158. output.Complete();
  159. result.is_valid_ = true;
  160. if (result.SchemeIsFileSystem()) {
  161. result.inner_url_ =
  162. std::make_unique<GURL>(result.spec_.data(), result.parsed_.Length(),
  163. *result.parsed_.inner_parsed(), true);
  164. }
  165. return result;
  166. }
  167. // Note: code duplicated above (it's inconvenient to use a template here).
  168. GURL GURL::Resolve(base::StringPiece16 relative) const {
  169. // Not allowed for invalid URLs.
  170. if (!is_valid_)
  171. return GURL();
  172. GURL result;
  173. url::StdStringCanonOutput output(&result.spec_);
  174. if (!url::ResolveRelative(spec_.data(), static_cast<int>(spec_.length()),
  175. parsed_, relative.data(),
  176. static_cast<int>(relative.length()),
  177. nullptr, &output, &result.parsed_)) {
  178. // Error resolving, return an empty URL.
  179. return GURL();
  180. }
  181. output.Complete();
  182. result.is_valid_ = true;
  183. if (result.SchemeIsFileSystem()) {
  184. result.inner_url_ =
  185. std::make_unique<GURL>(result.spec_.data(), result.parsed_.Length(),
  186. *result.parsed_.inner_parsed(), true);
  187. }
  188. return result;
  189. }
  190. // Note: code duplicated below (it's inconvenient to use a template here).
  191. GURL GURL::ReplaceComponents(const Replacements& replacements) const {
  192. GURL result;
  193. // Not allowed for invalid URLs.
  194. if (!is_valid_)
  195. return GURL();
  196. url::StdStringCanonOutput output(&result.spec_);
  197. result.is_valid_ = url::ReplaceComponents(
  198. spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
  199. NULL, &output, &result.parsed_);
  200. output.Complete();
  201. result.ProcessFileSystemURLAfterReplaceComponents();
  202. return result;
  203. }
  204. // Note: code duplicated above (it's inconvenient to use a template here).
  205. GURL GURL::ReplaceComponents(const ReplacementsW& replacements) const {
  206. GURL result;
  207. // Not allowed for invalid URLs.
  208. if (!is_valid_)
  209. return GURL();
  210. url::StdStringCanonOutput output(&result.spec_);
  211. result.is_valid_ = url::ReplaceComponents(
  212. spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements,
  213. NULL, &output, &result.parsed_);
  214. output.Complete();
  215. result.ProcessFileSystemURLAfterReplaceComponents();
  216. return result;
  217. }
  218. void GURL::ProcessFileSystemURLAfterReplaceComponents() {
  219. if (!is_valid_)
  220. return;
  221. if (SchemeIsFileSystem()) {
  222. inner_url_ = std::make_unique<GURL>(spec_.data(), parsed_.Length(),
  223. *parsed_.inner_parsed(), true);
  224. }
  225. }
  226. GURL GURL::DeprecatedGetOriginAsURL() const {
  227. // This doesn't make sense for invalid or nonstandard URLs, so return
  228. // the empty URL.
  229. if (!is_valid_ || !IsStandard())
  230. return GURL();
  231. if (SchemeIsFileSystem())
  232. return inner_url_->DeprecatedGetOriginAsURL();
  233. Replacements replacements;
  234. replacements.ClearUsername();
  235. replacements.ClearPassword();
  236. replacements.ClearPath();
  237. replacements.ClearQuery();
  238. replacements.ClearRef();
  239. return ReplaceComponents(replacements);
  240. }
  241. GURL GURL::GetAsReferrer() const {
  242. if (!is_valid() || !IsReferrerScheme(spec_.data(), parsed_.scheme))
  243. return GURL();
  244. if (!has_ref() && !has_username() && !has_password())
  245. return GURL(*this);
  246. Replacements replacements;
  247. replacements.ClearRef();
  248. replacements.ClearUsername();
  249. replacements.ClearPassword();
  250. return ReplaceComponents(replacements);
  251. }
  252. GURL GURL::GetWithEmptyPath() const {
  253. // This doesn't make sense for invalid or nonstandard URLs, so return
  254. // the empty URL.
  255. if (!is_valid_ || !IsStandard())
  256. return GURL();
  257. // We could optimize this since we know that the URL is canonical, and we are
  258. // appending a canonical path, so avoiding re-parsing.
  259. GURL other(*this);
  260. if (parsed_.path.len == 0)
  261. return other;
  262. // Clear everything after the path.
  263. other.parsed_.query.reset();
  264. other.parsed_.ref.reset();
  265. // Set the path, since the path is longer than one, we can just set the
  266. // first character and resize.
  267. other.spec_[other.parsed_.path.begin] = '/';
  268. other.parsed_.path.len = 1;
  269. other.spec_.resize(other.parsed_.path.begin + 1);
  270. return other;
  271. }
  272. GURL GURL::GetWithoutFilename() const {
  273. return Resolve(".");
  274. }
  275. bool GURL::IsStandard() const {
  276. return url::IsStandard(spec_.data(), parsed_.scheme);
  277. }
  278. bool GURL::IsAboutBlank() const {
  279. return IsAboutUrl(url::kAboutBlankPath);
  280. }
  281. bool GURL::IsAboutSrcdoc() const {
  282. return IsAboutUrl(url::kAboutSrcdocPath);
  283. }
  284. bool GURL::SchemeIs(base::StringPiece lower_ascii_scheme) const {
  285. DCHECK(base::IsStringASCII(lower_ascii_scheme));
  286. DCHECK(base::ToLowerASCII(lower_ascii_scheme) == lower_ascii_scheme);
  287. if (!has_scheme())
  288. return lower_ascii_scheme.empty();
  289. return scheme_piece() == lower_ascii_scheme;
  290. }
  291. bool GURL::SchemeIsHTTPOrHTTPS() const {
  292. return SchemeIs(url::kHttpScheme) || SchemeIs(url::kHttpsScheme);
  293. }
  294. bool GURL::SchemeIsWSOrWSS() const {
  295. return SchemeIs(url::kWsScheme) || SchemeIs(url::kWssScheme);
  296. }
  297. bool GURL::SchemeIsCryptographic() const {
  298. if (!has_scheme())
  299. return false;
  300. return SchemeIsCryptographic(scheme_piece());
  301. }
  302. bool GURL::SchemeIsCryptographic(base::StringPiece lower_ascii_scheme) {
  303. DCHECK(base::IsStringASCII(lower_ascii_scheme));
  304. DCHECK(base::ToLowerASCII(lower_ascii_scheme) == lower_ascii_scheme);
  305. return lower_ascii_scheme == url::kHttpsScheme ||
  306. lower_ascii_scheme == url::kWssScheme;
  307. }
  308. bool GURL::SchemeIsLocal() const {
  309. // The `filesystem:` scheme is not in the Fetch spec, but Chromium still
  310. // supports it in large part. It should be treated as a local scheme too.
  311. return SchemeIs(url::kAboutScheme) || SchemeIs(url::kBlobScheme) ||
  312. SchemeIs(url::kDataScheme) || SchemeIs(url::kFileSystemScheme);
  313. }
  314. int GURL::IntPort() const {
  315. if (parsed_.port.is_nonempty())
  316. return url::ParsePort(spec_.data(), parsed_.port);
  317. return url::PORT_UNSPECIFIED;
  318. }
  319. int GURL::EffectiveIntPort() const {
  320. int int_port = IntPort();
  321. if (int_port == url::PORT_UNSPECIFIED && IsStandard())
  322. return url::DefaultPortForScheme(spec_.data() + parsed_.scheme.begin,
  323. parsed_.scheme.len);
  324. return int_port;
  325. }
  326. std::string GURL::ExtractFileName() const {
  327. url::Component file_component;
  328. url::ExtractFileName(spec_.data(), parsed_.path, &file_component);
  329. return ComponentString(file_component);
  330. }
  331. base::StringPiece GURL::PathForRequestPiece() const {
  332. DCHECK(parsed_.path.len > 0)
  333. << "Canonical path for requests should be non-empty";
  334. if (parsed_.ref.len >= 0) {
  335. // Clip off the reference when it exists. The reference starts after the
  336. // #-sign, so we have to subtract one to also remove it.
  337. return base::StringPiece(&spec_[parsed_.path.begin],
  338. parsed_.ref.begin - parsed_.path.begin - 1);
  339. }
  340. // Compute the actual path length, rather than depending on the spec's
  341. // terminator. If we're an inner_url, our spec continues on into our outer
  342. // URL's path/query/ref.
  343. int path_len = parsed_.path.len;
  344. if (parsed_.query.is_valid())
  345. path_len = parsed_.query.end() - parsed_.path.begin;
  346. return base::StringPiece(&spec_[parsed_.path.begin], path_len);
  347. }
  348. std::string GURL::PathForRequest() const {
  349. return std::string(PathForRequestPiece());
  350. }
  351. std::string GURL::HostNoBrackets() const {
  352. return std::string(HostNoBracketsPiece());
  353. }
  354. base::StringPiece GURL::HostNoBracketsPiece() const {
  355. // If host looks like an IPv6 literal, strip the square brackets.
  356. url::Component h(parsed_.host);
  357. if (h.len >= 2 && spec_[h.begin] == '[' && spec_[h.end() - 1] == ']') {
  358. h.begin++;
  359. h.len -= 2;
  360. }
  361. return ComponentStringPiece(h);
  362. }
  363. std::string GURL::GetContent() const {
  364. return std::string(GetContentPiece());
  365. }
  366. base::StringPiece GURL::GetContentPiece() const {
  367. if (!is_valid_)
  368. return base::StringPiece();
  369. url::Component content_component = parsed_.GetContent();
  370. if (!SchemeIs(url::kJavaScriptScheme) && parsed_.ref.len >= 0)
  371. content_component.len -= parsed_.ref.len + 1;
  372. return ComponentStringPiece(content_component);
  373. }
  374. bool GURL::HostIsIPAddress() const {
  375. return is_valid_ && url::HostIsIPAddress(host_piece());
  376. }
  377. const GURL& GURL::EmptyGURL() {
  378. static base::NoDestructor<GURL> empty_gurl;
  379. return *empty_gurl;
  380. }
  381. bool GURL::DomainIs(base::StringPiece canonical_domain) const {
  382. if (!is_valid_)
  383. return false;
  384. // FileSystem URLs have empty host_piece, so check this first.
  385. if (inner_url_ && SchemeIsFileSystem())
  386. return inner_url_->DomainIs(canonical_domain);
  387. return url::DomainIs(host_piece(), canonical_domain);
  388. }
  389. bool GURL::EqualsIgnoringRef(const GURL& other) const {
  390. int ref_position = parsed_.CountCharactersBefore(url::Parsed::REF, true);
  391. int ref_position_other =
  392. other.parsed_.CountCharactersBefore(url::Parsed::REF, true);
  393. return base::StringPiece(spec_).substr(0, ref_position) ==
  394. base::StringPiece(other.spec_).substr(0, ref_position_other);
  395. }
  396. void GURL::Swap(GURL* other) {
  397. spec_.swap(other->spec_);
  398. std::swap(is_valid_, other->is_valid_);
  399. std::swap(parsed_, other->parsed_);
  400. inner_url_.swap(other->inner_url_);
  401. }
  402. size_t GURL::EstimateMemoryUsage() const {
  403. return base::trace_event::EstimateMemoryUsage(spec_) +
  404. base::trace_event::EstimateMemoryUsage(inner_url_) +
  405. (parsed_.inner_parsed() ? sizeof(url::Parsed) : 0);
  406. }
  407. bool GURL::IsAboutUrl(base::StringPiece allowed_path) const {
  408. if (!SchemeIs(url::kAboutScheme))
  409. return false;
  410. if (has_host() || has_username() || has_password() || has_port())
  411. return false;
  412. return IsAboutPath(path_piece(), allowed_path);
  413. }
  414. // static
  415. bool GURL::IsAboutPath(base::StringPiece actual_path,
  416. base::StringPiece allowed_path) {
  417. if (!base::StartsWith(actual_path, allowed_path))
  418. return false;
  419. if (actual_path.size() == allowed_path.size()) {
  420. DCHECK_EQ(actual_path, allowed_path);
  421. return true;
  422. }
  423. if ((actual_path.size() == allowed_path.size() + 1) &&
  424. actual_path.back() == '/') {
  425. DCHECK_EQ(actual_path, std::string(allowed_path) + '/');
  426. return true;
  427. }
  428. return false;
  429. }
  430. void GURL::WriteIntoTrace(perfetto::TracedValue context) const {
  431. std::move(context).WriteString(possibly_invalid_spec());
  432. }
  433. std::ostream& operator<<(std::ostream& out, const GURL& url) {
  434. return out << url.possibly_invalid_spec();
  435. }
  436. bool operator==(const GURL& x, const GURL& y) {
  437. return x.possibly_invalid_spec() == y.possibly_invalid_spec();
  438. }
  439. bool operator!=(const GURL& x, const GURL& y) {
  440. return !(x == y);
  441. }
  442. bool operator==(const GURL& x, const base::StringPiece& spec) {
  443. DCHECK_EQ(GURL(spec).possibly_invalid_spec(), spec)
  444. << "Comparisons of GURLs and strings must ensure as a precondition that "
  445. "the string is fully canonicalized.";
  446. return x.possibly_invalid_spec() == spec;
  447. }
  448. bool operator==(const base::StringPiece& spec, const GURL& x) {
  449. return x == spec;
  450. }
  451. bool operator!=(const GURL& x, const base::StringPiece& spec) {
  452. return !(x == spec);
  453. }
  454. bool operator!=(const base::StringPiece& spec, const GURL& x) {
  455. return !(x == spec);
  456. }
  457. namespace url::debug {
  458. ScopedUrlCrashKey::ScopedUrlCrashKey(base::debug::CrashKeyString* crash_key,
  459. const GURL& url)
  460. : scoped_string_value_(
  461. crash_key,
  462. url.is_empty() ? "<empty url>" : url.possibly_invalid_spec()) {}
  463. ScopedUrlCrashKey::~ScopedUrlCrashKey() = default;
  464. } // namespace url::debug