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

Side by Side Diff: net/base/net_util.cc

Issue 23619016: Switch the offset conversion routines from... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 3 months 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "net/base/net_util.h" 5 #include "net/base/net_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <map> 9 #include <map>
10 10
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 #include "third_party/icu/source/i18n/unicode/datefmt.h" 76 #include "third_party/icu/source/i18n/unicode/datefmt.h"
77 #include "third_party/icu/source/i18n/unicode/regex.h" 77 #include "third_party/icu/source/i18n/unicode/regex.h"
78 #include "third_party/icu/source/i18n/unicode/ulocdata.h" 78 #include "third_party/icu/source/i18n/unicode/ulocdata.h"
79 79
80 using base::Time; 80 using base::Time;
81 81
82 namespace net { 82 namespace net {
83 83
84 namespace { 84 namespace {
85 85
86 typedef std::vector<size_t> Offsets;
87
86 // what we prepend to get a file URL 88 // what we prepend to get a file URL
87 static const base::FilePath::CharType kFileURLPrefix[] = 89 static const base::FilePath::CharType kFileURLPrefix[] =
88 FILE_PATH_LITERAL("file:///"); 90 FILE_PATH_LITERAL("file:///");
89 91
90 // The general list of blocked ports. Will be blocked unless a specific 92 // The general list of blocked ports. Will be blocked unless a specific
91 // protocol overrides it. (Ex: ftp can use ports 20 and 21) 93 // protocol overrides it. (Ex: ftp can use ports 20 and 21)
92 static const int kRestrictedPorts[] = { 94 static const int kRestrictedPorts[] = {
93 1, // tcpmux 95 1, // tcpmux
94 7, // echo 96 7, // echo
95 9, // discard 97 9, // discard
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 out->resize(original_length); 440 out->resize(original_length);
439 } 441 }
440 442
441 // We get here with no IDN or on error, in which case we just append the 443 // We get here with no IDN or on error, in which case we just append the
442 // literal input. 444 // literal input.
443 out->append(comp, comp_len); 445 out->append(comp, comp_len);
444 return false; 446 return false;
445 } 447 }
446 448
447 // Clamps the offsets in |offsets_for_adjustment| to the length of |str|. 449 // Clamps the offsets in |offsets_for_adjustment| to the length of |str|.
448 void LimitOffsets(const base::string16& str, 450 void LimitOffsets(const base::string16& str, Offsets* offsets_for_adjustment) {
449 std::vector<size_t>* offsets_for_adjustment) {
450 if (offsets_for_adjustment) { 451 if (offsets_for_adjustment) {
451 std::for_each(offsets_for_adjustment->begin(), 452 std::for_each(offsets_for_adjustment->begin(),
452 offsets_for_adjustment->end(), 453 offsets_for_adjustment->end(),
453 base::LimitOffset<base::string16>(str.length())); 454 base::LimitOffset<base::string16>(str.length()));
454 } 455 }
455 } 456 }
456 457
457 // TODO(brettw) bug 734373: check the scripts for each host component and 458 // TODO(brettw) bug 734373: check the scripts for each host component and
458 // don't un-IDN-ize if there is more than one. Alternatively, only IDN for 459 // don't un-IDN-ize if there is more than one. Alternatively, only IDN for
459 // scripts that the user has installed. For now, just put the entire 460 // scripts that the user has installed. For now, just put the entire
460 // path through IDN. Maybe this feature can be implemented in ICU itself? 461 // path through IDN. Maybe this feature can be implemented in ICU itself?
461 // 462 //
462 // We may want to skip this step in the case of file URLs to allow unicode 463 // We may want to skip this step in the case of file URLs to allow unicode
463 // UNC hostnames regardless of encodings. 464 // UNC hostnames regardless of encodings.
464 base::string16 IDNToUnicodeWithOffsets( 465 base::string16 IDNToUnicodeWithOffsets(const std::string& host,
465 const std::string& host, 466 const std::string& languages,
466 const std::string& languages, 467 Offsets* offsets_for_adjustment) {
467 std::vector<size_t>* offsets_for_adjustment) {
468 // Convert the ASCII input to a base::string16 for ICU. 468 // Convert the ASCII input to a base::string16 for ICU.
469 base::string16 input16; 469 base::string16 input16;
470 input16.reserve(host.length()); 470 input16.reserve(host.length());
471 input16.insert(input16.end(), host.begin(), host.end()); 471 input16.insert(input16.end(), host.begin(), host.end());
472 472
473 // Do each component of the host separately, since we enforce script matching 473 // Do each component of the host separately, since we enforce script matching
474 // on a per-component basis. 474 // on a per-component basis.
475 base::string16 out16; 475 base::string16 out16;
476 { 476 {
477 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment); 477 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment);
(...skipping 23 matching lines...) Expand all
501 // Need to add the dot we just found (if we found one). 501 // Need to add the dot we just found (if we found one).
502 if (component_end < input16.length()) 502 if (component_end < input16.length())
503 out16.push_back('.'); 503 out16.push_back('.');
504 } 504 }
505 } 505 }
506 506
507 LimitOffsets(out16, offsets_for_adjustment); 507 LimitOffsets(out16, offsets_for_adjustment);
508 return out16; 508 return out16;
509 } 509 }
510 510
511 // Transforms |original_offsets| by subtracting |component_begin| from all
512 // offsets. Any offset which was not at least this large to begin with is set
513 // to std::string::npos.
514 std::vector<size_t> OffsetsIntoComponent(
msw 2013/09/06 00:31:08 Leave this helper intact, instead of writing two n
Peter Kasting 2013/09/06 19:04:36 Restored these two functions. I originally gutted
515 const std::vector<size_t>& original_offsets,
516 size_t component_begin) {
517 DCHECK_NE(std::string::npos, component_begin);
518 std::vector<size_t> offsets_into_component(original_offsets);
519 for (std::vector<size_t>::iterator i(offsets_into_component.begin());
520 i != offsets_into_component.end(); ++i) {
521 if (*i != std::string::npos)
522 *i = (*i < component_begin) ? std::string::npos : (*i - component_begin);
523 }
524 return offsets_into_component;
525 }
526
527 // Called after we transform a component and append it to an output string.
528 // Maps |transformed_offsets|, which represent offsets into the transformed
529 // component itself, into appropriate offsets for the output string, by adding
530 // |output_component_begin| to each. Determines which offsets need mapping by
531 // checking to see which of the |original_offsets| were within the designated
532 // original component, using its provided endpoints.
533 void AdjustForComponentTransform(
534 const std::vector<size_t>& original_offsets,
535 size_t original_component_begin,
536 size_t original_component_end,
537 const std::vector<size_t>& transformed_offsets,
538 size_t output_component_begin,
539 std::vector<size_t>* offsets_for_adjustment) {
540 if (!offsets_for_adjustment)
541 return;
542
543 DCHECK_NE(std::string::npos, original_component_begin);
544 DCHECK_NE(std::string::npos, original_component_end);
545 DCHECK_NE(base::string16::npos, output_component_begin);
546 size_t offsets_size = offsets_for_adjustment->size();
547 DCHECK_EQ(offsets_size, original_offsets.size());
548 DCHECK_EQ(offsets_size, transformed_offsets.size());
549 for (size_t i = 0; i < offsets_size; ++i) {
550 size_t original_offset = original_offsets[i];
551 if ((original_offset >= original_component_begin) &&
552 (original_offset < original_component_end)) {
553 size_t transformed_offset = transformed_offsets[i];
554 (*offsets_for_adjustment)[i] =
555 (transformed_offset == base::string16::npos) ?
556 base::string16::npos : (output_component_begin + transformed_offset);
557 }
558 }
559 }
560
561 // If |component| is valid, its begin is incremented by |delta|. 511 // If |component| is valid, its begin is incremented by |delta|.
562 void AdjustComponent(int delta, url_parse::Component* component) { 512 void AdjustComponent(int delta, url_parse::Component* component) {
563 if (!component->is_valid()) 513 if (!component->is_valid())
564 return; 514 return;
565 515
566 DCHECK(delta >= 0 || component->begin >= -delta); 516 DCHECK(delta >= 0 || component->begin >= -delta);
567 component->begin += delta; 517 component->begin += delta;
568 } 518 }
569 519
570 // Adjusts all the components of |parsed| by |delta|, except for the scheme. 520 // Adjusts all the components of |parsed| by |delta|, except for the scheme.
571 void AdjustComponents(int delta, url_parse::Parsed* parsed) { 521 void AdjustComponents(int delta, url_parse::Parsed* parsed) {
572 AdjustComponent(delta, &(parsed->username)); 522 AdjustComponent(delta, &(parsed->username));
573 AdjustComponent(delta, &(parsed->password)); 523 AdjustComponent(delta, &(parsed->password));
574 AdjustComponent(delta, &(parsed->host)); 524 AdjustComponent(delta, &(parsed->host));
575 AdjustComponent(delta, &(parsed->port)); 525 AdjustComponent(delta, &(parsed->port));
576 AdjustComponent(delta, &(parsed->path)); 526 AdjustComponent(delta, &(parsed->path));
577 AdjustComponent(delta, &(parsed->query)); 527 AdjustComponent(delta, &(parsed->query));
578 AdjustComponent(delta, &(parsed->ref)); 528 AdjustComponent(delta, &(parsed->ref));
579 } 529 }
580 530
581 // Helper for FormatUrlWithOffsets(). 531 // Helper for FormatUrlWithOffsets().
582 base::string16 FormatViewSourceUrl( 532 base::string16 FormatViewSourceUrl(const GURL& url,
583 const GURL& url, 533 const Offsets& original_offsets,
584 const std::vector<size_t>& original_offsets, 534 const std::string& languages,
585 const std::string& languages, 535 FormatUrlTypes format_types,
586 FormatUrlTypes format_types, 536 UnescapeRule::Type unescape_rules,
587 UnescapeRule::Type unescape_rules, 537 url_parse::Parsed* new_parsed,
588 url_parse::Parsed* new_parsed, 538 size_t* prefix_end,
589 size_t* prefix_end, 539 Offsets* offsets_for_adjustment) {
590 std::vector<size_t>* offsets_for_adjustment) {
591 DCHECK(new_parsed); 540 DCHECK(new_parsed);
592 const char kViewSource[] = "view-source:"; 541 const char kViewSource[] = "view-source:";
593 const size_t kViewSourceLength = arraysize(kViewSource) - 1; 542 const size_t kViewSourceLength = arraysize(kViewSource) - 1;
594 std::vector<size_t> offsets_into_url(
595 OffsetsIntoComponent(original_offsets, kViewSourceLength));
596 543
597 GURL real_url(url.possibly_invalid_spec().substr(kViewSourceLength)); 544 GURL underlying_url(url.possibly_invalid_spec().substr(kViewSourceLength));
545 Offsets offsets_into_underlying_url(original_offsets);
546 for (Offsets::iterator i(offsets_into_underlying_url.begin());
msw 2013/09/06 00:31:08 nit: use consistent iter/index looping if you're c
Peter Kasting 2013/09/06 19:04:36 This is no longer so noticeably inconsistent, sinc
547 i != offsets_into_underlying_url.end(); ++i) {
548 *i = ((*i == std::string::npos) || (*i < kViewSourceLength)) ?
msw 2013/09/06 00:31:08 Why should offsets into trimmed leading text go to
Peter Kasting 2013/09/06 19:04:36 It doesn't actually matter what we set them to, si
549 std::string::npos : (*i - kViewSourceLength);
550 }
551
598 base::string16 result(ASCIIToUTF16(kViewSource) + 552 base::string16 result(ASCIIToUTF16(kViewSource) +
599 FormatUrlWithOffsets(real_url, languages, format_types, unescape_rules, 553 FormatUrlWithOffsets(underlying_url, languages, format_types,
600 new_parsed, prefix_end, &offsets_into_url)); 554 unescape_rules, new_parsed, prefix_end,
555 &offsets_into_underlying_url));
601 556
602 // Adjust position values. 557 // Adjust position values.
603 if (new_parsed->scheme.is_nonempty()) { 558 if (new_parsed->scheme.is_nonempty()) {
604 // Assume "view-source:real-scheme" as a scheme. 559 // Assume "view-source:real-scheme" as a scheme.
605 new_parsed->scheme.len += kViewSourceLength; 560 new_parsed->scheme.len += kViewSourceLength;
606 } else { 561 } else {
607 new_parsed->scheme.begin = 0; 562 new_parsed->scheme.begin = 0;
608 new_parsed->scheme.len = kViewSourceLength - 1; 563 new_parsed->scheme.len = kViewSourceLength - 1;
609 } 564 }
610 AdjustComponents(kViewSourceLength, new_parsed); 565 AdjustComponents(kViewSourceLength, new_parsed);
611 if (prefix_end) 566 if (prefix_end)
612 *prefix_end += kViewSourceLength; 567 *prefix_end += kViewSourceLength;
613 AdjustForComponentTransform(original_offsets, kViewSourceLength, 568 for (size_t i = 0; i < original_offsets.size(); ++i) {
msw 2013/09/06 00:31:08 Why does this loop use |original_offsets| at all?
Peter Kasting 2013/09/06 19:04:36 This is now moot since this once again calls Adjus
614 url.possibly_invalid_spec().length(), offsets_into_url, kViewSourceLength, 569 size_t new_offset = offsets_into_underlying_url[i];
615 offsets_for_adjustment); 570 if (original_offsets[i] >= kViewSourceLength) {
571 (*offsets_for_adjustment)[i] = (new_offset == base::string16::npos) ?
msw 2013/09/06 00:31:08 offsets_for_adjustment may be NULL, check for that
Peter Kasting 2013/09/06 19:04:36 This is now moot since this once again calls Adjus
572 base::string16::npos : (new_offset + kViewSourceLength);
573 }
574 }
616 LimitOffsets(result, offsets_for_adjustment); 575 LimitOffsets(result, offsets_for_adjustment);
617 return result; 576 return result;
618 } 577 }
619 578
620 class AppendComponentTransform { 579 class AppendComponentTransform {
621 public: 580 public:
622 AppendComponentTransform() {} 581 AppendComponentTransform() {}
623 virtual ~AppendComponentTransform() {} 582 virtual ~AppendComponentTransform() {}
624 583
625 virtual base::string16 Execute( 584 virtual base::string16 Execute(const std::string& component_text,
626 const std::string& component_text, 585 Offsets* offsets_into_component) const = 0;
627 std::vector<size_t>* offsets_into_component) const = 0;
628 586
629 // NOTE: No DISALLOW_COPY_AND_ASSIGN here, since gcc < 4.3.0 requires an 587 // NOTE: No DISALLOW_COPY_AND_ASSIGN here, since gcc < 4.3.0 requires an
630 // accessible copy constructor in order to call AppendFormattedComponent() 588 // accessible copy constructor in order to call AppendFormattedComponent()
631 // with an inline temporary (see http://gcc.gnu.org/bugs/#cxx%5Frvalbind ). 589 // with an inline temporary (see http://gcc.gnu.org/bugs/#cxx%5Frvalbind ).
632 }; 590 };
633 591
634 class HostComponentTransform : public AppendComponentTransform { 592 class HostComponentTransform : public AppendComponentTransform {
635 public: 593 public:
636 explicit HostComponentTransform(const std::string& languages) 594 explicit HostComponentTransform(const std::string& languages)
637 : languages_(languages) { 595 : languages_(languages) {
638 } 596 }
639 597
640 private: 598 private:
641 virtual base::string16 Execute( 599 virtual base::string16 Execute(
642 const std::string& component_text, 600 const std::string& component_text,
643 std::vector<size_t>* offsets_into_component) const OVERRIDE { 601 Offsets* offsets_into_component) const OVERRIDE {
644 return IDNToUnicodeWithOffsets(component_text, languages_, 602 return IDNToUnicodeWithOffsets(component_text, languages_,
645 offsets_into_component); 603 offsets_into_component);
646 } 604 }
647 605
648 const std::string& languages_; 606 const std::string& languages_;
649 }; 607 };
650 608
651 class NonHostComponentTransform : public AppendComponentTransform { 609 class NonHostComponentTransform : public AppendComponentTransform {
652 public: 610 public:
653 explicit NonHostComponentTransform(UnescapeRule::Type unescape_rules) 611 explicit NonHostComponentTransform(UnescapeRule::Type unescape_rules)
654 : unescape_rules_(unescape_rules) { 612 : unescape_rules_(unescape_rules) {
655 } 613 }
656 614
657 private: 615 private:
658 virtual base::string16 Execute( 616 virtual base::string16 Execute(
659 const std::string& component_text, 617 const std::string& component_text,
660 std::vector<size_t>* offsets_into_component) const OVERRIDE { 618 Offsets* offsets_into_component) const OVERRIDE {
661 return (unescape_rules_ == UnescapeRule::NONE) ? 619 return (unescape_rules_ == UnescapeRule::NONE) ?
662 base::UTF8ToUTF16AndAdjustOffsets(component_text, 620 base::UTF8ToUTF16AndAdjustOffsets(component_text,
663 offsets_into_component) : 621 offsets_into_component) :
664 UnescapeAndDecodeUTF8URLComponentWithOffsets(component_text, 622 UnescapeAndDecodeUTF8URLComponentWithOffsets(component_text,
665 unescape_rules_, offsets_into_component); 623 unescape_rules_, offsets_into_component);
666 } 624 }
667 625
668 const UnescapeRule::Type unescape_rules_; 626 const UnescapeRule::Type unescape_rules_;
669 }; 627 };
670 628
629 // Transforms the portion of |spec| covered by |original_component| according to
630 // |transform|. Appends the result to |output|. If |output_component| is
631 // non-NULL, its start and length are set to the transformed component's new
632 // start and length. For each element in |original_offsets| which is at least
633 // as large as original_component.begin, the corresponding element of
634 // |offsets_for_adjustment| is transformed appropriately.
671 void AppendFormattedComponent(const std::string& spec, 635 void AppendFormattedComponent(const std::string& spec,
672 const url_parse::Component& original_component, 636 const url_parse::Component& original_component,
673 const std::vector<size_t>& original_offsets, 637 const Offsets& original_offsets,
674 const AppendComponentTransform& transform, 638 const AppendComponentTransform& transform,
675 base::string16* output, 639 base::string16* output,
676 url_parse::Component* output_component, 640 url_parse::Component* output_component,
677 std::vector<size_t>* offsets_for_adjustment) { 641 Offsets* offsets_for_adjustment) {
678 DCHECK(output); 642 DCHECK(output);
679 if (original_component.is_nonempty()) { 643 if (original_component.is_nonempty()) {
680 size_t original_component_begin = 644 size_t original_component_begin =
681 static_cast<size_t>(original_component.begin); 645 static_cast<size_t>(original_component.begin);
682 size_t output_component_begin = output->length(); 646 size_t output_component_begin = output->length();
683 if (output_component) 647 std::string component_str(spec, original_component_begin,
684 output_component->begin = static_cast<int>(output_component_begin); 648 static_cast<size_t>(original_component.len));
685 649
686 std::vector<size_t> offsets_into_component = 650 // Transform |original_offsets| into a vetor of offsets relative to
msw 2013/09/06 00:31:08 nit: 'vector'
Peter Kasting 2013/09/06 19:04:36 This is now moot.
687 OffsetsIntoComponent(original_offsets, original_component_begin); 651 // |component_str|.
688 output->append(transform.Execute(std::string(spec, original_component_begin, 652 Offsets offsets_into_component(original_offsets);
689 static_cast<size_t>(original_component.len)), &offsets_into_component)); 653 size_t original_component_end =
654 static_cast<size_t>(original_component.end());
655 for (Offsets::iterator i(offsets_into_component.begin());
656 i != offsets_into_component.end(); ++i) {
657 // If the offset originally pointed into this component, adjust down by
658 // |original_component_begin|. (Other offsets are ignored, since it
msw 2013/09/06 00:31:08 Is it even worthwhile to ignore other offsets? It
Peter Kasting 2013/09/06 19:04:36 You're right, we can unconditionally subtract sinc
659 // doesn't matter what the transform does with them, as we won't be paying
660 // attention to them below).
661 if ((*i >= original_component_begin) && (*i < original_component_end))
662 *i -= original_component_begin;
663 }
664
665 // Now format |component_str| and adjust the offsets accordingly.
666 output->append(transform.Execute(component_str, &offsets_into_component));
667
668 if (offsets_for_adjustment) {
669 // Transform back to absolute offsets by checking where each element of
670 // |original_offsets| pointed.
671 DCHECK_EQ(original_offsets.size(), offsets_for_adjustment->size());
672 for (size_t i = 0; i < original_offsets.size(); ++i) {
673 size_t original_offset = original_offsets[i];
674 if ((original_offset >= original_component_begin) &&
675 (original_offset < original_component_end)) {
676 // This offset originally pointed into the transformed component.
msw 2013/09/06 00:31:08 I find it odd that we are adjusting offsets within
Peter Kasting 2013/09/06 19:04:36 Yes, any time the path needs escaping or unescapin
677 // Adjust the transformed relative offset back up by
678 // |output_component_begin|.
679 (*offsets_for_adjustment)[i] =
680 (offsets_into_component[i] == base::string16::npos) ?
681 base::string16::npos :
682 (offsets_into_component[i] + output_component_begin);
683 } else if ((original_offset >= original_component_end) &&
684 (original_offset != std::string::npos)) {
msw 2013/09/06 00:31:08 nit: fix indent
Peter Kasting 2013/09/06 19:04:36 Done.
685 // This offset pointed after the transformed component. Adjust by the
686 // overall difference in the transformed strings to this point.
687 (*offsets_for_adjustment)[i] =
688 original_offset - original_component_end + output->length();
msw 2013/09/06 00:31:08 I suppose either is approach should yield the same
Peter Kasting 2013/09/06 19:04:36 They do yield the same result. Now that this code
689 }
690 }
691 }
690 692
691 if (output_component) { 693 if (output_component) {
694 output_component->begin = static_cast<int>(output_component_begin);
692 output_component->len = 695 output_component->len =
693 static_cast<int>(output->length() - output_component_begin); 696 static_cast<int>(output->length() - output_component_begin);
694 } 697 }
695 AdjustForComponentTransform(original_offsets, original_component_begin,
696 static_cast<size_t>(original_component.end()),
697 offsets_into_component, output_component_begin,
698 offsets_for_adjustment);
699 } else if (output_component) { 698 } else if (output_component) {
700 output_component->reset(); 699 output_component->reset();
701 } 700 }
702 } 701 }
703 702
704 void SanitizeGeneratedFileName(base::FilePath::StringType* filename, 703 void SanitizeGeneratedFileName(base::FilePath::StringType* filename,
705 bool replace_trailing) { 704 bool replace_trailing) {
706 const base::FilePath::CharType kReplace[] = FILE_PATH_LITERAL("-"); 705 const base::FilePath::CharType kReplace[] = FILE_PATH_LITERAL("-");
707 if (filename->empty()) 706 if (filename->empty())
708 return; 707 return;
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after
1630 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags, NULL); 1629 *password = UnescapeAndDecodeUTF8URLComponent(url.password(), flags, NULL);
1631 } 1630 }
1632 1631
1633 std::string GetHostOrSpecFromURL(const GURL& url) { 1632 std::string GetHostOrSpecFromURL(const GURL& url) {
1634 return url.has_host() ? TrimEndingDot(url.host()) : url.spec(); 1633 return url.has_host() ? TrimEndingDot(url.host()) : url.spec();
1635 } 1634 }
1636 1635
1637 void AppendFormattedHost(const GURL& url, 1636 void AppendFormattedHost(const GURL& url,
1638 const std::string& languages, 1637 const std::string& languages,
1639 base::string16* output) { 1638 base::string16* output) {
1640 std::vector<size_t> offsets; 1639 Offsets offsets;
1641 AppendFormattedComponent(url.possibly_invalid_spec(), 1640 AppendFormattedComponent(url.possibly_invalid_spec(),
1642 url.parsed_for_possibly_invalid_spec().host, offsets, 1641 url.parsed_for_possibly_invalid_spec().host, offsets,
1643 HostComponentTransform(languages), output, NULL, NULL); 1642 HostComponentTransform(languages), output, NULL, NULL);
1644 } 1643 }
1645 1644
1646 base::string16 FormatUrlWithOffsets( 1645 base::string16 FormatUrlWithOffsets(
1647 const GURL& url, 1646 const GURL& url,
1648 const std::string& languages, 1647 const std::string& languages,
1649 FormatUrlTypes format_types, 1648 FormatUrlTypes format_types,
1650 UnescapeRule::Type unescape_rules, 1649 UnescapeRule::Type unescape_rules,
1651 url_parse::Parsed* new_parsed, 1650 url_parse::Parsed* new_parsed,
1652 size_t* prefix_end, 1651 size_t* prefix_end,
1653 std::vector<size_t>* offsets_for_adjustment) { 1652 Offsets* offsets_for_adjustment) {
1654 url_parse::Parsed parsed_temp; 1653 url_parse::Parsed parsed_temp;
1655 if (!new_parsed) 1654 if (!new_parsed)
1656 new_parsed = &parsed_temp; 1655 new_parsed = &parsed_temp;
1657 else 1656 else
1658 *new_parsed = url_parse::Parsed(); 1657 *new_parsed = url_parse::Parsed();
1659 std::vector<size_t> original_offsets; 1658 Offsets original_offsets;
1660 if (offsets_for_adjustment) 1659 if (offsets_for_adjustment)
1661 original_offsets = *offsets_for_adjustment; 1660 original_offsets = *offsets_for_adjustment;
1662 1661
1663 // Special handling for view-source:. Don't use content::kViewSourceScheme 1662 // Special handling for view-source:. Don't use content::kViewSourceScheme
1664 // because this library shouldn't depend on chrome. 1663 // because this library shouldn't depend on chrome.
1665 const char* const kViewSource = "view-source"; 1664 const char* const kViewSource = "view-source";
1666 // Reject "view-source:view-source:..." to avoid deep recursion. 1665 // Reject "view-source:view-source:..." to avoid deep recursion.
1667 const char* const kViewSourceTwice = "view-source:view-source:"; 1666 const char* const kViewSourceTwice = "view-source:view-source:";
1668 if (url.SchemeIs(kViewSource) && 1667 if (url.SchemeIs(kViewSource) &&
1669 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) { 1668 !StartsWithASCII(url.possibly_invalid_spec(), kViewSourceTwice, false)) {
1670 return FormatViewSourceUrl(url, original_offsets, languages, format_types, 1669 return FormatViewSourceUrl(url, original_offsets, languages, format_types,
1671 unescape_rules, new_parsed, prefix_end, offsets_for_adjustment); 1670 unescape_rules, new_parsed, prefix_end,
1671 offsets_for_adjustment);
1672 } 1672 }
1673 1673
1674 // We handle both valid and invalid URLs (this will give us the spec 1674 // We handle both valid and invalid URLs (this will give us the spec
1675 // regardless of validity). 1675 // regardless of validity).
1676 const std::string& spec = url.possibly_invalid_spec(); 1676 const std::string& spec = url.possibly_invalid_spec();
1677 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec(); 1677 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
1678 1678
1679 // Scheme & separators. These are ASCII. 1679 // Scheme & separators. These are ASCII.
1680 base::string16 url_string; 1680 base::string16 url_string;
1681 url_string.insert(url_string.end(), spec.begin(), 1681 url_string.insert(url_string.end(), spec.begin(),
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1719 // username/password. 1719 // username/password.
1720 offset_adjuster.Add(base::OffsetAdjuster::Adjustment( 1720 offset_adjuster.Add(base::OffsetAdjuster::Adjustment(
1721 static_cast<size_t>(nonempty_component->begin), 1721 static_cast<size_t>(nonempty_component->begin),
1722 static_cast<size_t>(nonempty_component->len + 1), 0)); 1722 static_cast<size_t>(nonempty_component->len + 1), 0));
1723 } 1723 }
1724 } 1724 }
1725 } else { 1725 } else {
1726 AppendFormattedComponent(spec, parsed.username, original_offsets, 1726 AppendFormattedComponent(spec, parsed.username, original_offsets,
1727 NonHostComponentTransform(unescape_rules), &url_string, 1727 NonHostComponentTransform(unescape_rules), &url_string,
1728 &new_parsed->username, offsets_for_adjustment); 1728 &new_parsed->username, offsets_for_adjustment);
1729 if (parsed.password.is_valid()) { 1729 if (parsed.password.is_valid())
1730 size_t colon = parsed.username.end();
msw 2013/09/06 00:31:08 What was this code doing and why is it no longer n
Peter Kasting 2013/09/06 19:04:36 This is somewhat subtle. Before this change, Appe
1731 DCHECK_EQ(static_cast<size_t>(parsed.password.begin - 1), colon);
1732 std::vector<size_t>::const_iterator colon_iter =
1733 std::find(original_offsets.begin(), original_offsets.end(), colon);
1734 if (colon_iter != original_offsets.end()) {
1735 (*offsets_for_adjustment)[colon_iter - original_offsets.begin()] =
1736 url_string.length();
1737 }
1738 url_string.push_back(':'); 1730 url_string.push_back(':');
1739 }
1740 AppendFormattedComponent(spec, parsed.password, original_offsets, 1731 AppendFormattedComponent(spec, parsed.password, original_offsets,
1741 NonHostComponentTransform(unescape_rules), &url_string, 1732 NonHostComponentTransform(unescape_rules), &url_string,
1742 &new_parsed->password, offsets_for_adjustment); 1733 &new_parsed->password, offsets_for_adjustment);
1743 if (parsed.username.is_valid() || parsed.password.is_valid()) { 1734 if (parsed.username.is_valid() || parsed.password.is_valid())
1744 size_t at_sign = (parsed.password.is_valid() ?
msw 2013/09/06 00:31:08 Ditto: What was this code doing and why is it no l
1745 parsed.password : parsed.username).end();
1746 DCHECK_EQ(static_cast<size_t>(parsed.host.begin - 1), at_sign);
1747 std::vector<size_t>::const_iterator at_sign_iter =
1748 std::find(original_offsets.begin(), original_offsets.end(), at_sign);
1749 if (at_sign_iter != original_offsets.end()) {
1750 (*offsets_for_adjustment)[at_sign_iter - original_offsets.begin()] =
1751 url_string.length();
1752 }
1753 url_string.push_back('@'); 1735 url_string.push_back('@');
1754 }
1755 } 1736 }
1756 if (prefix_end) 1737 if (prefix_end)
1757 *prefix_end = static_cast<size_t>(url_string.length()); 1738 *prefix_end = static_cast<size_t>(url_string.length());
1758 1739
1759 // Host. 1740 // Host.
1760 AppendFormattedComponent(spec, parsed.host, original_offsets, 1741 AppendFormattedComponent(spec, parsed.host, original_offsets,
1761 HostComponentTransform(languages), &url_string, &new_parsed->host, 1742 HostComponentTransform(languages), &url_string, &new_parsed->host,
1762 offsets_for_adjustment); 1743 offsets_for_adjustment);
1763 1744
1764 // Port. 1745 // Port.
1765 if (parsed.port.is_nonempty()) { 1746 if (parsed.port.is_nonempty()) {
1766 url_string.push_back(':'); 1747 url_string.push_back(':');
1767 new_parsed->port.begin = url_string.length(); 1748 new_parsed->port.begin = url_string.length();
1768 url_string.insert(url_string.end(), 1749 url_string.insert(url_string.end(),
1769 spec.begin() + parsed.port.begin, 1750 spec.begin() + parsed.port.begin,
1770 spec.begin() + parsed.port.end()); 1751 spec.begin() + parsed.port.end());
1771 new_parsed->port.len = url_string.length() - new_parsed->port.begin; 1752 new_parsed->port.len = url_string.length() - new_parsed->port.begin;
1772 } else { 1753 } else {
1773 new_parsed->port.reset(); 1754 new_parsed->port.reset();
1774 } 1755 }
1775 1756
1776 // Path & query. Both get the same general unescape & convert treatment. 1757 // Path & query. Both get the same general unescape & convert treatment.
1777 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) || 1758 if (!(format_types & kFormatUrlOmitTrailingSlashOnBareHostname) ||
1778 !CanStripTrailingSlash(url)) { 1759 !CanStripTrailingSlash(url)) {
1779 AppendFormattedComponent(spec, parsed.path, original_offsets, 1760 AppendFormattedComponent(spec, parsed.path, original_offsets,
1780 NonHostComponentTransform(unescape_rules), &url_string, 1761 NonHostComponentTransform(unescape_rules), &url_string,
1781 &new_parsed->path, offsets_for_adjustment); 1762 &new_parsed->path, offsets_for_adjustment);
1763 } else {
1764 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment);
msw 2013/09/06 00:31:08 What is this doing? Adjusting offsets past a remov
Peter Kasting 2013/09/06 19:04:36 Yes. This only really matters for when we have an
1765 offset_adjuster.Add(base::OffsetAdjuster::Adjustment(
1766 url_string.length(), parsed.path.len, 0));
1782 } 1767 }
1783 if (parsed.query.is_valid()) 1768 if (parsed.query.is_valid())
1784 url_string.push_back('?'); 1769 url_string.push_back('?');
1785 AppendFormattedComponent(spec, parsed.query, original_offsets, 1770 AppendFormattedComponent(spec, parsed.query, original_offsets,
1786 NonHostComponentTransform(unescape_rules), &url_string, 1771 NonHostComponentTransform(unescape_rules), &url_string,
1787 &new_parsed->query, offsets_for_adjustment); 1772 &new_parsed->query, offsets_for_adjustment);
1788 1773
1789 // Ref. This is valid, unescaped UTF-8, so we can just convert. 1774 // Ref. This is valid, unescaped UTF-8, so we can just convert.
1790 if (parsed.ref.is_valid()) { 1775 if (parsed.ref.is_valid())
1791 url_string.push_back('#'); 1776 url_string.push_back('#');
1792 size_t original_ref_begin = static_cast<size_t>(parsed.ref.begin); 1777 AppendFormattedComponent(spec, parsed.ref, original_offsets,
msw 2013/09/06 00:31:08 Nice!
Peter Kasting 2013/09/06 19:04:36 Yeah... old code made me facepalm.
1793 size_t output_ref_begin = url_string.length(); 1778 NonHostComponentTransform(UnescapeRule::NONE), &url_string,
1794 new_parsed->ref.begin = static_cast<int>(output_ref_begin); 1779 &new_parsed->ref, offsets_for_adjustment);
1795
1796 std::vector<size_t> offsets_into_ref(
1797 OffsetsIntoComponent(original_offsets, original_ref_begin));
1798 if (parsed.ref.len > 0) {
1799 url_string.append(base::UTF8ToUTF16AndAdjustOffsets(
1800 spec.substr(original_ref_begin, static_cast<size_t>(parsed.ref.len)),
1801 &offsets_into_ref));
1802 }
1803
1804 new_parsed->ref.len =
1805 static_cast<int>(url_string.length() - new_parsed->ref.begin);
1806 AdjustForComponentTransform(original_offsets, original_ref_begin,
1807 static_cast<size_t>(parsed.ref.end()), offsets_into_ref,
1808 output_ref_begin, offsets_for_adjustment);
1809 }
1810 1780
1811 // If we need to strip out http do it after the fact. This way we don't need 1781 // If we need to strip out http do it after the fact. This way we don't need
1812 // to worry about how offset_for_adjustment is interpreted. 1782 // to worry about how offset_for_adjustment is interpreted.
1813 if (omit_http && StartsWith(url_string, ASCIIToUTF16(kHTTP), true)) { 1783 if (omit_http && StartsWith(url_string, ASCIIToUTF16(kHTTP), true)) {
1814 const size_t kHTTPSize = arraysize(kHTTP) - 1; 1784 const size_t kHTTPSize = arraysize(kHTTP) - 1;
1815 url_string = url_string.substr(kHTTPSize); 1785 url_string = url_string.substr(kHTTPSize);
1816 if (offsets_for_adjustment && !offsets_for_adjustment->empty()) { 1786 if (offsets_for_adjustment && !offsets_for_adjustment->empty()) {
1817 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment); 1787 base::OffsetAdjuster offset_adjuster(offsets_for_adjustment);
1818 offset_adjuster.Add(base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0)); 1788 offset_adjuster.Add(base::OffsetAdjuster::Adjustment(0, kHTTPSize, 0));
1819 } 1789 }
(...skipping 11 matching lines...) Expand all
1831 return url_string; 1801 return url_string;
1832 } 1802 }
1833 1803
1834 base::string16 FormatUrl(const GURL& url, 1804 base::string16 FormatUrl(const GURL& url,
1835 const std::string& languages, 1805 const std::string& languages,
1836 FormatUrlTypes format_types, 1806 FormatUrlTypes format_types,
1837 UnescapeRule::Type unescape_rules, 1807 UnescapeRule::Type unescape_rules,
1838 url_parse::Parsed* new_parsed, 1808 url_parse::Parsed* new_parsed,
1839 size_t* prefix_end, 1809 size_t* prefix_end,
1840 size_t* offset_for_adjustment) { 1810 size_t* offset_for_adjustment) {
1841 std::vector<size_t> offsets; 1811 Offsets offsets;
1842 if (offset_for_adjustment) 1812 if (offset_for_adjustment)
1843 offsets.push_back(*offset_for_adjustment); 1813 offsets.push_back(*offset_for_adjustment);
1844 base::string16 result = FormatUrlWithOffsets(url, languages, format_types, 1814 base::string16 result = FormatUrlWithOffsets(url, languages, format_types,
1845 unescape_rules, new_parsed, prefix_end, &offsets); 1815 unescape_rules, new_parsed, prefix_end, &offsets);
1846 if (offset_for_adjustment) 1816 if (offset_for_adjustment)
1847 *offset_for_adjustment = offsets[0]; 1817 *offset_for_adjustment = offsets[0];
1848 return result; 1818 return result;
1849 } 1819 }
1850 1820
1851 bool CanStripTrailingSlash(const GURL& url) { 1821 bool CanStripTrailingSlash(const GURL& url) {
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
2161 2131
2162 NetworkInterface::NetworkInterface(const std::string& name, 2132 NetworkInterface::NetworkInterface(const std::string& name,
2163 const IPAddressNumber& address) 2133 const IPAddressNumber& address)
2164 : name(name), address(address) { 2134 : name(name), address(address) {
2165 } 2135 }
2166 2136
2167 NetworkInterface::~NetworkInterface() { 2137 NetworkInterface::~NetworkInterface() {
2168 } 2138 }
2169 2139
2170 } // namespace net 2140 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698