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 // Contains common inline helper functions used by the URL parsing routines.
     31 
     32 #ifndef GOOGLEURL_SRC_URL_PARSE_INTERNAL_H__
     33 #define GOOGLEURL_SRC_URL_PARSE_INTERNAL_H__
     34 
     35 #include "googleurl/src/url_parse.h"
     36 
     37 namespace url_parse {
     38 
     39 // We treat slashes and backslashes the same for IE compatability.
     40 inline bool IsURLSlash(char16 ch) {
     41   return ch == '/' || ch == '\\';
     42 }
     43 
     44 // Returns true if we should trim this character from the URL because it is a
     45 // space or a control character.
     46 inline bool ShouldTrimFromURL(char16 ch) {
     47   return ch <= ' ';
     48 }
     49 
     50 // Given an already-initialized begin index and length, this shrinks the range
     51 // to eliminate "should-be-trimmed" characters. Note that the length does *not*
     52 // indicate the length of untrimmed data from |*begin|, but rather the position
     53 // in the input string (so the string starts at character |*begin| in the spec,
     54 // and goes until |*len|).
     55 template<typename CHAR>
     56 inline void TrimURL(const CHAR* spec, int* begin, int* len) {
     57   // Strip leading whitespace and control characters.
     58   while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
     59     (*begin)++;
     60 
     61   // Strip trailing whitespace and control characters. We need the >i test for
     62   // when the input string is all blanks; we don't want to back past the input.
     63   while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
     64     (*len)--;
     65 }
     66 
     67 // Counts the number of consecutive slashes starting at the given offset
     68 // in the given string of the given length.
     69 template<typename CHAR>
     70 inline int CountConsecutiveSlashes(const CHAR *str,
     71                                    int begin_offset, int str_len) {
     72   int count = 0;
     73   while (begin_offset + count < str_len &&
     74          IsURLSlash(str[begin_offset + count]))
     75     ++count;
     76   return count;
     77 }
     78 
     79 // Internal functions in url_parse.cc that parse the path, that is, everything
     80 // following the authority section. The input is the range of everything
     81 // following the authority section, and the output is the identified ranges.
     82 //
     83 // This is designed for the file URL parser or other consumers who may do
     84 // special stuff at the beginning, but want regular path parsing, it just
     85 // maps to the internal parsing function for paths.
     86 void ParsePathInternal(const char* spec,
     87                        const Component& path,
     88                        Component* filepath,
     89                        Component* query,
     90                        Component* ref);
     91 void ParsePathInternal(const char16* spec,
     92                        const Component& path,
     93                        Component* filepath,
     94                        Component* query,
     95                        Component* ref);
     96 
     97 
     98 // Given a spec and a pointer to the character after the colon following the
     99 // scheme, this parses it and fills in the structure, Every item in the parsed
    100 // structure is filled EXCEPT for the scheme, which is untouched.
    101 void ParseAfterScheme(const char* spec,
    102                       int spec_len,
    103                       int after_scheme,
    104                       Parsed* parsed);
    105 void ParseAfterScheme(const char16* spec,
    106                       int spec_len,
    107                       int after_scheme,
    108                       Parsed* parsed);
    109 
    110 }  // namespace url_parse
    111 
    112 #endif  // GOOGLEURL_SRC_URL_PARSE_INTERNAL_H__
    113