Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

168 wiersze
5.1KB

  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. """Module with functions to normalize components."""
  16. import re
  17. from . import compat
  18. from . import misc
  19. def normalize_scheme(scheme):
  20. """Normalize the scheme component."""
  21. return scheme.lower()
  22. def normalize_authority(authority):
  23. """Normalize an authority tuple to a string."""
  24. userinfo, host, port = authority
  25. result = ''
  26. if userinfo:
  27. result += normalize_percent_characters(userinfo) + '@'
  28. if host:
  29. result += normalize_host(host)
  30. if port:
  31. result += ':' + port
  32. return result
  33. def normalize_username(username):
  34. """Normalize a username to make it safe to include in userinfo."""
  35. return compat.urlquote(username)
  36. def normalize_password(password):
  37. """Normalize a password to make safe for userinfo."""
  38. return compat.urlquote(password)
  39. def normalize_host(host):
  40. """Normalize a host string."""
  41. if misc.IPv6_MATCHER.match(host):
  42. percent = host.find('%')
  43. if percent != -1:
  44. percent_25 = host.find('%25')
  45. # Replace RFC 4007 IPv6 Zone ID delimiter '%' with '%25'
  46. # from RFC 6874. If the host is '[<IPv6 addr>%25]' then we
  47. # assume RFC 4007 and normalize to '[<IPV6 addr>%2525]'
  48. if percent_25 == -1 or percent < percent_25 or \
  49. (percent == percent_25 and percent_25 == len(host) - 4):
  50. host = host.replace('%', '%25', 1)
  51. # Don't normalize the casing of the Zone ID
  52. return host[:percent].lower() + host[percent:]
  53. return host.lower()
  54. def normalize_path(path):
  55. """Normalize the path string."""
  56. if not path:
  57. return path
  58. path = normalize_percent_characters(path)
  59. return remove_dot_segments(path)
  60. def normalize_query(query):
  61. """Normalize the query string."""
  62. if not query:
  63. return query
  64. return normalize_percent_characters(query)
  65. def normalize_fragment(fragment):
  66. """Normalize the fragment string."""
  67. if not fragment:
  68. return fragment
  69. return normalize_percent_characters(fragment)
  70. PERCENT_MATCHER = re.compile('%[A-Fa-f0-9]{2}')
  71. def normalize_percent_characters(s):
  72. """All percent characters should be upper-cased.
  73. For example, ``"%3afoo%DF%ab"`` should be turned into ``"%3Afoo%DF%AB"``.
  74. """
  75. matches = set(PERCENT_MATCHER.findall(s))
  76. for m in matches:
  77. if not m.isupper():
  78. s = s.replace(m, m.upper())
  79. return s
  80. def remove_dot_segments(s):
  81. """Remove dot segments from the string.
  82. See also Section 5.2.4 of :rfc:`3986`.
  83. """
  84. # See http://tools.ietf.org/html/rfc3986#section-5.2.4 for pseudo-code
  85. segments = s.split('/') # Turn the path into a list of segments
  86. output = [] # Initialize the variable to use to store output
  87. for segment in segments:
  88. # '.' is the current directory, so ignore it, it is superfluous
  89. if segment == '.':
  90. continue
  91. # Anything other than '..', should be appended to the output
  92. elif segment != '..':
  93. output.append(segment)
  94. # In this case segment == '..', if we can, we should pop the last
  95. # element
  96. elif output:
  97. output.pop()
  98. # If the path starts with '/' and the output is empty or the first string
  99. # is non-empty
  100. if s.startswith('/') and (not output or output[0]):
  101. output.insert(0, '')
  102. # If the path starts with '/.' or '/..' ensure we add one more empty
  103. # string to add a trailing '/'
  104. if s.endswith(('/.', '/..')):
  105. output.append('')
  106. return '/'.join(output)
  107. def encode_component(uri_component, encoding):
  108. """Encode the specific component in the provided encoding."""
  109. if uri_component is None:
  110. return uri_component
  111. # Try to see if the component we're encoding is already percent-encoded
  112. # so we can skip all '%' characters but still encode all others.
  113. percent_encodings = len(PERCENT_MATCHER.findall(
  114. compat.to_str(uri_component, encoding)))
  115. uri_bytes = compat.to_bytes(uri_component, encoding)
  116. is_percent_encoded = percent_encodings == uri_bytes.count(b'%')
  117. encoded_uri = bytearray()
  118. for i in range(0, len(uri_bytes)):
  119. # Will return a single character bytestring on both Python 2 & 3
  120. byte = uri_bytes[i:i+1]
  121. byte_ord = ord(byte)
  122. if ((is_percent_encoded and byte == b'%')
  123. or (byte_ord < 128 and byte.decode() in misc.NON_PCT_ENCODED)):
  124. encoded_uri.extend(byte)
  125. continue
  126. encoded_uri.extend('%{0:02x}'.format(byte_ord).encode().upper())
  127. return encoded_uri.decode(encoding)