MCPcopy
hub / github.com/scrapy/scrapy / _tunnel_request_data

Function _tunnel_request_data

scrapy/core/downloader/handlers/http11.py:262–282  ·  view source on GitHub ↗

r""" Return binary content of a CONNECT request. >>> from scrapy.utils.python import to_unicode as s >>> s(_tunnel_request_data("example.com", 8080)) 'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\n\r\n' >>> s(_tunnel_request_data("example.com", 8080, b"123"))

(
    host: str, port: int, proxy_auth_header: bytes | None = None
)

Source from the content-addressed store, hash-verified

260
261
262def _tunnel_request_data(
263 host: str, port: int, proxy_auth_header: bytes | None = None
264) -> bytes:
265 r"""
266 Return binary content of a CONNECT request.
267
268 >>> from scrapy.utils.python import to_unicode as s
269 >>> s(_tunnel_request_data("example.com", 8080))
270 'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\n\r\n'
271 >>> s(_tunnel_request_data("example.com", 8080, b"123"))
272 'CONNECT example.com:8080 HTTP/1.1\r\nHost: example.com:8080\r\nProxy-Authorization: 123\r\n\r\n'
273 >>> s(_tunnel_request_data(b"example.com", "8090"))
274 'CONNECT example.com:8090 HTTP/1.1\r\nHost: example.com:8090\r\n\r\n'
275 """
276 host_value = to_bytes(host, encoding="ascii") + b":" + to_bytes(str(port))
277 tunnel_req = b"CONNECT " + host_value + b" HTTP/1.1\r\n"
278 tunnel_req += b"Host: " + host_value + b"\r\n"
279 if proxy_auth_header:
280 tunnel_req += b"Proxy-Authorization: " + proxy_auth_header + b"\r\n"
281 tunnel_req += b"\r\n"
282 return tunnel_req
283
284
285class _TunnelingAgent(Agent):

Callers 1

requestTunnelMethod · 0.85

Calls 1

to_bytesFunction · 0.90

Tested by

no test coverage detected