normalizers.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2014 Rackspace
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import re
  16. from .compat import to_bytes
  17. from .misc import NON_PCT_ENCODED
  18. def normalize_scheme(scheme):
  19. return scheme.lower()
  20. def normalize_authority(authority):
  21. userinfo, host, port = authority
  22. result = ''
  23. if userinfo:
  24. result += normalize_percent_characters(userinfo) + '@'
  25. if host:
  26. result += host.lower()
  27. if port:
  28. result += ':' + port
  29. return result
  30. def normalize_path(path):
  31. if not path:
  32. return path
  33. path = normalize_percent_characters(path)
  34. return remove_dot_segments(path)
  35. def normalize_query(query):
  36. return normalize_percent_characters(query)
  37. def normalize_fragment(fragment):
  38. return normalize_percent_characters(fragment)
  39. PERCENT_MATCHER = re.compile('%[A-Fa-f0-9]{2}')
  40. def normalize_percent_characters(s):
  41. """All percent characters should be upper-cased.
  42. For example, ``"%3afoo%DF%ab"`` should be turned into ``"%3Afoo%DF%AB"``.
  43. """
  44. matches = set(PERCENT_MATCHER.findall(s))
  45. for m in matches:
  46. if not m.isupper():
  47. s = s.replace(m, m.upper())
  48. return s
  49. def remove_dot_segments(s):
  50. # See http://tools.ietf.org/html/rfc3986#section-5.2.4 for pseudo-code
  51. segments = s.split('/') # Turn the path into a list of segments
  52. output = [] # Initialize the variable to use to store output
  53. for segment in segments:
  54. # '.' is the current directory, so ignore it, it is superfluous
  55. if segment == '.':
  56. continue
  57. # Anything other than '..', should be appended to the output
  58. elif segment != '..':
  59. output.append(segment)
  60. # In this case segment == '..', if we can, we should pop the last
  61. # element
  62. elif output:
  63. output.pop()
  64. # If the path starts with '/' and the output is empty or the first string
  65. # is non-empty
  66. if s.startswith('/') and (not output or output[0]):
  67. output.insert(0, '')
  68. # If the path starts with '/.' or '/..' ensure we add one more empty
  69. # string to add a trailing '/'
  70. if s.endswith(('/.', '/..')):
  71. output.append('')
  72. return '/'.join(output)
  73. def encode_component(uri_component, encoding):
  74. if uri_component is None:
  75. return uri_component
  76. uri_bytes = to_bytes(uri_component, encoding)
  77. encoded_uri = bytearray()
  78. for i in range(0, len(uri_bytes)):
  79. # Will return a single character bytestring on both Python 2 & 3
  80. byte = uri_bytes[i:i+1]
  81. byte_ord = ord(byte)
  82. if byte_ord < 128 and byte.decode() in NON_PCT_ENCODED:
  83. encoded_uri.extend(byte)
  84. continue
  85. encoded_uri.extend('%{0:02x}'.format(byte_ord).encode())
  86. return encoded_uri.decode(encoding)