@@ -107,15 +107,8 @@ def __init__(self, reactor, host, port, proxyConf, contextFactory,
107
107
108
108
def requestTunnel (self , protocol ):
109
109
"""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 )
119
112
protocol .transport .write (tunnelReq )
120
113
self ._protocolDataReceived = protocol .dataReceived
121
114
protocol .dataReceived = self .processProxyResponse
@@ -149,6 +142,29 @@ def connect(self, protocolFactory):
149
142
return self ._tunnelReadyDeferred
150
143
151
144
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
+
152
168
class TunnelingAgent (Agent ):
153
169
"""An agent that uses a L{TunnelingTCP4ClientEndpoint} to make HTTPS
154
170
downloads. It may look strange that we have chosen to subclass Agent and not
0 commit comments