Home | History | Annotate | Download | only in src
      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 // Functions for canonicalizing "path" URLs. Not to be confused with the path
     31 // of a URL, these are URLs that have no authority section, only a path. For
     32 // example, "javascript:" and "data:".
     33 
     34 #include "googleurl/src/url_canon.h"
     35 #include "googleurl/src/url_canon_internal.h"
     36 
     37 namespace url_canon {
     38 
     39 namespace {
     40 
     41 template<typename CHAR, typename UCHAR>
     42 bool DoCanonicalizePathURL(const URLComponentSource<CHAR>& source,
     43                            const url_parse::Parsed& parsed,
     44                            CanonOutput* output,
     45                            url_parse::Parsed* new_parsed) {
     46   // Scheme: this will append the colon.
     47   bool success = CanonicalizeScheme(source.scheme, parsed.scheme,
     48                                     output, &new_parsed->scheme);
     49 
     50   // We assume there's no authority for path URLs. Note that hosts should never
     51   // have -1 length.
     52   new_parsed->username.reset();
     53   new_parsed->password.reset();
     54   new_parsed->host.reset();
     55   new_parsed->port.reset();
     56 
     57   if (parsed.path.is_valid()) {
     58     // Copy the path using path URL's more lax escaping rules (think for
     59     // javascript:). We convert to UTF-8 and escape non-ASCII, but leave all
     60     // ASCII characters alone. This helps readability of JavaStript.
     61     new_parsed->path.begin = output->length();
     62     int end = parsed.path.end();
     63     for (int i = parsed.path.begin; i < end; i++) {
     64       UCHAR uch = static_cast<UCHAR>(source.path[i]);
     65       if (uch < 0x20 || uch >= 0x80)
     66         success &= AppendUTF8EscapedChar(source.path, &i, end, output);
     67       else
     68         output->push_back(static_cast<char>(uch));
     69     }
     70     new_parsed->path.len = output->length() - new_parsed->path.begin;
     71   } else {
     72     // Empty path.
     73     new_parsed->path.reset();
     74   }
     75 
     76   // Assume there's no query or ref.
     77   new_parsed->query.reset();
     78   new_parsed->ref.reset();
     79 
     80   return success;
     81 }
     82 
     83 }  // namespace
     84 
     85 bool CanonicalizePathURL(const char* spec,
     86                          int spec_len,
     87                          const url_parse::Parsed& parsed,
     88                          CanonOutput* output,
     89                          url_parse::Parsed* new_parsed) {
     90   return DoCanonicalizePathURL<char, unsigned char>(
     91       URLComponentSource<char>(spec), parsed, output, new_parsed);
     92 }
     93 
     94 bool CanonicalizePathURL(const char16* spec,
     95                          int spec_len,
     96                          const url_parse::Parsed& parsed,
     97                          CanonOutput* output,
     98                          url_parse::Parsed* new_parsed) {
     99   return DoCanonicalizePathURL<char16, char16>(
    100       URLComponentSource<char16>(spec), parsed, output, new_parsed);
    101 }
    102 
    103 bool ReplacePathURL(const char* base,
    104                     const url_parse::Parsed& base_parsed,
    105                     const Replacements<char>& replacements,
    106                     CanonOutput* output,
    107                     url_parse::Parsed* new_parsed) {
    108   URLComponentSource<char> source(base);
    109   url_parse::Parsed parsed(base_parsed);
    110   SetupOverrideComponents(base, replacements, &source, &parsed);
    111   return DoCanonicalizePathURL<char, unsigned char>(
    112       source, parsed, output, new_parsed);
    113 }
    114 
    115 bool ReplacePathURL(const char* base,
    116                     const url_parse::Parsed& base_parsed,
    117                     const Replacements<char16>& replacements,
    118                     CanonOutput* output,
    119                     url_parse::Parsed* new_parsed) {
    120   RawCanonOutput<1024> utf8;
    121   URLComponentSource<char> source(base);
    122   url_parse::Parsed parsed(base_parsed);
    123   SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed);
    124   return DoCanonicalizePathURL<char, unsigned char>(
    125       source, parsed, output, new_parsed);
    126 }
    127 
    128 }  // namespace url_canon
    129