OLD | NEW |
(Empty) | |
| 1 // Copyright 2012, 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 "filesystem:file:" URLs. |
| 31 |
| 32 #include "googleurl/src/url_canon.h" |
| 33 #include "googleurl/src/url_canon_internal.h" |
| 34 #include "googleurl/src/url_file.h" |
| 35 #include "googleurl/src/url_parse_internal.h" |
| 36 #include "googleurl/src/url_util.h" |
| 37 #include "googleurl/src/url_util_internal.h" |
| 38 |
| 39 namespace url_canon { |
| 40 |
| 41 namespace { |
| 42 |
| 43 // We use the URLComponentSource for the outer URL, as it can have replacements, |
| 44 // whereas the inner_url can't, so it uses spec. |
| 45 template<typename CHAR, typename UCHAR> |
| 46 bool DoCanonicalizeFileSystemURL(const CHAR* spec, |
| 47 const URLComponentSource<CHAR>& source, |
| 48 const url_parse::Parsed& parsed, |
| 49 CharsetConverter* charset_converter, |
| 50 CanonOutput* output, |
| 51 url_parse::Parsed* new_parsed) { |
| 52 // filesystem only uses {scheme, path, query, ref} -- clear the rest. |
| 53 new_parsed->username = url_parse::Component(); |
| 54 new_parsed->password = url_parse::Component(); |
| 55 new_parsed->host = url_parse::Component(); |
| 56 new_parsed->port = url_parse::Component(); |
| 57 |
| 58 const url_parse::Parsed* inner_parsed = parsed.inner_parsed(); |
| 59 url_parse::Parsed new_inner_parsed; |
| 60 |
| 61 // Scheme (known, so we don't bother running it through the more |
| 62 // complicated scheme canonicalizer). |
| 63 new_parsed->scheme.begin = output->length(); |
| 64 output->Append("filesystem:", 11); |
| 65 new_parsed->scheme.len = 10; |
| 66 |
| 67 if (!parsed.inner_parsed() || !parsed.inner_parsed()->scheme.is_valid()) |
| 68 return false; |
| 69 |
| 70 bool success = true; |
| 71 if (url_util::CompareSchemeComponent(spec, inner_parsed->scheme, |
| 72 url_util::kFileScheme)) { |
| 73 new_inner_parsed.scheme.begin = output->length(); |
| 74 output->Append("file://", 7); |
| 75 new_inner_parsed.scheme.len = 4; |
| 76 success &= CanonicalizePath(spec, inner_parsed->path, output, |
| 77 &new_inner_parsed.path); |
| 78 } else if (url_util::IsStandard(spec, inner_parsed->scheme)) { |
| 79 success = |
| 80 url_canon::CanonicalizeStandardURL(spec, |
| 81 parsed.inner_parsed()->Length(), |
| 82 *parsed.inner_parsed(), |
| 83 charset_converter, output, |
| 84 &new_inner_parsed); |
| 85 } else { |
| 86 // TODO(ericu): The URL is wrong, but should we try to output more of what |
| 87 // we were given? Echoing back filesystem:mailto etc. doesn't seem all that |
| 88 // useful. |
| 89 return false; |
| 90 } |
| 91 // The filesystem type must be more than just a leading slash for validity. |
| 92 success &= parsed.inner_parsed()->path.len > 1; |
| 93 |
| 94 success &= CanonicalizePath(source.path, parsed.path, output, |
| 95 &new_parsed->path); |
| 96 |
| 97 // Ignore failures for query/ref since the URL can probably still be loaded. |
| 98 CanonicalizeQuery(source.query, parsed.query, charset_converter, |
| 99 output, &new_parsed->query); |
| 100 CanonicalizeRef(source.ref, parsed.ref, output, &new_parsed->ref); |
| 101 if (success) |
| 102 new_parsed->set_inner_parsed(new_inner_parsed); |
| 103 |
| 104 return success; |
| 105 } |
| 106 |
| 107 } // namespace |
| 108 |
| 109 bool CanonicalizeFileSystemURL(const char* spec, |
| 110 int spec_len, |
| 111 const url_parse::Parsed& parsed, |
| 112 CharsetConverter* charset_converter, |
| 113 CanonOutput* output, |
| 114 url_parse::Parsed* new_parsed) { |
| 115 return DoCanonicalizeFileSystemURL<char, unsigned char>( |
| 116 spec, URLComponentSource<char>(spec), parsed, charset_converter, output, |
| 117 new_parsed); |
| 118 } |
| 119 |
| 120 bool CanonicalizeFileSystemURL(const char16* spec, |
| 121 int spec_len, |
| 122 const url_parse::Parsed& parsed, |
| 123 CharsetConverter* charset_converter, |
| 124 CanonOutput* output, |
| 125 url_parse::Parsed* new_parsed) { |
| 126 return DoCanonicalizeFileSystemURL<char16, char16>( |
| 127 spec, URLComponentSource<char16>(spec), parsed, charset_converter, output, |
| 128 new_parsed); |
| 129 } |
| 130 |
| 131 bool ReplaceFileSystemURL(const char* base, |
| 132 const url_parse::Parsed& base_parsed, |
| 133 const Replacements<char>& replacements, |
| 134 CharsetConverter* charset_converter, |
| 135 CanonOutput* output, |
| 136 url_parse::Parsed* new_parsed) { |
| 137 URLComponentSource<char> source(base); |
| 138 url_parse::Parsed parsed(base_parsed); |
| 139 SetupOverrideComponents(base, replacements, &source, &parsed); |
| 140 return DoCanonicalizeFileSystemURL<char, unsigned char>( |
| 141 base, source, parsed, charset_converter, output, new_parsed); |
| 142 } |
| 143 |
| 144 bool ReplaceFileSystemURL(const char* base, |
| 145 const url_parse::Parsed& base_parsed, |
| 146 const Replacements<char16>& replacements, |
| 147 CharsetConverter* charset_converter, |
| 148 CanonOutput* output, |
| 149 url_parse::Parsed* new_parsed) { |
| 150 RawCanonOutput<1024> utf8; |
| 151 URLComponentSource<char> source(base); |
| 152 url_parse::Parsed parsed(base_parsed); |
| 153 SetupUTF16OverrideComponents(base, replacements, &utf8, &source, &parsed); |
| 154 return DoCanonicalizeFileSystemURL<char, unsigned char>( |
| 155 base, source, parsed, charset_converter, output, new_parsed); |
| 156 } |
| 157 |
| 158 } // namespace url_canon |
OLD | NEW |