Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

https proxy tunneling - add a test (not perfect, but covers all impl) and fix for py3

Konstantin Lopuhin0f527849f2e8
diff --git a/scrapy/core/downloader/handlers/http11.py b/scrapy/core/downloader/handlers/http11.py
index d81093a9f..729b80b05 100644
--- a/scrapy/core/downloader/handlers/http11.py
+++ b/scrapy/core/downloader/handlers/http11.py
@@ -78,7 +78,7 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
for it.
"""
- _responseMatcher = re.compile('HTTP/1\.. 200')
+ _responseMatcher = re.compile(b'HTTP/1\.. 200')
def __init__(self, reactor, host, port, proxyConf, contextFactory,
timeout=30, bindAddress=None):
@@ -92,11 +92,15 @@ class TunnelingTCP4ClientEndpoint(TCP4ClientEndpoint):
def requestTunnel(self, protocol):
"""Asks the proxy to open a tunnel."""
- tunnelReq = 'CONNECT %s:%s HTTP/1.1\r\n' % (self._tunneledHost,
- self._tunneledPort)
+ tunnelReq = (
+ b'CONNECT ' +
+ to_bytes(self._tunneledHost, encoding='ascii') + b':' +
+ to_bytes(str(self._tunneledPort)) +
+ b' HTTP/1.1\r\n')
if self._proxyAuthHeader:
- tunnelReq += 'Proxy-Authorization: %s\r\n' % self._proxyAuthHeader
- tunnelReq += '\r\n'
+ tunnelReq += \
+ b'Proxy-Authorization: ' + self._proxyAuthHeader + b'\r\n'
+ tunnelReq += b'\r\n'
protocol.transport.write(tunnelReq)
self._protocolDataReceived = protocol.dataReceived
protocol.dataReceived = self.processProxyResponse
diff --git a/tests/test_downloader_handlers.py b/tests/test_downloader_handlers.py
index 999fa4c0a..2d6c05741 100644
--- a/tests/test_downloader_handlers.py
+++ b/tests/test_downloader_handlers.py
@@ -388,6 +388,16 @@ class HttpProxyTestCase(unittest.TestCase):
request = Request('https://example.com', meta={'proxy': http_proxy})
return self.download_request(request, Spider('foo')).addCallback(_test)
+ @defer.inlineCallbacks
+ def test_download_with_proxy_https_timeout(self):
+ http_proxy = self.getURL('')
+ domain = 'https://no-such-domain.nosuch'
+ request = Request(
+ domain, meta={'proxy': http_proxy, 'download_timeout': 0.2})
+ d = self.download_request(request, Spider('foo'))
+ timeout = yield self.assertFailure(d, error.TimeoutError)
+ self.assertIn(domain, timeout.osError)
+
def test_download_without_proxy(self):
def _test(response):
self.assertEquals(response.status, 200)