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

Side by Side Diff: url/url_canon_relative.cc

Issue 13821004: Move googleurl into the Chrome repo. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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
« no previous file with comments | « url/url_canon_query.cc ('k') | url/url_canon_stdstring.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 // Canonicalizer functions for working with and resolving relative URLs.
31
32 #include "base/logging.h"
33 #include "googleurl/src/url_canon.h"
34 #include "googleurl/src/url_canon_internal.h"
35 #include "googleurl/src/url_file.h"
36 #include "googleurl/src/url_parse_internal.h"
37 #include "googleurl/src/url_util_internal.h"
38
39 namespace url_canon {
40
41 namespace {
42
43 // Firefox does a case-sensitive compare (which is probably wrong--Mozilla bug
44 // 379034), whereas IE is case-insensetive.
45 //
46 // We choose to be more permissive like IE. We don't need to worry about
47 // unescaping or anything here: neither IE or Firefox allow this. We also
48 // don't have to worry about invalid scheme characters since we are comparing
49 // against the canonical scheme of the base.
50 //
51 // The base URL should always be canonical, therefore is ASCII.
52 template<typename CHAR>
53 bool AreSchemesEqual(const char* base,
54 const url_parse::Component& base_scheme,
55 const CHAR* cmp,
56 const url_parse::Component& cmp_scheme) {
57 if (base_scheme.len != cmp_scheme.len)
58 return false;
59 for (int i = 0; i < base_scheme.len; i++) {
60 // We assume the base is already canonical, so we don't have to
61 // canonicalize it.
62 if (CanonicalSchemeChar(cmp[cmp_scheme.begin + i]) !=
63 base[base_scheme.begin + i])
64 return false;
65 }
66 return true;
67 }
68
69 #ifdef WIN32
70
71 // Here, we also allow Windows paths to be represented as "/C:/" so we can be
72 // consistent about URL paths beginning with slashes. This function is like
73 // DoesBeginWindowsDrivePath except that it also requires a slash at the
74 // beginning.
75 template<typename CHAR>
76 bool DoesBeginSlashWindowsDriveSpec(const CHAR* spec, int start_offset,
77 int spec_len) {
78 if (start_offset >= spec_len)
79 return false;
80 return url_parse::IsURLSlash(spec[start_offset]) &&
81 url_parse::DoesBeginWindowsDriveSpec(spec, start_offset + 1, spec_len);
82 }
83
84 #endif // WIN32
85
86 // See IsRelativeURL in the header file for usage.
87 template<typename CHAR>
88 bool DoIsRelativeURL(const char* base,
89 const url_parse::Parsed& base_parsed,
90 const CHAR* url,
91 int url_len,
92 bool is_base_hierarchical,
93 bool* is_relative,
94 url_parse::Component* relative_component) {
95 *is_relative = false; // So we can default later to not relative.
96
97 // Trim whitespace and construct a new range for the substring.
98 int begin = 0;
99 url_parse::TrimURL(url, &begin, &url_len);
100 if (begin >= url_len) {
101 // Empty URLs are relative, but do nothing.
102 *relative_component = url_parse::Component(begin, 0);
103 *is_relative = true;
104 return true;
105 }
106
107 #ifdef WIN32
108 // We special case paths like "C:\foo" so they can link directly to the
109 // file on Windows (IE compatability). The security domain stuff should
110 // prevent a link like this from actually being followed if its on a
111 // web page.
112 //
113 // We treat "C:/foo" as an absolute URL. We can go ahead and treat "/c:/"
114 // as relative, as this will just replace the path when the base scheme
115 // is a file and the answer will still be correct.
116 //
117 // We require strict backslashes when detecting UNC since two forward
118 // shashes should be treated a a relative URL with a hostname.
119 if (url_parse::DoesBeginWindowsDriveSpec(url, begin, url_len) ||
120 url_parse::DoesBeginUNCPath(url, begin, url_len, true))
121 return true;
122 #endif // WIN32
123
124 // See if we've got a scheme, if not, we know this is a relative URL.
125 // BUT: Just because we have a scheme, doesn't make it absolute.
126 // "http:foo.html" is a relative URL with path "foo.html". If the scheme is
127 // empty, we treat it as relative (":foo") like IE does.
128 url_parse::Component scheme;
129 if (!url_parse::ExtractScheme(url, url_len, &scheme) || scheme.len == 0) {
130 // Don't allow relative URLs if the base scheme doesn't support it.
131 if (!is_base_hierarchical)
132 return false;
133
134 *relative_component = url_parse::MakeRange(begin, url_len);
135 *is_relative = true;
136 return true;
137 }
138
139 // If the scheme isn't valid, then it's relative.
140 int scheme_end = scheme.end();
141 for (int i = scheme.begin; i < scheme_end; i++) {
142 if (!CanonicalSchemeChar(url[i])) {
143 *relative_component = url_parse::MakeRange(begin, url_len);
144 *is_relative = true;
145 return true;
146 }
147 }
148
149 // If the scheme is not the same, then we can't count it as relative.
150 if (!AreSchemesEqual(base, base_parsed.scheme, url, scheme))
151 return true;
152
153 // When the scheme that they both share is not hierarchical, treat the
154 // incoming scheme as absolute (this way with the base of "data:foo",
155 // "data:bar" will be reported as absolute.
156 if (!is_base_hierarchical)
157 return true;
158
159 int colon_offset = scheme.end();
160
161 // If it's a filesystem URL, the only valid way to make it relative is not to
162 // supply a scheme. There's no equivalent to e.g. http:index.html.
163 if (url_util::CompareSchemeComponent(url, scheme, "filesystem"))
164 return true;
165
166 // ExtractScheme guarantees that the colon immediately follows what it
167 // considers to be the scheme. CountConsecutiveSlashes will handle the
168 // case where the begin offset is the end of the input.
169 int num_slashes = url_parse::CountConsecutiveSlashes(url, colon_offset + 1,
170 url_len);
171
172 if (num_slashes == 0 || num_slashes == 1) {
173 // No slashes means it's a relative path like "http:foo.html". One slash
174 // is an absolute path. "http:/home/foo.html"
175 *is_relative = true;
176 *relative_component = url_parse::MakeRange(colon_offset + 1, url_len);
177 return true;
178 }
179
180 // Two or more slashes after the scheme we treat as absolute.
181 return true;
182 }
183
184 // Copies all characters in the range [begin, end) of |spec| to the output,
185 // up until and including the last slash. There should be a slash in the
186 // range, if not, nothing will be copied.
187 //
188 // The input is assumed to be canonical, so we search only for exact slashes
189 // and not backslashes as well. We also know that it's ASCII.
190 void CopyToLastSlash(const char* spec,
191 int begin,
192 int end,
193 CanonOutput* output) {
194 // Find the last slash.
195 int last_slash = -1;
196 for (int i = end - 1; i >= begin; i--) {
197 if (spec[i] == '/') {
198 last_slash = i;
199 break;
200 }
201 }
202 if (last_slash < 0)
203 return; // No slash.
204
205 // Copy.
206 for (int i = begin; i <= last_slash; i++)
207 output->push_back(spec[i]);
208 }
209
210 // Copies a single component from the source to the output. This is used
211 // when resolving relative URLs and a given component is unchanged. Since the
212 // source should already be canonical, we don't have to do anything special,
213 // and the input is ASCII.
214 void CopyOneComponent(const char* source,
215 const url_parse::Component& source_component,
216 CanonOutput* output,
217 url_parse::Component* output_component) {
218 if (source_component.len < 0) {
219 // This component is not present.
220 *output_component = url_parse::Component();
221 return;
222 }
223
224 output_component->begin = output->length();
225 int source_end = source_component.end();
226 for (int i = source_component.begin; i < source_end; i++)
227 output->push_back(source[i]);
228 output_component->len = output->length() - output_component->begin;
229 }
230
231 #ifdef WIN32
232
233 // Called on Windows when the base URL is a file URL, this will copy the "C:"
234 // to the output, if there is a drive letter and if that drive letter is not
235 // being overridden by the relative URL. Otherwise, do nothing.
236 //
237 // It will return the index of the beginning of the next character in the
238 // base to be processed: if there is a "C:", the slash after it, or if
239 // there is no drive letter, the slash at the beginning of the path, or
240 // the end of the base. This can be used as the starting offset for further
241 // path processing.
242 template<typename CHAR>
243 int CopyBaseDriveSpecIfNecessary(const char* base_url,
244 int base_path_begin,
245 int base_path_end,
246 const CHAR* relative_url,
247 int path_start,
248 int relative_url_len,
249 CanonOutput* output) {
250 if (base_path_begin >= base_path_end)
251 return base_path_begin; // No path.
252
253 // If the relative begins with a drive spec, don't do anything. The existing
254 // drive spec in the base will be replaced.
255 if (url_parse::DoesBeginWindowsDriveSpec(relative_url,
256 path_start, relative_url_len)) {
257 return base_path_begin; // Relative URL path is "C:/foo"
258 }
259
260 // The path should begin with a slash (as all canonical paths do). We check
261 // if it is followed by a drive letter and copy it.
262 if (DoesBeginSlashWindowsDriveSpec(base_url,
263 base_path_begin,
264 base_path_end)) {
265 // Copy the two-character drive spec to the output. It will now look like
266 // "file:///C:" so the rest of it can be treated like a standard path.
267 output->push_back('/');
268 output->push_back(base_url[base_path_begin + 1]);
269 output->push_back(base_url[base_path_begin + 2]);
270 return base_path_begin + 3;
271 }
272
273 return base_path_begin;
274 }
275
276 #endif // WIN32
277
278 // A subroutine of DoResolveRelativeURL, this resolves the URL knowning that
279 // the input is a relative path or less (qyuery or ref).
280 template<typename CHAR>
281 bool DoResolveRelativePath(const char* base_url,
282 const url_parse::Parsed& base_parsed,
283 bool base_is_file,
284 const CHAR* relative_url,
285 const url_parse::Component& relative_component,
286 CharsetConverter* query_converter,
287 CanonOutput* output,
288 url_parse::Parsed* out_parsed) {
289 bool success = true;
290
291 // We know the authority section didn't change, copy it to the output. We
292 // also know we have a path so can copy up to there.
293 url_parse::Component path, query, ref;
294 url_parse::ParsePathInternal(relative_url,
295 relative_component,
296 &path,
297 &query,
298 &ref);
299 // Canonical URLs always have a path, so we can use that offset.
300 output->Append(base_url, base_parsed.path.begin);
301
302 if (path.len > 0) {
303 // The path is replaced or modified.
304 int true_path_begin = output->length();
305
306 // For file: URLs on Windows, we don't want to treat the drive letter and
307 // colon as part of the path for relative file resolution when the
308 // incoming URL does not provide a drive spec. We save the true path
309 // beginning so we can fix it up after we are done.
310 int base_path_begin = base_parsed.path.begin;
311 #ifdef WIN32
312 if (base_is_file) {
313 base_path_begin = CopyBaseDriveSpecIfNecessary(
314 base_url, base_parsed.path.begin, base_parsed.path.end(),
315 relative_url, relative_component.begin, relative_component.end(),
316 output);
317 // Now the output looks like either "file://" or "file:///C:"
318 // and we can start appending the rest of the path. |base_path_begin|
319 // points to the character in the base that comes next.
320 }
321 #endif // WIN32
322
323 if (url_parse::IsURLSlash(relative_url[path.begin])) {
324 // Easy case: the path is an absolute path on the server, so we can
325 // just replace everything from the path on with the new versions.
326 // Since the input should be canonical hierarchical URL, we should
327 // always have a path.
328 success &= CanonicalizePath(relative_url, path,
329 output, &out_parsed->path);
330 } else {
331 // Relative path, replace the query, and reference. We take the
332 // original path with the file part stripped, and append the new path.
333 // The canonicalizer will take care of resolving ".." and "."
334 int path_begin = output->length();
335 CopyToLastSlash(base_url, base_path_begin, base_parsed.path.end(),
336 output);
337 success &= CanonicalizePartialPath(relative_url, path, path_begin,
338 output);
339 out_parsed->path = url_parse::MakeRange(path_begin, output->length());
340
341 // Copy the rest of the stuff after the path from the relative path.
342 }
343
344 // Finish with the query and reference part (these can't fail).
345 CanonicalizeQuery(relative_url, query, query_converter,
346 output, &out_parsed->query);
347 CanonicalizeRef(relative_url, ref, output, &out_parsed->ref);
348
349 // Fix the path beginning to add back the "C:" we may have written above.
350 out_parsed->path = url_parse::MakeRange(true_path_begin,
351 out_parsed->path.end());
352 return success;
353 }
354
355 // If we get here, the path is unchanged: copy to output.
356 CopyOneComponent(base_url, base_parsed.path, output, &out_parsed->path);
357
358 if (query.is_valid()) {
359 // Just the query specified, replace the query and reference (ignore
360 // failures for refs)
361 CanonicalizeQuery(relative_url, query, query_converter,
362 output, &out_parsed->query);
363 CanonicalizeRef(relative_url, ref, output, &out_parsed->ref);
364 return success;
365 }
366
367 // If we get here, the query is unchanged: copy to output. Note that the
368 // range of the query parameter doesn't include the question mark, so we
369 // have to add it manually if there is a component.
370 if (base_parsed.query.is_valid())
371 output->push_back('?');
372 CopyOneComponent(base_url, base_parsed.query, output, &out_parsed->query);
373
374 if (ref.is_valid()) {
375 // Just the reference specified: replace it (ignoring failures).
376 CanonicalizeRef(relative_url, ref, output, &out_parsed->ref);
377 return success;
378 }
379
380 // We should always have something to do in this function, the caller checks
381 // that some component is being replaced.
382 DCHECK(false) << "Not reached";
383 return success;
384 }
385
386 // Resolves a relative URL that contains a host. Typically, these will
387 // be of the form "//www.google.com/foo/bar?baz#ref" and the only thing which
388 // should be kept from the original URL is the scheme.
389 template<typename CHAR>
390 bool DoResolveRelativeHost(const char* base_url,
391 const url_parse::Parsed& base_parsed,
392 const CHAR* relative_url,
393 const url_parse::Component& relative_component,
394 CharsetConverter* query_converter,
395 CanonOutput* output,
396 url_parse::Parsed* out_parsed) {
397 // Parse the relative URL, just like we would for anything following a
398 // scheme.
399 url_parse::Parsed relative_parsed; // Everything but the scheme is valid.
400 url_parse::ParseAfterScheme(&relative_url[relative_component.begin],
401 relative_component.len, relative_component.begin,
402 &relative_parsed);
403
404 // Now we can just use the replacement function to replace all the necessary
405 // parts of the old URL with the new one.
406 Replacements<CHAR> replacements;
407 replacements.SetUsername(relative_url, relative_parsed.username);
408 replacements.SetPassword(relative_url, relative_parsed.password);
409 replacements.SetHost(relative_url, relative_parsed.host);
410 replacements.SetPort(relative_url, relative_parsed.port);
411 replacements.SetPath(relative_url, relative_parsed.path);
412 replacements.SetQuery(relative_url, relative_parsed.query);
413 replacements.SetRef(relative_url, relative_parsed.ref);
414
415 return ReplaceStandardURL(base_url, base_parsed, replacements,
416 query_converter, output, out_parsed);
417 }
418
419 // Resolves a relative URL that happens to be an absolute file path. Examples
420 // include: "//hostname/path", "/c:/foo", and "//hostname/c:/foo".
421 template<typename CHAR>
422 bool DoResolveAbsoluteFile(const CHAR* relative_url,
423 const url_parse::Component& relative_component,
424 CharsetConverter* query_converter,
425 CanonOutput* output,
426 url_parse::Parsed* out_parsed) {
427 // Parse the file URL. The file URl parsing function uses the same logic
428 // as we do for determining if the file is absolute, in which case it will
429 // not bother to look for a scheme.
430 url_parse::Parsed relative_parsed;
431 url_parse::ParseFileURL(&relative_url[relative_component.begin],
432 relative_component.len, &relative_parsed);
433
434 return CanonicalizeFileURL(&relative_url[relative_component.begin],
435 relative_component.len, relative_parsed,
436 query_converter, output, out_parsed);
437 }
438
439 // TODO(brettw) treat two slashes as root like Mozilla for FTP?
440 template<typename CHAR>
441 bool DoResolveRelativeURL(const char* base_url,
442 const url_parse::Parsed& base_parsed,
443 bool base_is_file,
444 const CHAR* relative_url,
445 const url_parse::Component& relative_component,
446 CharsetConverter* query_converter,
447 CanonOutput* output,
448 url_parse::Parsed* out_parsed) {
449 // Starting point for our output parsed. We'll fix what we change.
450 *out_parsed = base_parsed;
451
452 // Sanity check: the input should have a host or we'll break badly below.
453 // We can only resolve relative URLs with base URLs that have hosts and
454 // paths (even the default path of "/" is OK).
455 //
456 // We allow hosts with no length so we can handle file URLs, for example.
457 if (base_parsed.path.len <= 0) {
458 // On error, return the input (resolving a relative URL on a non-relative
459 // base = the base).
460 int base_len = base_parsed.Length();
461 for (int i = 0; i < base_len; i++)
462 output->push_back(base_url[i]);
463 return false;
464 }
465
466 if (relative_component.len <= 0) {
467 // Empty relative URL, leave unchanged, only removing the ref component.
468 int base_len = base_parsed.Length();
469 base_len -= base_parsed.ref.len + 1;
470 out_parsed->ref.reset();
471 output->Append(base_url, base_len);
472 return true;
473 }
474
475 int num_slashes = url_parse::CountConsecutiveSlashes(
476 relative_url, relative_component.begin, relative_component.end());
477
478 #ifdef WIN32
479 // On Windows, two slashes for a file path (regardless of which direction
480 // they are) means that it's UNC. Two backslashes on any base scheme mean
481 // that it's an absolute UNC path (we use the base_is_file flag to control
482 // how strict the UNC finder is).
483 //
484 // We also allow Windows absolute drive specs on any scheme (for example
485 // "c:\foo") like IE does. There must be no preceeding slashes in this
486 // case (we reject anything like "/c:/foo") because that should be treated
487 // as a path. For file URLs, we allow any number of slashes since that would
488 // be setting the path.
489 //
490 // This assumes the absolute path resolver handles absolute URLs like this
491 // properly. url_util::DoCanonicalize does this.
492 int after_slashes = relative_component.begin + num_slashes;
493 if (url_parse::DoesBeginUNCPath(relative_url, relative_component.begin,
494 relative_component.end(), !base_is_file) ||
495 ((num_slashes == 0 || base_is_file) &&
496 url_parse::DoesBeginWindowsDriveSpec(relative_url, after_slashes,
497 relative_component.end()))) {
498 return DoResolveAbsoluteFile(relative_url, relative_component,
499 query_converter, output, out_parsed);
500 }
501 #else
502 // Other platforms need explicit handling for file: URLs with multiple
503 // slashes because the generic scheme parsing always extracts a host, but a
504 // file: URL only has a host if it has exactly 2 slashes. This also
505 // handles the special case where the URL is only slashes, since that
506 // doesn't have a host part either.
507 if (base_is_file &&
508 (num_slashes > 2 || num_slashes == relative_component.len)) {
509 return DoResolveAbsoluteFile(relative_url, relative_component,
510 query_converter, output, out_parsed);
511 }
512 #endif
513
514 // Any other double-slashes mean that this is relative to the scheme.
515 if (num_slashes >= 2) {
516 return DoResolveRelativeHost(base_url, base_parsed,
517 relative_url, relative_component,
518 query_converter, output, out_parsed);
519 }
520
521 // When we get here, we know that the relative URL is on the same host.
522 return DoResolveRelativePath(base_url, base_parsed, base_is_file,
523 relative_url, relative_component,
524 query_converter, output, out_parsed);
525 }
526
527 } // namespace
528
529 bool IsRelativeURL(const char* base,
530 const url_parse::Parsed& base_parsed,
531 const char* fragment,
532 int fragment_len,
533 bool is_base_hierarchical,
534 bool* is_relative,
535 url_parse::Component* relative_component) {
536 return DoIsRelativeURL<char>(
537 base, base_parsed, fragment, fragment_len, is_base_hierarchical,
538 is_relative, relative_component);
539 }
540
541 bool IsRelativeURL(const char* base,
542 const url_parse::Parsed& base_parsed,
543 const char16* fragment,
544 int fragment_len,
545 bool is_base_hierarchical,
546 bool* is_relative,
547 url_parse::Component* relative_component) {
548 return DoIsRelativeURL<char16>(
549 base, base_parsed, fragment, fragment_len, is_base_hierarchical,
550 is_relative, relative_component);
551 }
552
553 bool ResolveRelativeURL(const char* base_url,
554 const url_parse::Parsed& base_parsed,
555 bool base_is_file,
556 const char* relative_url,
557 const url_parse::Component& relative_component,
558 CharsetConverter* query_converter,
559 CanonOutput* output,
560 url_parse::Parsed* out_parsed) {
561 return DoResolveRelativeURL<char>(
562 base_url, base_parsed, base_is_file, relative_url,
563 relative_component, query_converter, output, out_parsed);
564 }
565
566 bool ResolveRelativeURL(const char* base_url,
567 const url_parse::Parsed& base_parsed,
568 bool base_is_file,
569 const char16* relative_url,
570 const url_parse::Component& relative_component,
571 CharsetConverter* query_converter,
572 CanonOutput* output,
573 url_parse::Parsed* out_parsed) {
574 return DoResolveRelativeURL<char16>(
575 base_url, base_parsed, base_is_file, relative_url,
576 relative_component, query_converter, output, out_parsed);
577 }
578
579 } // namespace url_canon
OLDNEW
« no previous file with comments | « url/url_canon_query.cc ('k') | url/url_canon_stdstring.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698