url_parse_internal.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef URL_URL_PARSE_INTERNAL_H_
  5. #define URL_URL_PARSE_INTERNAL_H_
  6. // Contains common inline helper functions used by the URL parsing routines.
  7. #include "url/third_party/mozilla/url_parse.h"
  8. namespace url {
  9. // We treat slashes and backslashes the same for IE compatibility.
  10. inline bool IsURLSlash(char16_t ch) {
  11. return ch == '/' || ch == '\\';
  12. }
  13. inline bool IsURLSlash(char ch) {
  14. return IsURLSlash(static_cast<char16_t>(ch));
  15. }
  16. // Returns true if we should trim this character from the URL because it is a
  17. // space or a control character.
  18. inline bool ShouldTrimFromURL(char16_t ch) {
  19. return ch <= ' ';
  20. }
  21. inline bool ShouldTrimFromURL(char ch) {
  22. return ShouldTrimFromURL(static_cast<char16_t>(ch));
  23. }
  24. // Given an already-initialized begin index and length, this shrinks the range
  25. // to eliminate "should-be-trimmed" characters. Note that the length does *not*
  26. // indicate the length of untrimmed data from |*begin|, but rather the position
  27. // in the input string (so the string starts at character |*begin| in the spec,
  28. // and goes until |*len|).
  29. template<typename CHAR>
  30. inline void TrimURL(const CHAR* spec, int* begin, int* len,
  31. bool trim_path_end = true) {
  32. // Strip leading whitespace and control characters.
  33. while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
  34. (*begin)++;
  35. if (trim_path_end) {
  36. // Strip trailing whitespace and control characters. We need the >i test
  37. // for when the input string is all blanks; we don't want to back past the
  38. // input.
  39. while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
  40. (*len)--;
  41. }
  42. }
  43. // Counts the number of consecutive slashes starting at the given offset
  44. // in the given string of the given length.
  45. template<typename CHAR>
  46. inline int CountConsecutiveSlashes(const CHAR *str,
  47. int begin_offset, int str_len) {
  48. int count = 0;
  49. while (begin_offset + count < str_len &&
  50. IsURLSlash(str[begin_offset + count]))
  51. ++count;
  52. return count;
  53. }
  54. // Internal functions in url_parse.cc that parse the path, that is, everything
  55. // following the authority section. The input is the range of everything
  56. // following the authority section, and the output is the identified ranges.
  57. //
  58. // This is designed for the file URL parser or other consumers who may do
  59. // special stuff at the beginning, but want regular path parsing, it just
  60. // maps to the internal parsing function for paths.
  61. void ParsePathInternal(const char* spec,
  62. const Component& path,
  63. Component* filepath,
  64. Component* query,
  65. Component* ref);
  66. void ParsePathInternal(const char16_t* spec,
  67. const Component& path,
  68. Component* filepath,
  69. Component* query,
  70. Component* ref);
  71. // Given a spec and a pointer to the character after the colon following the
  72. // scheme, this parses it and fills in the structure, Every item in the parsed
  73. // structure is filled EXCEPT for the scheme, which is untouched.
  74. void ParseAfterScheme(const char* spec,
  75. int spec_len,
  76. int after_scheme,
  77. Parsed* parsed);
  78. void ParseAfterScheme(const char16_t* spec,
  79. int spec_len,
  80. int after_scheme,
  81. Parsed* parsed);
  82. } // namespace url
  83. #endif // URL_URL_PARSE_INTERNAL_H_