-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull in code from requests-unixsocket and support urllib3 v2 (#29)
- Loading branch information
Showing
15 changed files
with
448 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Forked Code | ||
|
||
This code was forked from requests-unixsocket, with source taken from [v0.3.0 on PyPI](https://pypi.org/project/requests-unixsocket/0.3.0/#files). The original source code is on GitHub at [msabramo/requests-unixsocket](https://github.com/msabramo/requests-unixsocket), but v0.3.0 doesn't appear in the tags there. This code is licensed under Apache v2, so this is permitted use. | ||
|
||
I forked the code because it's incompatible with [urllib3 v2](https://urllib3.readthedocs.io/en/stable/v2-migration-guide.html), which requests moved to as of [v2.30.0](https://github.com/psf/requests/releases/tag/v2.30.0). We need to be on requests [>= v2.31.0](https://github.com/psf/requests/releases/tag/v2.31.0) due to [CVE-2023-32681](https://nvd.nist.gov/vuln/detail/CVE-2023-32681). The problem with requests-unixsocket is tracked in [issue #70](https://github.com/msabramo/requests-unixsocket/issues/70) and fixed in [PR #69](https://github.com/msabramo/requests-unixsocket/pull/69). However, as of this writing, the requests-unixsocket maintainer hasn't responded to either the issue or the PR. Given how small the code is, it seems safer and simpler to just pull it in rather than waiting for a new package to be released on PyPI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# pylint: disable=super-with-arguments,keyword-arg-before-vararg,invalid-name,redefined-outer-name,unused-argument: | ||
|
||
# This originated at msabramo/requests-unixsocket on GitHub; see README.md for details | ||
|
||
import sys | ||
|
||
import requests | ||
|
||
from .adapters import UnixAdapter | ||
|
||
DEFAULT_SCHEME = "http+unix://" | ||
|
||
|
||
class Session(requests.Session): | ||
def __init__(self, url_scheme=DEFAULT_SCHEME, *args, **kwargs): | ||
super(Session, self).__init__(*args, **kwargs) | ||
self.mount(url_scheme, UnixAdapter()) | ||
|
||
|
||
class monkeypatch: | ||
def __init__(self, url_scheme=DEFAULT_SCHEME): | ||
self.session = Session() | ||
requests = self._get_global_requests_module() | ||
|
||
# Methods to replace | ||
self.methods = ("request", "get", "head", "post", "patch", "put", "delete", "options") | ||
# Store the original methods | ||
self.orig_methods = dict((m, requests.__dict__[m]) for m in self.methods) | ||
# Monkey patch | ||
g = globals() | ||
for m in self.methods: | ||
requests.__dict__[m] = g[m] | ||
|
||
def _get_global_requests_module(self): | ||
return sys.modules["requests"] | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, *args): | ||
requests = self._get_global_requests_module() | ||
for m in self.methods: | ||
requests.__dict__[m] = self.orig_methods[m] | ||
|
||
|
||
# These are the same methods defined for the global requests object | ||
def request(method, url, **kwargs): | ||
session = Session() | ||
return session.request(method=method, url=url, **kwargs) | ||
|
||
|
||
def get(url, **kwargs): | ||
kwargs.setdefault("allow_redirects", True) | ||
return request("get", url, **kwargs) | ||
|
||
|
||
def head(url, **kwargs): | ||
kwargs.setdefault("allow_redirects", False) | ||
return request("head", url, **kwargs) | ||
|
||
|
||
def post(url, data=None, json=None, **kwargs): | ||
return request("post", url, data=data, json=json, **kwargs) | ||
|
||
|
||
def patch(url, data=None, **kwargs): | ||
return request("patch", url, data=data, **kwargs) | ||
|
||
|
||
def put(url, data=None, **kwargs): | ||
return request("put", url, data=data, **kwargs) | ||
|
||
|
||
def delete(url, **kwargs): | ||
return request("delete", url, **kwargs) | ||
|
||
|
||
def options(url, **kwargs): | ||
kwargs.setdefault("allow_redirects", True) | ||
return request("options", url, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# pylint: disable=super-with-arguments,keyword-arg-before-vararg,ungrouped-imports: | ||
|
||
# This originated at msabramo/requests-unixsocket on GitHub; see README.md for details | ||
|
||
import socket | ||
|
||
import urllib3 | ||
from requests.adapters import HTTPAdapter | ||
from requests.compat import unquote, urlparse | ||
|
||
|
||
# The following was adapted from some code from docker-py | ||
# https://github.com/docker/docker-py/blob/master/docker/transport/unixconn.py | ||
class UnixHTTPConnection(urllib3.connection.HTTPConnection): | ||
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 | ||
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) | ||
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 | ||
|
||
def _new_conn(self): | ||
return UnixHTTPConnection(self.socket_path, self.timeout) | ||
|
||
|
||
class UnixAdapter(HTTPAdapter): | ||
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()) | ||
|
||
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 | ||
|
||
pool = UnixHTTPConnectionPool(url, self.timeout) | ||
self.pools[url] = pool | ||
|
||
return pool | ||
|
||
def request_url(self, request, proxies): | ||
return request.path_url | ||
|
||
def close(self): | ||
self.pools.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Forked Code | ||
|
||
This code was forked from requests-unixsocket, with source taken from [v0.3.0 on PyPI](https://pypi.org/project/requests-unixsocket/0.3.0/#files). The original source code is on GitHub at [msabramo/requests-unixsocket](https://github.com/msabramo/requests-unixsocket), but v0.3.0 doesn't appear in the tags there. This code is licensed under Apache v2, so this is permitted use. | ||
|
||
I forked the code because it's incompatible with [urllib3 v2](https://urllib3.readthedocs.io/en/stable/v2-migration-guide.html), which requests moved to as of [v2.30.0](https://github.com/psf/requests/releases/tag/v2.30.0). We need to be on requests [>= v2.31.0](https://github.com/psf/requests/releases/tag/v2.31.0) due to [CVE-2023-32681](https://nvd.nist.gov/vuln/detail/CVE-2023-32681). The problem with requests-unixsocket is tracked in [issue #70](https://github.com/msabramo/requests-unixsocket/issues/70) and fixed in [PR #69](https://github.com/msabramo/requests-unixsocket/pull/69). However, as of this writing, the requests-unixsocket maintainer hasn't responded to either the issue or the PR. Given how small the code is, it seems safer and simpler to just pull it in rather than waiting for a new package to be released on PyPI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__all__ = [] # type: ignore |
Oops, something went wrong.