comparison env/lib/python3.9/site-packages/requests_toolbelt/adapters/host_header_ssl.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 # -*- coding: utf-8 -*-
2 """
3 requests_toolbelt.adapters.host_header_ssl
4 ==========================================
5
6 This file contains an implementation of the HostHeaderSSLAdapter.
7 """
8
9 from requests.adapters import HTTPAdapter
10
11
12 class HostHeaderSSLAdapter(HTTPAdapter):
13 """
14 A HTTPS Adapter for Python Requests that sets the hostname for certificate
15 verification based on the Host header.
16
17 This allows requesting the IP address directly via HTTPS without getting
18 a "hostname doesn't match" exception.
19
20 Example usage:
21
22 >>> s.mount('https://', HostHeaderSSLAdapter())
23 >>> s.get("https://93.184.216.34", headers={"Host": "example.org"})
24
25 """
26
27 def send(self, request, **kwargs):
28 # HTTP headers are case-insensitive (RFC 7230)
29 host_header = None
30 for header in request.headers:
31 if header.lower() == "host":
32 host_header = request.headers[header]
33 break
34
35 connection_pool_kwargs = self.poolmanager.connection_pool_kw
36
37 if host_header:
38 connection_pool_kwargs["assert_hostname"] = host_header
39 elif "assert_hostname" in connection_pool_kwargs:
40 # an assert_hostname from a previous request may have been left
41 connection_pool_kwargs.pop("assert_hostname", None)
42
43 return super(HostHeaderSSLAdapter, self).send(request, **kwargs)