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// As with url_canon_internal.h, this file is intended to be included in 31// another C++ file where the template types are defined. This allows the 32// programmer to use this to use these functions for their own strings 33// types, without bloating the code by having inline templates used in 34// every call site. 35// 36// *** This file must be included after url_canon_internal as we depend on some 37// functions in it. *** 38 39#ifndef GOOGLEURL_SRC_URL_CANON_INTERNAL_FILE_H__ 40#define GOOGLEURL_SRC_URL_CANON_INTERNAL_FILE_H__ 41 42#include "googleurl/src/url_file.h" 43#include "googleurl/src/url_parse_internal.h" 44 45using namespace url_canon; 46 47// Given a pointer into the spec, this copies and canonicalizes the drive 48// letter and colon to the output, if one is found. If there is not a drive 49// spec, it won't do anything. The index of the next character in the input 50// spec is returned (after the colon when a drive spec is found, the begin 51// offset if one is not). 52template<typename CHAR> 53static int FileDoDriveSpec(const CHAR* spec, int begin, int end, 54 CanonOutput* output) { 55 // The path could be one of several things: /foo/bar, c:/foo/bar, /c:/foo, 56 // (with backslashes instead of slashes as well). 57 int num_slashes = CountConsecutiveSlashes(spec, begin, end); 58 int after_slashes = begin + num_slashes; 59 60 if (!DoesBeginWindowsDriveSpec(spec, after_slashes, end)) 61 return begin; // Haven't consumed any characters 62 63 // DoesBeginWindowsDriveSpec will ensure that the drive letter is valid 64 // and that it is followed by a colon/pipe. 65 66 // Normalize Windows drive letters to uppercase 67 if (spec[after_slashes] >= 'a' && spec[after_slashes] <= 'z') 68 output->push_back(spec[after_slashes] - 'a' + 'A'); 69 else 70 output->push_back(static_cast<char>(spec[after_slashes])); 71 72 // Normalize the character following it to a colon rather than pipe. 73 output->push_back(':'); 74 output->push_back('/'); 75 return after_slashes + 2; 76} 77 78// FileDoDriveSpec will have already added the first backslash, so we need to 79// write everything following the slashes using the path canonicalizer. 80template<typename CHAR, typename UCHAR> 81static void FileDoPath(const CHAR* spec, int begin, int end, 82 CanonOutput* output) { 83 // Normalize the number of slashes after the drive letter. The path 84 // canonicalizer expects the input to begin in a slash already so 85 // doesn't check. We want to handle no-slashes 86 int num_slashes = CountConsecutiveSlashes(spec, begin, end); 87 int after_slashes = begin + num_slashes; 88 89 // Now use the regular path canonicalizer to canonicalize the rest of the 90 // path. We supply it with the path following the slashes. It won't prepend 91 // a slash because it assumes any nonempty path already starts with one. 92 // We explicitly filter out calls with no path here to prevent that case. 93 ParsedURL::Component sub_path(after_slashes, end - after_slashes); 94 if (sub_path.len > 0) { 95 // Give it a fake output component to write into. DoCanonicalizeFile will 96 // compute the full path component. 97 ParsedURL::Component fake_output_path; 98 URLCanonInternal<CHAR, UCHAR>::DoPath( 99 spec, sub_path, output, &fake_output_path); 100 } 101} 102 103template<typename CHAR, typename UCHAR> 104static bool DoCanonicalizeFileURL(const URLComponentSource<CHAR>& source, 105 const ParsedURL& parsed, 106 CanonOutput* output, 107 ParsedURL* new_parsed) { 108 // Things we don't set in file: URLs. 109 new_parsed->username = ParsedURL::Component(0, -1); 110 new_parsed->password = ParsedURL::Component(0, -1); 111 new_parsed->port = ParsedURL::Component(0, -1); 112 113 // Scheme (known, so we don't bother running it through the more 114 // complicated scheme canonicalizer). 115 new_parsed->scheme.begin = output->length(); 116 output->push_back('f'); 117 output->push_back('i'); 118 output->push_back('l'); 119 output->push_back('e'); 120 new_parsed->scheme.len = output->length() - new_parsed->scheme.begin; 121 output->push_back(':'); 122 123 // Write the separator for the host. 124 output->push_back('/'); 125 output->push_back('/'); 126 127 // Append the host. For many file URLs, this will be empty. For UNC, this 128 // will be present. 129 // TODO(brettw) This doesn't do any checking for host name validity. We 130 // should probably handle validity checking of UNC hosts differently than 131 // for regular IP hosts. 132 bool success = URLCanonInternal<CHAR, UCHAR>::DoHost( 133 source.host, parsed.host, output, &new_parsed->host); 134 135 // Write a separator for the start of the path. We'll ignore any slashes 136 // already at the beginning of the path. 137 new_parsed->path.begin = output->length(); 138 output->push_back('/'); 139 140 // Copies and normalizes the "c:" at the beginning, if present. 141 int after_drive = FileDoDriveSpec(source.path, parsed.path.begin, 142 parsed.path.end(), output); 143 144 // Copies the rest of the path 145 FileDoPath<CHAR, UCHAR>(source.path, after_drive, parsed.path.end(), output); 146 new_parsed->path.len = output->length() - new_parsed->path.begin; 147 148 // Things following the path we can use the standard canonicalizers for. 149 success &= URLCanonInternal<CHAR, UCHAR>::DoQuery( 150 source.query, parsed.query, output, &new_parsed->query); 151 success &= URLCanonInternal<CHAR, UCHAR>::DoRef( 152 source.ref, parsed.ref, output, &new_parsed->ref); 153 154 return success; 155} 156 157#endif // GOOGLEURL_SRC_URL_CANON_INTERNAL_FILE_H__ 158