Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/requests_toolbelt/adapters/host_header_ssl.py: 7%

15 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1# -*- coding: utf-8 -*- 

2""" 

3requests_toolbelt.adapters.host_header_ssl 

4========================================== 

5 

6This file contains an implementation of the HostHeaderSSLAdapter. 

7""" 

8 

9from requests.adapters import HTTPAdapter 

10 

11 

12class 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)