File requests230.patch of Package python-requests-unixsocket.40411
From 5a614f60e7b3639758a6b77691b4e0c0d6827e94 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Roukala=20=28n=C3=A9=20Peres=29?=
<martin.roukala@mupuf.org>
Date: Fri, 5 May 2023 09:23:41 +0300
Subject: [PATCH] Inherit HTTPConnection through urllib3.connection, not
httplib
By inheriting from `urllib3.connection.HTTPConnection` (that inherits
from `httplib.HTTPConnection` itself), we can adapt to the internal
changes in urllib3 2.0 that added a `request()` method that is
incompatible with httplib.HTTPConnection.request.
This fixes the incompatibility between urllib3 2.0 and requests 1.26+,
which was the first version that stopped vendoring urllib3.
Reference: https://github.com/docker/docker-py/issues/3113#issuecomment-1531570788
---
---
requests_unixsocket/adapters.py | 126 ++++++++----------
requests_unixsocket/tests/test_requests_unixsocket.py | 5
2 files changed, 62 insertions(+), 69 deletions(-)
Index: requests-unixsocket-0.3.0/requests_unixsocket/adapters.py
===================================================================
--- requests-unixsocket-0.3.0.orig/requests_unixsocket/adapters.py 2021-12-24 00:26:37.000000000 +0100
+++ requests-unixsocket-0.3.0/requests_unixsocket/adapters.py 2025-08-01 19:47:08.062483582 +0200
@@ -1,88 +1,84 @@
import socket
+from urllib.parse import unquote, urlparse
-from requests.adapters import HTTPAdapter
-from requests.compat import urlparse, unquote
+import requests
+import urllib3
-try:
- import http.client as httplib
-except ImportError:
- import httplib
-
-try:
- from requests.packages import urllib3
-except ImportError:
- import urllib3
-
-
-# The following was adapted from some code from docker-py
-# https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py
-class UnixHTTPConnection(httplib.HTTPConnection, object):
-
- def __init__(self, unix_socket_url, timeout=60):
- """Create an HTTP connection to a unix domain socket
-
- :param unix_socket_url: A URL with a scheme of 'http+unix' and the
- netloc is a percent-encoded path to a unix domain socket. E.g.:
- 'http+unix://%2Ftmp%2Fprofilesvc.sock/status/pid'
- """
- super(UnixHTTPConnection, self).__init__('localhost', timeout=timeout)
- self.unix_socket_url = unix_socket_url
+
+class UnixHTTPConnection(urllib3.connection.HTTPConnection):
+ """
+ A connection class for a unix domain socket.
+ """
+ def __init__(self, socket_path, timeout=60):
+ super().__init__("localhost", timeout=timeout)
+ self.socket_path = socket_path
self.timeout = timeout
self.sock = None
- def __del__(self): # base class does not have d'tor
- if self.sock:
- self.sock.close()
-
def connect(self):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock.settimeout(self.timeout)
- socket_path = unquote(urlparse(self.unix_socket_url).netloc)
- sock.connect(socket_path)
+ # The path is percent-encoded from the URL's "netloc"
+ sock.connect(unquote(self.socket_path))
self.sock = sock
-
-class UnixHTTPConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
-
- def __init__(self, socket_path, timeout=60):
- super(UnixHTTPConnectionPool, self).__init__(
- 'localhost', timeout=timeout)
- self.socket_path = socket_path
- self.timeout = timeout
+class UnixConnectionPool(urllib3.connectionpool.HTTPConnectionPool):
+ """
+ A connection pool for a unix domain socket.
+ """
+ def __init__(self, host, port=None, **kwargs):
+ super().__init__(host, port=port, **kwargs)
+ # The 'host' parameter from the PoolManager is the percent-encoded
+ # socket path.
+ self.socket_path = host
def _new_conn(self):
+ # Pass the raw socket path and the pool's timeout to the connection object.
return UnixHTTPConnection(self.socket_path, self.timeout)
-class UnixAdapter(HTTPAdapter):
-
+class UnixPoolManager(urllib3.poolmanager.PoolManager):
+ """
+ Custom PoolManager that handles unix sockets.
+ """
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.pool_classes_by_scheme['http+unix'] = UnixConnectionPool
+ self.key_fn_by_scheme['http+unix'] = self.key_fn_by_scheme['http']
+
+class UnixAdapter(requests.adapters.HTTPAdapter):
+ """
+ A requests transport adapter that handles connecting to a unix socket.
+ """
def __init__(self, timeout=60, pool_connections=25, *args, **kwargs):
- super(UnixAdapter, self).__init__(*args, **kwargs)
- self.timeout = timeout
- self.pools = urllib3._collections.RecentlyUsedContainer(
- pool_connections, dispose_func=lambda p: p.close()
+ super().__init__(*args, **kwargs)
+ self.poolmanager = UnixPoolManager(
+ timeout=timeout,
+ maxsize=pool_connections
)
- def get_connection(self, url, proxies=None):
- proxies = proxies or {}
- proxy = proxies.get(urlparse(url.lower()).scheme)
-
- if proxy:
- raise ValueError('%s does not support specifying proxies'
- % self.__class__.__name__)
-
- with self.pools.lock:
- pool = self.pools.get(url)
- if pool:
- return pool
+ def send(self, request, stream=False, timeout=None, verify=True, cert=None, proxies=None):
+ """
+ Sends the request and explicitly forbids proxies.
+ """
+ if proxies:
+ raise ValueError("Proxies are not supported for unix sockets")
- pool = UnixHTTPConnectionPool(url, self.timeout)
- self.pools[url] = pool
+ return super().send(request, stream=stream, timeout=timeout,
+ verify=verify, cert=cert, proxies=proxies)
- return pool
+ def get_connection_with_tls_context(self, request, verify, proxies=None, cert=None):
+ """Fix for requests 2.32.2+"""
+ return self.get_connection(request.url, proxies)
+
+ def _get_connection(self, request, *args, proxies=None, **kwargs):
+ """Fix for requests 2.32.0+"""
+ return self.get_connection(request.url, proxies)
- def request_url(self, request, proxies):
- return request.path_url
+ def get_connection(self, url, proxies=None):
+ # The proxy check is now handled in send(), but we keep it here
+ # for safety in case get_connection is called directly.
+ if proxies:
+ raise ValueError('Proxies aren\'t supported for unix sockets')
- def close(self):
- self.pools.clear()
+ return self.poolmanager.connection_from_url(url)
Index: requests-unixsocket-0.3.0/requests_unixsocket/tests/test_requests_unixsocket.py
===================================================================
--- requests-unixsocket-0.3.0.orig/requests_unixsocket/tests/test_requests_unixsocket.py 2019-08-16 00:50:29.000000000 +0200
+++ requests-unixsocket-0.3.0/requests_unixsocket/tests/test_requests_unixsocket.py 2025-08-01 19:53:19.920254652 +0200
@@ -1,6 +1,3 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
"""Tests for requests_unixsocket"""
import logging
@@ -86,7 +83,7 @@
getattr(session, method)(
'http+unix://socket_does_not_exist/path/to/page',
proxies={"http+unix": "http://10.10.1.10:1080"})
- assert ('UnixAdapter does not support specifying proxies'
+ assert ('Proxies are not supported for unix sockets'
in str(excinfo.value))