def parse_url(self): """ Parse the proxy url into its component pieces """ # NOTE: If this changes, update tests/regex/proxy.py # # proxy=[protocol://][username[:password]@]host[:port][path] # groups # 1 = protocol # 2 = username:password@ # 3 = username # 4 = password # 5 = hostname # 6 = port # 7 = extra m = PROXY_URL_PARSE.match(self.url) if not m: raise ProxyStringError("malformed url, cannot parse it.") # If no protocol was given default to http. if m.group(1): self.protocol = m.group(1) else: self.protocol = "http://" if m.group(3): self.username = m.group(3) if m.group(4): # Skip the leading colon self.password = m.group(4)[1:] if m.group(5): self.host = m.group(5) if m.group(6): # Skip the leading colon self.port = m.group(6)[1:] else: raise ProxyStringError("url has no host component") self.parse_components()
def proxy_regex_test(self): """ Run a list of possible proxy= values through the regex and check for correct results. tests are in the form of: (proxy string, match.groups() tuple) """ tests = [ ("proxy.host", (None, None, None, None, 'proxy.host', None, None)), ("proxy.host:3128", (None, None, None, None, 'proxy.host', ':3128', None)), ("user:[email protected]", (None, 'user:password@', 'user', ':password', 'proxy.host', None, None)), ("*****@*****.**", (None, 'user@', 'user', None, 'proxy.host', None, None)), ("user:[email protected]:3128", (None, 'user:password@', 'user', ':password', 'proxy.host', ':3128', None)), ("[email protected]:3128", (None, 'user@', 'user', None, 'proxy.host', ':3128', None)), ("proxy.host/blah/blah", (None, None, None, None, 'proxy.host', None, '/blah/blah')), ("proxy.host:3128/blah/blah", (None, None, None, None, 'proxy.host', ':3128', '/blah/blah')), ("user:[email protected]/blah/blah", (None, 'user:password@', 'user', ':password', 'proxy.host', None, '/blah/blah')), ("[email protected]/blah/blah", (None, 'user@', 'user', None, 'proxy.host', None, '/blah/blah')), ("user:[email protected]:3128/blah/blah", (None, 'user:password@', 'user', ':password', 'proxy.host', ':3128', "/blah/blah")), ("[email protected]:3128/blah/blah", (None, 'user@', 'user', None, 'proxy.host', ':3128', "/blah/blah")), ("http://proxy.host", ('http://', None, None, None, 'proxy.host', None, None)), ("http://proxy.host:3128", ('http://', None, None, None, 'proxy.host', ':3128', None)), ("http://*****:*****@proxy.host", ('http://', 'user:password@', 'user', ':password', 'proxy.host', None, None)), ("http://[email protected]", ('http://', 'user@', 'user', None, 'proxy.host', None, None)), ("http://*****:*****@proxy.host:3128", ('http://', 'user:password@', 'user', ':password', 'proxy.host', ':3128', None)), ("http://[email protected]:3128", ('http://', 'user@', 'user', None, 'proxy.host', ':3128', None)), ("http://proxy.host/blah/blah", ('http://', None, None, None, 'proxy.host', None, '/blah/blah')), ("http://proxy.host:3128/blah/blah", ('http://', None, None, None, 'proxy.host', ':3128', '/blah/blah')), ("http://*****:*****@proxy.host/blah/blah", ("http://", 'user:password@', 'user', ':password', 'proxy.host', None, '/blah/blah')), ("http://[email protected]/blah/blah", ("http://", 'user@', 'user', None, 'proxy.host', None, '/blah/blah')), ("http://*****:*****@proxy.host:3128/blah/blah", ("http://", 'user:password@', 'user', ':password', 'proxy.host', ':3128', '/blah/blah')), ("http://[email protected]:3128/blah/blah", ("http://", 'user@', 'user', None, 'proxy.host', ':3128', '/blah/blah')), ] got_error = False for proxy, result in tests: try: self.assertEqual(PROXY_URL_PARSE.match(proxy).groups(), result) except AssertionError: got_error = True print("Proxy parse error: `%s' did not parse as `%s'" % (proxy, result)) if got_error: self.fail()
def proxy_regex_test(self): """ Run a list of possible proxy= values through the regex and check for correct results. tests are in the form of: (proxy string, match.groups() tuple) """ tests = [ ( "proxy.host", (None, None, None, None, 'proxy.host', None, None) ), ( "proxy.host:3128", (None, None, None, None, 'proxy.host', ':3128', None) ), ( "user:[email protected]", (None, 'user:password@', 'user', ':password', 'proxy.host', None, None) ), ( "*****@*****.**", (None, 'user@', 'user', None, 'proxy.host', None, None) ), ( "user:[email protected]:3128", (None, 'user:password@', 'user', ':password', 'proxy.host', ':3128', None) ), ( "[email protected]:3128", (None, 'user@', 'user', None, 'proxy.host', ':3128', None) ), ( "proxy.host/blah/blah", (None, None, None, None, 'proxy.host', None, '/blah/blah') ), ( "proxy.host:3128/blah/blah", (None, None, None, None, 'proxy.host', ':3128', '/blah/blah') ), ( "user:[email protected]/blah/blah", (None, 'user:password@', 'user', ':password', 'proxy.host', None, '/blah/blah') ), ( "[email protected]/blah/blah", (None, 'user@', 'user', None, 'proxy.host', None, '/blah/blah') ), ( "user:[email protected]:3128/blah/blah", (None, 'user:password@', 'user', ':password', 'proxy.host', ':3128', "/blah/blah") ), ( "[email protected]:3128/blah/blah", (None, 'user@', 'user', None, 'proxy.host', ':3128', "/blah/blah") ), ( "http://proxy.host", ('http://', None, None, None, 'proxy.host', None, None) ), ( "http://proxy.host:3128", ('http://', None, None, None, 'proxy.host', ':3128', None) ), ( "http://*****:*****@proxy.host", ('http://', 'user:password@', 'user', ':password', 'proxy.host', None, None) ), ( "http://[email protected]", ('http://', 'user@', 'user', None, 'proxy.host', None, None) ), ( "http://*****:*****@proxy.host:3128", ('http://', 'user:password@', 'user', ':password', 'proxy.host', ':3128', None) ), ( "http://[email protected]:3128", ('http://', 'user@', 'user', None, 'proxy.host', ':3128', None) ), ( "http://proxy.host/blah/blah", ('http://', None, None, None, 'proxy.host', None, '/blah/blah') ), ( "http://proxy.host:3128/blah/blah", ('http://', None, None, None, 'proxy.host', ':3128', '/blah/blah') ), ( "http://*****:*****@proxy.host/blah/blah", ("http://", 'user:password@', 'user', ':password', 'proxy.host', None, '/blah/blah') ), ( "http://[email protected]/blah/blah", ("http://", 'user@', 'user', None, 'proxy.host', None, '/blah/blah') ), ( "http://*****:*****@proxy.host:3128/blah/blah", ("http://", 'user:password@', 'user', ':password', 'proxy.host', ':3128', '/blah/blah') ), ( "http://[email protected]:3128/blah/blah", ("http://", 'user@', 'user', None, 'proxy.host', ':3128', '/blah/blah') ), ] got_error = False for proxy, result in tests: try: self.assertEqual(PROXY_URL_PARSE.match(proxy).groups(), result) except AssertionError: got_error = True print("Proxy parse error: `%s' did not parse as `%s'" % (proxy, result)) if got_error: self.fail()