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
)
| 260 | |
| 261 | |
| 262 | def _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 | |
| 285 | class _TunnelingAgent(Agent): |
no test coverage detected