OLD | NEW |
(Empty) | |
| 1 // Copyright 2007, Google Inc. |
| 2 // All rights reserved. |
| 3 // |
| 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are |
| 6 // met: |
| 7 // |
| 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above |
| 11 // copyright notice, this list of conditions and the following disclaimer |
| 12 // in the documentation and/or other materials provided with the |
| 13 // distribution. |
| 14 // * Neither the name of Google Inc. nor the names of its |
| 15 // contributors may be used to endorse or promote products derived from |
| 16 // this software without specific prior written permission. |
| 17 // |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
| 30 #ifdef WIN32 |
| 31 #include <windows.h> |
| 32 #else |
| 33 #include <pthread.h> |
| 34 #endif |
| 35 |
| 36 #include <algorithm> |
| 37 #include <ostream> |
| 38 |
| 39 #include "googleurl/src/gurl.h" |
| 40 |
| 41 #include "base/logging.h" |
| 42 #include "googleurl/src/url_canon_stdstring.h" |
| 43 #include "googleurl/src/url_util.h" |
| 44 |
| 45 namespace { |
| 46 |
| 47 // External template that can handle initialization of either character type. |
| 48 // The input spec is given, and the canonical version will be placed in |
| 49 // |*canonical|, along with the parsing of the canonical spec in |*parsed|. |
| 50 template<typename STR> |
| 51 bool InitCanonical(const STR& input_spec, |
| 52 std::string* canonical, |
| 53 url_parse::Parsed* parsed) { |
| 54 // Reserve enough room in the output for the input, plus some extra so that |
| 55 // we have room if we have to escape a few things without reallocating. |
| 56 canonical->reserve(input_spec.size() + 32); |
| 57 url_canon::StdStringCanonOutput output(canonical); |
| 58 bool success = url_util::Canonicalize( |
| 59 input_spec.data(), static_cast<int>(input_spec.length()), |
| 60 NULL, &output, parsed); |
| 61 |
| 62 output.Complete(); // Must be done before using string. |
| 63 return success; |
| 64 } |
| 65 |
| 66 static std::string* empty_string = NULL; |
| 67 static GURL* empty_gurl = NULL; |
| 68 |
| 69 #ifdef WIN32 |
| 70 |
| 71 // Returns a static reference to an empty string for returning a reference |
| 72 // when there is no underlying string. |
| 73 const std::string& EmptyStringForGURL() { |
| 74 // Avoid static object construction/destruction on startup/shutdown. |
| 75 if (!empty_string) { |
| 76 // Create the string. Be careful that we don't break in the case that this |
| 77 // is being called from multiple threads. Statics are not threadsafe. |
| 78 std::string* new_empty_string = new std::string; |
| 79 if (InterlockedCompareExchangePointer( |
| 80 reinterpret_cast<PVOID*>(&empty_string), new_empty_string, NULL)) { |
| 81 // The old value was non-NULL, so no replacement was done. Another |
| 82 // thread did the initialization out from under us. |
| 83 delete new_empty_string; |
| 84 } |
| 85 } |
| 86 return *empty_string; |
| 87 } |
| 88 |
| 89 #else |
| 90 |
| 91 static pthread_once_t empty_string_once = PTHREAD_ONCE_INIT; |
| 92 static pthread_once_t empty_gurl_once = PTHREAD_ONCE_INIT; |
| 93 |
| 94 void EmptyStringForGURLOnce(void) { |
| 95 empty_string = new std::string; |
| 96 } |
| 97 |
| 98 const std::string& EmptyStringForGURL() { |
| 99 // Avoid static object construction/destruction on startup/shutdown. |
| 100 pthread_once(&empty_string_once, EmptyStringForGURLOnce); |
| 101 return *empty_string; |
| 102 } |
| 103 |
| 104 #endif // WIN32 |
| 105 |
| 106 } // namespace |
| 107 |
| 108 GURL::GURL() : is_valid_(false), inner_url_(NULL) { |
| 109 } |
| 110 |
| 111 GURL::GURL(const GURL& other) |
| 112 : spec_(other.spec_), |
| 113 is_valid_(other.is_valid_), |
| 114 parsed_(other.parsed_), |
| 115 inner_url_(NULL) { |
| 116 if (other.inner_url_) |
| 117 inner_url_ = new GURL(*other.inner_url_); |
| 118 // Valid filesystem urls should always have an inner_url_. |
| 119 DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_); |
| 120 } |
| 121 |
| 122 GURL::GURL(const std::string& url_string) : inner_url_(NULL) { |
| 123 is_valid_ = InitCanonical(url_string, &spec_, &parsed_); |
| 124 if (is_valid_ && SchemeIsFileSystem()) { |
| 125 inner_url_ = |
| 126 new GURL(spec_.data(), parsed_.Length(), *parsed_.inner_parsed(), true); |
| 127 } |
| 128 } |
| 129 |
| 130 GURL::GURL(const string16& url_string) : inner_url_(NULL) { |
| 131 is_valid_ = InitCanonical(url_string, &spec_, &parsed_); |
| 132 if (is_valid_ && SchemeIsFileSystem()) { |
| 133 inner_url_ = |
| 134 new GURL(spec_.data(), parsed_.Length(), *parsed_.inner_parsed(), true); |
| 135 } |
| 136 } |
| 137 |
| 138 GURL::GURL(const char* canonical_spec, size_t canonical_spec_len, |
| 139 const url_parse::Parsed& parsed, bool is_valid) |
| 140 : spec_(canonical_spec, canonical_spec_len), |
| 141 is_valid_(is_valid), |
| 142 parsed_(parsed), |
| 143 inner_url_(NULL) { |
| 144 if (is_valid_ && SchemeIsFileSystem()) { |
| 145 inner_url_ = |
| 146 new GURL(spec_.data(), parsed_.Length(), *parsed_.inner_parsed(), true); |
| 147 } |
| 148 |
| 149 #ifndef NDEBUG |
| 150 // For testing purposes, check that the parsed canonical URL is identical to |
| 151 // what we would have produced. Skip checking for invalid URLs have no meaning |
| 152 // and we can't always canonicalize then reproducabely. |
| 153 if (is_valid_) { |
| 154 url_parse::Component scheme; |
| 155 if (!url_util::FindAndCompareScheme(canonical_spec, canonical_spec_len, |
| 156 "filesystem", &scheme) || |
| 157 scheme.begin == parsed.scheme.begin) { |
| 158 // We can't do this check on the inner_url of a filesystem URL, as |
| 159 // canonical_spec actually points to the start of the outer URL, so we'd |
| 160 // end up with infinite recursion in this constructor. |
| 161 GURL test_url(spec_); |
| 162 |
| 163 DCHECK(test_url.is_valid_ == is_valid_); |
| 164 DCHECK(test_url.spec_ == spec_); |
| 165 |
| 166 DCHECK(test_url.parsed_.scheme == parsed_.scheme); |
| 167 DCHECK(test_url.parsed_.username == parsed_.username); |
| 168 DCHECK(test_url.parsed_.password == parsed_.password); |
| 169 DCHECK(test_url.parsed_.host == parsed_.host); |
| 170 DCHECK(test_url.parsed_.port == parsed_.port); |
| 171 DCHECK(test_url.parsed_.path == parsed_.path); |
| 172 DCHECK(test_url.parsed_.query == parsed_.query); |
| 173 DCHECK(test_url.parsed_.ref == parsed_.ref); |
| 174 } |
| 175 } |
| 176 #endif |
| 177 } |
| 178 |
| 179 GURL::~GURL() { |
| 180 delete inner_url_; |
| 181 } |
| 182 |
| 183 GURL& GURL::operator=(const GURL& other) { |
| 184 spec_ = other.spec_; |
| 185 is_valid_ = other.is_valid_; |
| 186 parsed_ = other.parsed_; |
| 187 delete inner_url_; |
| 188 inner_url_ = NULL; |
| 189 if (other.inner_url_) |
| 190 inner_url_ = new GURL(*other.inner_url_); |
| 191 // Valid filesystem urls should always have an inner_url_. |
| 192 DCHECK(!is_valid_ || !SchemeIsFileSystem() || inner_url_); |
| 193 return *this; |
| 194 } |
| 195 |
| 196 const std::string& GURL::spec() const { |
| 197 if (is_valid_ || spec_.empty()) |
| 198 return spec_; |
| 199 |
| 200 DCHECK(false) << "Trying to get the spec of an invalid URL!"; |
| 201 return EmptyStringForGURL(); |
| 202 } |
| 203 |
| 204 GURL GURL::Resolve(const std::string& relative) const { |
| 205 return ResolveWithCharsetConverter(relative, NULL); |
| 206 } |
| 207 GURL GURL::Resolve(const string16& relative) const { |
| 208 return ResolveWithCharsetConverter(relative, NULL); |
| 209 } |
| 210 |
| 211 // Note: code duplicated below (it's inconvenient to use a template here). |
| 212 GURL GURL::ResolveWithCharsetConverter( |
| 213 const std::string& relative, |
| 214 url_canon::CharsetConverter* charset_converter) const { |
| 215 // Not allowed for invalid URLs. |
| 216 if (!is_valid_) |
| 217 return GURL(); |
| 218 |
| 219 GURL result; |
| 220 |
| 221 // Reserve enough room in the output for the input, plus some extra so that |
| 222 // we have room if we have to escape a few things without reallocating. |
| 223 result.spec_.reserve(spec_.size() + 32); |
| 224 url_canon::StdStringCanonOutput output(&result.spec_); |
| 225 |
| 226 if (!url_util::ResolveRelative( |
| 227 spec_.data(), static_cast<int>(spec_.length()), parsed_, |
| 228 relative.data(), static_cast<int>(relative.length()), |
| 229 charset_converter, &output, &result.parsed_)) { |
| 230 // Error resolving, return an empty URL. |
| 231 return GURL(); |
| 232 } |
| 233 |
| 234 output.Complete(); |
| 235 result.is_valid_ = true; |
| 236 if (result.SchemeIsFileSystem()) { |
| 237 result.inner_url_ = new GURL(result.spec_.data(), result.parsed_.Length(), |
| 238 *result.parsed_.inner_parsed(), true); |
| 239 } |
| 240 return result; |
| 241 } |
| 242 |
| 243 // Note: code duplicated above (it's inconvenient to use a template here). |
| 244 GURL GURL::ResolveWithCharsetConverter( |
| 245 const string16& relative, |
| 246 url_canon::CharsetConverter* charset_converter) const { |
| 247 // Not allowed for invalid URLs. |
| 248 if (!is_valid_) |
| 249 return GURL(); |
| 250 |
| 251 GURL result; |
| 252 |
| 253 // Reserve enough room in the output for the input, plus some extra so that |
| 254 // we have room if we have to escape a few things without reallocating. |
| 255 result.spec_.reserve(spec_.size() + 32); |
| 256 url_canon::StdStringCanonOutput output(&result.spec_); |
| 257 |
| 258 if (!url_util::ResolveRelative( |
| 259 spec_.data(), static_cast<int>(spec_.length()), parsed_, |
| 260 relative.data(), static_cast<int>(relative.length()), |
| 261 charset_converter, &output, &result.parsed_)) { |
| 262 // Error resolving, return an empty URL. |
| 263 return GURL(); |
| 264 } |
| 265 |
| 266 output.Complete(); |
| 267 result.is_valid_ = true; |
| 268 if (result.SchemeIsFileSystem()) { |
| 269 result.inner_url_ = new GURL(result.spec_.data(), result.parsed_.Length(), |
| 270 *result.parsed_.inner_parsed(), true); |
| 271 } |
| 272 return result; |
| 273 } |
| 274 |
| 275 // Note: code duplicated below (it's inconvenient to use a template here). |
| 276 GURL GURL::ReplaceComponents( |
| 277 const url_canon::Replacements<char>& replacements) const { |
| 278 GURL result; |
| 279 |
| 280 // Not allowed for invalid URLs. |
| 281 if (!is_valid_) |
| 282 return GURL(); |
| 283 |
| 284 // Reserve enough room in the output for the input, plus some extra so that |
| 285 // we have room if we have to escape a few things without reallocating. |
| 286 result.spec_.reserve(spec_.size() + 32); |
| 287 url_canon::StdStringCanonOutput output(&result.spec_); |
| 288 |
| 289 result.is_valid_ = url_util::ReplaceComponents( |
| 290 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements, |
| 291 NULL, &output, &result.parsed_); |
| 292 |
| 293 output.Complete(); |
| 294 if (result.is_valid_ && result.SchemeIsFileSystem()) { |
| 295 result.inner_url_ = new GURL(spec_.data(), result.parsed_.Length(), |
| 296 *result.parsed_.inner_parsed(), true); |
| 297 } |
| 298 return result; |
| 299 } |
| 300 |
| 301 // Note: code duplicated above (it's inconvenient to use a template here). |
| 302 GURL GURL::ReplaceComponents( |
| 303 const url_canon::Replacements<char16>& replacements) const { |
| 304 GURL result; |
| 305 |
| 306 // Not allowed for invalid URLs. |
| 307 if (!is_valid_) |
| 308 return GURL(); |
| 309 |
| 310 // Reserve enough room in the output for the input, plus some extra so that |
| 311 // we have room if we have to escape a few things without reallocating. |
| 312 result.spec_.reserve(spec_.size() + 32); |
| 313 url_canon::StdStringCanonOutput output(&result.spec_); |
| 314 |
| 315 result.is_valid_ = url_util::ReplaceComponents( |
| 316 spec_.data(), static_cast<int>(spec_.length()), parsed_, replacements, |
| 317 NULL, &output, &result.parsed_); |
| 318 |
| 319 output.Complete(); |
| 320 if (result.is_valid_ && result.SchemeIsFileSystem()) { |
| 321 result.inner_url_ = new GURL(spec_.data(), result.parsed_.Length(), |
| 322 *result.parsed_.inner_parsed(), true); |
| 323 } |
| 324 return result; |
| 325 } |
| 326 |
| 327 GURL GURL::GetOrigin() const { |
| 328 // This doesn't make sense for invalid or nonstandard URLs, so return |
| 329 // the empty URL |
| 330 if (!is_valid_ || !IsStandard()) |
| 331 return GURL(); |
| 332 |
| 333 if (SchemeIsFileSystem()) |
| 334 return inner_url_->GetOrigin(); |
| 335 |
| 336 url_canon::Replacements<char> replacements; |
| 337 replacements.ClearUsername(); |
| 338 replacements.ClearPassword(); |
| 339 replacements.ClearPath(); |
| 340 replacements.ClearQuery(); |
| 341 replacements.ClearRef(); |
| 342 |
| 343 return ReplaceComponents(replacements); |
| 344 } |
| 345 |
| 346 GURL GURL::GetWithEmptyPath() const { |
| 347 // This doesn't make sense for invalid or nonstandard URLs, so return |
| 348 // the empty URL. |
| 349 if (!is_valid_ || !IsStandard()) |
| 350 return GURL(); |
| 351 |
| 352 // We could optimize this since we know that the URL is canonical, and we are |
| 353 // appending a canonical path, so avoiding re-parsing. |
| 354 GURL other(*this); |
| 355 if (parsed_.path.len == 0) |
| 356 return other; |
| 357 |
| 358 // Clear everything after the path. |
| 359 other.parsed_.query.reset(); |
| 360 other.parsed_.ref.reset(); |
| 361 |
| 362 // Set the path, since the path is longer than one, we can just set the |
| 363 // first character and resize. |
| 364 other.spec_[other.parsed_.path.begin] = '/'; |
| 365 other.parsed_.path.len = 1; |
| 366 other.spec_.resize(other.parsed_.path.begin + 1); |
| 367 return other; |
| 368 } |
| 369 |
| 370 bool GURL::IsStandard() const { |
| 371 return url_util::IsStandard(spec_.data(), parsed_.scheme); |
| 372 } |
| 373 |
| 374 bool GURL::SchemeIs(const char* lower_ascii_scheme) const { |
| 375 if (parsed_.scheme.len <= 0) |
| 376 return lower_ascii_scheme == NULL; |
| 377 return url_util::LowerCaseEqualsASCII(spec_.data() + parsed_.scheme.begin, |
| 378 spec_.data() + parsed_.scheme.end(), |
| 379 lower_ascii_scheme); |
| 380 } |
| 381 |
| 382 int GURL::IntPort() const { |
| 383 if (parsed_.port.is_nonempty()) |
| 384 return url_parse::ParsePort(spec_.data(), parsed_.port); |
| 385 return url_parse::PORT_UNSPECIFIED; |
| 386 } |
| 387 |
| 388 int GURL::EffectiveIntPort() const { |
| 389 int int_port = IntPort(); |
| 390 if (int_port == url_parse::PORT_UNSPECIFIED && IsStandard()) |
| 391 return url_canon::DefaultPortForScheme(spec_.data() + parsed_.scheme.begin, |
| 392 parsed_.scheme.len); |
| 393 return int_port; |
| 394 } |
| 395 |
| 396 std::string GURL::ExtractFileName() const { |
| 397 url_parse::Component file_component; |
| 398 url_parse::ExtractFileName(spec_.data(), parsed_.path, &file_component); |
| 399 return ComponentString(file_component); |
| 400 } |
| 401 |
| 402 std::string GURL::PathForRequest() const { |
| 403 DCHECK(parsed_.path.len > 0) << "Canonical path for requests should be non-emp
ty"; |
| 404 if (parsed_.ref.len >= 0) { |
| 405 // Clip off the reference when it exists. The reference starts after the # |
| 406 // sign, so we have to subtract one to also remove it. |
| 407 return std::string(spec_, parsed_.path.begin, |
| 408 parsed_.ref.begin - parsed_.path.begin - 1); |
| 409 } |
| 410 // Compute the actual path length, rather than depending on the spec's |
| 411 // terminator. If we're an inner_url, our spec continues on into our outer |
| 412 // url's path/query/ref. |
| 413 int path_len = parsed_.path.len; |
| 414 if (parsed_.query.is_valid()) |
| 415 path_len = parsed_.query.end() - parsed_.path.begin; |
| 416 |
| 417 return std::string(spec_, parsed_.path.begin, path_len); |
| 418 } |
| 419 |
| 420 std::string GURL::HostNoBrackets() const { |
| 421 // If host looks like an IPv6 literal, strip the square brackets. |
| 422 url_parse::Component h(parsed_.host); |
| 423 if (h.len >= 2 && spec_[h.begin] == '[' && spec_[h.end() - 1] == ']') { |
| 424 h.begin++; |
| 425 h.len -= 2; |
| 426 } |
| 427 return ComponentString(h); |
| 428 } |
| 429 |
| 430 bool GURL::HostIsIPAddress() const { |
| 431 if (!is_valid_ || spec_.empty()) |
| 432 return false; |
| 433 |
| 434 url_canon::RawCanonOutputT<char, 128> ignored_output; |
| 435 url_canon::CanonHostInfo host_info; |
| 436 url_canon::CanonicalizeIPAddress(spec_.c_str(), parsed_.host, |
| 437 &ignored_output, &host_info); |
| 438 return host_info.IsIPAddress(); |
| 439 } |
| 440 |
| 441 #ifdef WIN32 |
| 442 |
| 443 const GURL& GURL::EmptyGURL() { |
| 444 // Avoid static object construction/destruction on startup/shutdown. |
| 445 if (!empty_gurl) { |
| 446 // Create the string. Be careful that we don't break in the case that this |
| 447 // is being called from multiple threads. |
| 448 GURL* new_empty_gurl = new GURL; |
| 449 if (InterlockedCompareExchangePointer( |
| 450 reinterpret_cast<PVOID*>(&empty_gurl), new_empty_gurl, NULL)) { |
| 451 // The old value was non-NULL, so no replacement was done. Another |
| 452 // thread did the initialization out from under us. |
| 453 delete new_empty_gurl; |
| 454 } |
| 455 } |
| 456 return *empty_gurl; |
| 457 } |
| 458 |
| 459 #else |
| 460 |
| 461 void EmptyGURLOnce(void) { |
| 462 empty_gurl = new GURL; |
| 463 } |
| 464 |
| 465 const GURL& GURL::EmptyGURL() { |
| 466 // Avoid static object construction/destruction on startup/shutdown. |
| 467 pthread_once(&empty_gurl_once, EmptyGURLOnce); |
| 468 return *empty_gurl; |
| 469 } |
| 470 |
| 471 #endif // WIN32 |
| 472 |
| 473 bool GURL::DomainIs(const char* lower_ascii_domain, |
| 474 int domain_len) const { |
| 475 // Return false if this URL is not valid or domain is empty. |
| 476 if (!is_valid_ || !domain_len) |
| 477 return false; |
| 478 |
| 479 // FileSystem URLs have empty parsed_.host, so check this first. |
| 480 if (SchemeIsFileSystem() && inner_url_) |
| 481 return inner_url_->DomainIs(lower_ascii_domain, domain_len); |
| 482 |
| 483 if (!parsed_.host.is_nonempty()) |
| 484 return false; |
| 485 |
| 486 // Check whether the host name is end with a dot. If yes, treat it |
| 487 // the same as no-dot unless the input comparison domain is end |
| 488 // with dot. |
| 489 const char* last_pos = spec_.data() + parsed_.host.end() - 1; |
| 490 int host_len = parsed_.host.len; |
| 491 if ('.' == *last_pos && '.' != lower_ascii_domain[domain_len - 1]) { |
| 492 last_pos--; |
| 493 host_len--; |
| 494 } |
| 495 |
| 496 // Return false if host's length is less than domain's length. |
| 497 if (host_len < domain_len) |
| 498 return false; |
| 499 |
| 500 // Compare this url whether belong specific domain. |
| 501 const char* start_pos = spec_.data() + parsed_.host.begin + |
| 502 host_len - domain_len; |
| 503 |
| 504 if (!url_util::LowerCaseEqualsASCII(start_pos, |
| 505 last_pos + 1, |
| 506 lower_ascii_domain, |
| 507 lower_ascii_domain + domain_len)) |
| 508 return false; |
| 509 |
| 510 // Check whether host has right domain start with dot, make sure we got |
| 511 // right domain range. For example www.google.com has domain |
| 512 // "google.com" but www.iamnotgoogle.com does not. |
| 513 if ('.' != lower_ascii_domain[0] && host_len > domain_len && |
| 514 '.' != *(start_pos - 1)) |
| 515 return false; |
| 516 |
| 517 return true; |
| 518 } |
| 519 |
| 520 void GURL::Swap(GURL* other) { |
| 521 spec_.swap(other->spec_); |
| 522 std::swap(is_valid_, other->is_valid_); |
| 523 std::swap(parsed_, other->parsed_); |
| 524 std::swap(inner_url_, other->inner_url_); |
| 525 } |
| 526 |
| 527 std::ostream& operator<<(std::ostream& out, const GURL& url) { |
| 528 return out << url.possibly_invalid_spec(); |
| 529 } |
OLD | NEW |