PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/cges/pymodules/python2.7/lib/python/urllib3/util/url.py

https://gitlab.com/pooja043/Globus_Docker_2
Python | 212 lines | 201 code | 7 blank | 4 comment | 0 complexity | be63feaaf6fb96e6021ca8ff171cfb63 MD5 | raw file
  1. from collections import namedtuple
  2. from ..exceptions import LocationParseError
  3. url_attrs = ['scheme', 'auth', 'host', 'port', 'path', 'query', 'fragment']
  4. class Url(namedtuple('Url', url_attrs)):
  5. """
  6. Datastructure for representing an HTTP URL. Used as a return value for
  7. :func:`parse_url`.
  8. """
  9. slots = ()
  10. def __new__(cls, scheme=None, auth=None, host=None, port=None, path=None,
  11. query=None, fragment=None):
  12. return super(Url, cls).__new__(cls, scheme, auth, host, port, path,
  13. query, fragment)
  14. @property
  15. def hostname(self):
  16. """For backwards-compatibility with urlparse. We're nice like that."""
  17. return self.host
  18. @property
  19. def request_uri(self):
  20. """Absolute path including the query string."""
  21. uri = self.path or '/'
  22. if self.query is not None:
  23. uri += '?' + self.query
  24. return uri
  25. @property
  26. def netloc(self):
  27. """Network location including host and port"""
  28. if self.port:
  29. return '%s:%d' % (self.host, self.port)
  30. return self.host
  31. @property
  32. def url(self):
  33. """
  34. Convert self into a url
  35. This function should more or less round-trip with :func:`.parse_url`. The
  36. returned url may not be exactly the same as the url inputted to
  37. :func:`.parse_url`, but it should be equivalent by the RFC (e.g., urls
  38. with a blank port will have : removed).
  39. Example: ::
  40. >>> U = parse_url('http://google.com/mail/')
  41. >>> U.url
  42. 'http://google.com/mail/'
  43. >>> Url('http', 'username:password', 'host.com', 80,
  44. ... '/path', 'query', 'fragment').url
  45. 'http://username:password@host.com:80/path?query#fragment'
  46. """
  47. scheme, auth, host, port, path, query, fragment = self
  48. url = ''
  49. # We use "is not None" we want things to happen with empty strings (or 0 port)
  50. if scheme is not None:
  51. url += scheme + '://'
  52. if auth is not None:
  53. url += auth + '@'
  54. if host is not None:
  55. url += host
  56. if port is not None:
  57. url += ':' + str(port)
  58. if path is not None:
  59. url += path
  60. if query is not None:
  61. url += '?' + query
  62. if fragment is not None:
  63. url += '#' + fragment
  64. return url
  65. def __str__(self):
  66. return self.url
  67. def split_first(s, delims):
  68. """
  69. Given a string and an iterable of delimiters, split on the first found
  70. delimiter. Return two split parts and the matched delimiter.
  71. If not found, then the first part is the full input string.
  72. Example::
  73. >>> split_first('foo/bar?baz', '?/=')
  74. ('foo', 'bar?baz', '/')
  75. >>> split_first('foo/bar?baz', '123')
  76. ('foo/bar?baz', '', None)
  77. Scales linearly with number of delims. Not ideal for large number of delims.
  78. """
  79. min_idx = None
  80. min_delim = None
  81. for d in delims:
  82. idx = s.find(d)
  83. if idx < 0:
  84. continue
  85. if min_idx is None or idx < min_idx:
  86. min_idx = idx
  87. min_delim = d
  88. if min_idx is None or min_idx < 0:
  89. return s, '', None
  90. return s[:min_idx], s[min_idx+1:], min_delim
  91. def parse_url(url):
  92. """
  93. Given a url, return a parsed :class:`.Url` namedtuple. Best-effort is
  94. performed to parse incomplete urls. Fields not provided will be None.
  95. Partly backwards-compatible with :mod:`urlparse`.
  96. Example::
  97. >>> parse_url('http://google.com/mail/')
  98. Url(scheme='http', host='google.com', port=None, path='/mail/', ...)
  99. >>> parse_url('google.com:80')
  100. Url(scheme=None, host='google.com', port=80, path=None, ...)
  101. >>> parse_url('/foo?bar')
  102. Url(scheme=None, host=None, port=None, path='/foo', query='bar', ...)
  103. """
  104. # While this code has overlap with stdlib's urlparse, it is much
  105. # simplified for our needs and less annoying.
  106. # Additionally, this implementations does silly things to be optimal
  107. # on CPython.
  108. if not url:
  109. # Empty
  110. return Url()
  111. scheme = None
  112. auth = None
  113. host = None
  114. port = None
  115. path = None
  116. fragment = None
  117. query = None
  118. # Scheme
  119. if '://' in url:
  120. scheme, url = url.split('://', 1)
  121. # Find the earliest Authority Terminator
  122. # (http://tools.ietf.org/html/rfc3986#section-3.2)
  123. url, path_, delim = split_first(url, ['/', '?', '#'])
  124. if delim:
  125. # Reassemble the path
  126. path = delim + path_
  127. # Auth
  128. if '@' in url:
  129. # Last '@' denotes end of auth part
  130. auth, url = url.rsplit('@', 1)
  131. # IPv6
  132. if url and url[0] == '[':
  133. host, url = url.split(']', 1)
  134. host += ']'
  135. # Port
  136. if ':' in url:
  137. _host, port = url.split(':', 1)
  138. if not host:
  139. host = _host
  140. if port:
  141. # If given, ports must be integers.
  142. if not port.isdigit():
  143. raise LocationParseError(url)
  144. port = int(port)
  145. else:
  146. # Blank ports are cool, too. (rfc3986#section-3.2.3)
  147. port = None
  148. elif not host and url:
  149. host = url
  150. if not path:
  151. return Url(scheme, auth, host, port, path, query, fragment)
  152. # Fragment
  153. if '#' in path:
  154. path, fragment = path.split('#', 1)
  155. # Query
  156. if '?' in path:
  157. path, query = path.split('?', 1)
  158. return Url(scheme, auth, host, port, path, query, fragment)
  159. def get_host(url):
  160. """
  161. Deprecated. Use :func:`.parse_url` instead.
  162. """
  163. p = parse_url(url)
  164. return p.scheme or 'http', p.hostname, p.port