Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: url/gurl.cc

Issue 23835019: Support URL fragment resolution againt non-hierarchical schemes (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: actually restore PS4 this time Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifdef WIN32 5 #ifdef WIN32
6 #include <windows.h> 6 #include <windows.h>
7 #else 7 #else
8 #include <pthread.h> 8 #include <pthread.h>
9 #endif 9 #endif
10 10
11 #include <algorithm> 11 #include <algorithm>
12 #include <ostream> 12 #include <ostream>
13 13
14 #include "url/gurl.h" 14 #include "url/gurl.h"
15 15
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "url/url_canon_stdstring.h" 17 #include "url/url_canon_stdstring.h"
18 #include "url/url_util.h" 18 #include "url/url_util.h"
19 19
20 namespace { 20 // TODO(joth): Move to appropriate place in file, justing putting this here
brettw 2013/11/20 00:03:51 Thanks, I think you can move this now.
joth 2013/11/21 00:08:45 Done.
21 // to make initial code review iterations easier to diff.
21 22
22 // External template that can handle initialization of either character type. 23 // External template that can handle initialization of either character type.
23 // The input spec is given, and the canonical version will be placed in 24 // The input spec is given, and the canonical version will be placed in
24 // |*canonical|, along with the parsing of the canonical spec in |*parsed|. 25 // |*canonical|, along with the parsing of the canonical spec in |*parsed|.
25 template<typename STR> 26 template<typename STR>
26 bool InitCanonical(const STR& input_spec, 27 void GURL::InitCanonical(const STR& input_spec,
27 std::string* canonical, 28 bool trim_path_end) {
brettw 2013/11/20 00:03:51 One line.
joth 2013/11/21 00:08:45 Done.
28 url_parse::Parsed* parsed) {
29 // Reserve enough room in the output for the input, plus some extra so that 29 // Reserve enough room in the output for the input, plus some extra so that
30 // we have room if we have to escape a few things without reallocating. 30 // we have room if we have to escape a few things without reallocating.
31 canonical->reserve(input_spec.size() + 32); 31 spec_.reserve(input_spec.size() + 32);
32 url_canon::StdStringCanonOutput output(canonical); 32 url_canon::StdStringCanonOutput output(&spec_);
33 bool success = url_util::Canonicalize( 33 is_valid_ = url_util::Canonicalize(
34 input_spec.data(), static_cast<int>(input_spec.length()), 34 input_spec.data(), static_cast<int>(input_spec.length()), trim_path_end,
35 NULL, &output, parsed); 35 NULL, &output, &parsed_);
36 36
37 output.Complete(); // Must be done before using string. 37 output.Complete(); // Must be done before using string.
38 return success; 38 if (is_valid_ && SchemeIsFileSystem()) {
39 inner_url_.reset(new GURL(spec_.data(), parsed_.Length(),
40 *parsed_.inner_parsed(), true));
41 }
39 } 42 }
40 43
44 namespace {
45
41 static std::string* empty_string = NULL; 46 static std::string* empty_string = NULL;
42 static GURL* empty_gurl = NULL; 47 static GURL* empty_gurl = NULL;
43 48
44 #ifdef WIN32 49 #ifdef WIN32
45 50
46 // Returns a static reference to an empty string for returning a reference 51 // Returns a static reference to an empty string for returning a reference
47 // when there is no underlying string. 52 // when there is no underlying string.
48 const std::string& EmptyStringForGURL() { 53 const std::string& EmptyStringForGURL() {
49 // Avoid static object construction/destruction on startup/shutdown. 54 // Avoid static object construction/destruction on startup/shutdown.
50 if (!empty_string) { 55 if (!empty_string) {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 : spec_(other.spec_), 92 : spec_(other.spec_),
88 is_valid_(other.is_valid_), 93 is_valid_(other.is_valid_),
89 parsed_(other.parsed_) { 94 parsed_(other.parsed_) {
90 if (other.inner_url_) 95 if (other.inner_url_)
91 inner_url_.reset(new GURL(*other.inner_url_)); 96 inner_url_.reset(new GURL(*other.inner_url_));
92 // Valid filesystem urls should always have an inner_url_. 97 // Valid filesystem urls should always have an inner_url_.
93 DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_); 98 DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_);
94 } 99 }
95 100
96 GURL::GURL(const std::string& url_string) { 101 GURL::GURL(const std::string& url_string) {
97 is_valid_ = InitCanonical(url_string, &spec_, &parsed_); 102 InitCanonical(url_string, true);
98 if (is_valid_ && SchemeIsFileSystem()) {
99 inner_url_.reset(
100 new GURL(spec_.data(), parsed_.Length(),
101 *parsed_.inner_parsed(), true));
102 }
103 } 103 }
104 104
105 GURL::GURL(const base::string16& url_string) { 105 GURL::GURL(const base::string16& url_string) {
106 is_valid_ = InitCanonical(url_string, &spec_, &parsed_); 106 InitCanonical(url_string, true);
107 if (is_valid_ && SchemeIsFileSystem()) { 107 }
108 inner_url_.reset( 108
109 new GURL(spec_.data(), parsed_.Length(), 109 GURL::GURL(const std::string& url_string, RetainWhiteSpaceSelector) {
110 *parsed_.inner_parsed(), true)); 110 InitCanonical(url_string, false);
111 }
112 } 111 }
113 112
114 GURL::GURL(const char* canonical_spec, size_t canonical_spec_len, 113 GURL::GURL(const char* canonical_spec, size_t canonical_spec_len,
115 const url_parse::Parsed& parsed, bool is_valid) 114 const url_parse::Parsed& parsed, bool is_valid)
116 : spec_(canonical_spec, canonical_spec_len), 115 : spec_(canonical_spec, canonical_spec_len),
117 is_valid_(is_valid), 116 is_valid_(is_valid),
118 parsed_(parsed) { 117 parsed_(parsed) {
119 InitializeFromCanonicalSpec(); 118 InitializeFromCanonicalSpec();
120 } 119 }
121 120
(...skipping 17 matching lines...) Expand all
139 // what we would have produced. Skip checking for invalid URLs have no meaning 138 // what we would have produced. Skip checking for invalid URLs have no meaning
140 // and we can't always canonicalize then reproducabely. 139 // and we can't always canonicalize then reproducabely.
141 if (is_valid_) { 140 if (is_valid_) {
142 url_parse::Component scheme; 141 url_parse::Component scheme;
143 if (!url_util::FindAndCompareScheme(spec_.data(), spec_.length(), 142 if (!url_util::FindAndCompareScheme(spec_.data(), spec_.length(),
144 "filesystem", &scheme) || 143 "filesystem", &scheme) ||
145 scheme.begin == parsed_.scheme.begin) { 144 scheme.begin == parsed_.scheme.begin) {
146 // We can't do this check on the inner_url of a filesystem URL, as 145 // We can't do this check on the inner_url of a filesystem URL, as
147 // canonical_spec actually points to the start of the outer URL, so we'd 146 // canonical_spec actually points to the start of the outer URL, so we'd
148 // end up with infinite recursion in this constructor. 147 // end up with infinite recursion in this constructor.
149 GURL test_url(spec_); 148 GURL test_url(spec_, RETAIN_TRAILING_PATH_WHITEPACE);
joth 2013/11/21 00:08:45 I added an extra comment here too: // We nee
150 149
151 DCHECK(test_url.is_valid_ == is_valid_); 150 DCHECK(test_url.is_valid_ == is_valid_);
152 DCHECK(test_url.spec_ == spec_); 151 DCHECK(test_url.spec_ == spec_);
153 152
154 DCHECK(test_url.parsed_.scheme == parsed_.scheme); 153 DCHECK(test_url.parsed_.scheme == parsed_.scheme);
155 DCHECK(test_url.parsed_.username == parsed_.username); 154 DCHECK(test_url.parsed_.username == parsed_.username);
156 DCHECK(test_url.parsed_.password == parsed_.password); 155 DCHECK(test_url.parsed_.password == parsed_.password);
157 DCHECK(test_url.parsed_.host == parsed_.host); 156 DCHECK(test_url.parsed_.host == parsed_.host);
158 DCHECK(test_url.parsed_.port == parsed_.port); 157 DCHECK(test_url.parsed_.port == parsed_.port);
159 DCHECK(test_url.parsed_.path == parsed_.path); 158 DCHECK(test_url.parsed_.path == parsed_.path);
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
509 void GURL::Swap(GURL* other) { 508 void GURL::Swap(GURL* other) {
510 spec_.swap(other->spec_); 509 spec_.swap(other->spec_);
511 std::swap(is_valid_, other->is_valid_); 510 std::swap(is_valid_, other->is_valid_);
512 std::swap(parsed_, other->parsed_); 511 std::swap(parsed_, other->parsed_);
513 inner_url_.swap(other->inner_url_); 512 inner_url_.swap(other->inner_url_);
514 } 513 }
515 514
516 std::ostream& operator<<(std::ostream& out, const GURL& url) { 515 std::ostream& operator<<(std::ostream& out, const GURL& url) {
517 return out << url.possibly_invalid_spec(); 516 return out << url.possibly_invalid_spec();
518 } 517 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698