Skip to content

Commit cf535fe

Browse files
committed
Merge pull request scrapy#1827 from scrapy/proxy-auth-test
[MRG+1] Extract a function to build CONNECT request; add tests for it.
2 parents e8635cd + 94e28ad commit cf535fe

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

scrapy/core/downloader/handlers/http11.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,8 @@ def __init__(self, reactor, host, port, proxyConf, contextFactory,
107107

108108
def requestTunnel(self, protocol):
109109
"""Asks the proxy to open a tunnel."""
110-
tunnelReq = (
111-
b'CONNECT ' +
112-
to_bytes(self._tunneledHost, encoding='ascii') + b':' +
113-
to_bytes(str(self._tunneledPort)) +
114-
b' HTTP/1.1\r\n')
115-
if self._proxyAuthHeader:
116-
tunnelReq += \
117-
b'Proxy-Authorization: ' + self._proxyAuthHeader + b'\r\n'
118-
tunnelReq += b'\r\n'
110+
tunnelReq = tunnel_request_data(self._tunneledHost, self._tunneledPort,
111+
self._proxyAuthHeader)
119112
protocol.transport.write(tunnelReq)
120113
self._protocolDataReceived = protocol.dataReceived
121114
protocol.dataReceived = self.processProxyResponse
@@ -149,6 +142,29 @@ def connect(self, protocolFactory):
149142
return self._tunnelReadyDeferred
150143

151144

145+
def tunnel_request_data(host, port, proxy_auth_header=None):
146+
r"""
147+
Return binary content of a CONNECT request.
148+
149+
>>> from scrapy.utils.python import to_native_str as s
150+
>>> s(tunnel_request_data("example.com", 8080))
151+
'CONNECT example.com:8080 HTTP/1.1\r\n\r\n'
152+
>>> s(tunnel_request_data("example.com", 8080, b"123"))
153+
'CONNECT example.com:8080 HTTP/1.1\r\nProxy-Authorization: 123\r\n\r\n'
154+
>>> s(tunnel_request_data(b"example.com", "8090"))
155+
'CONNECT example.com:8090 HTTP/1.1\r\n\r\n'
156+
"""
157+
tunnel_req = (
158+
b'CONNECT ' +
159+
to_bytes(host, encoding='ascii') + b':' +
160+
to_bytes(str(port)) +
161+
b' HTTP/1.1\r\n')
162+
if proxy_auth_header:
163+
tunnel_req += b'Proxy-Authorization: ' + proxy_auth_header + b'\r\n'
164+
tunnel_req += b'\r\n'
165+
return tunnel_req
166+
167+
152168
class TunnelingAgent(Agent):
153169
"""An agent that uses a L{TunnelingTCP4ClientEndpoint} to make HTTPS
154170
downloads. It may look strange that we have chosen to subclass Agent and not

0 commit comments

Comments
 (0)