Skip to content

Commit 970c891

Browse files
committed
[upd] upgrade httpx 0.18.2
adjust searx.network module to the new internal API see encode/httpx#1522
1 parent a925254 commit 970c891

2 files changed

Lines changed: 33 additions & 24 deletions

File tree

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ lxml==4.6.3
77
pygments==2.9.0
88
python-dateutil==2.8.2
99
pyyaml==5.4.1
10-
httpx[http2]==0.17.1
10+
httpx[http2]==0.18.2
1111
Brotli==1.0.9
1212
uvloop==0.16.0; python_version >= '3.7'
1313
uvloop==0.14.0; python_version < '3.7'
14-
httpx-socks[asyncio]==0.3.1
14+
httpx-socks[asyncio]==0.4.1
1515
langdetect==1.0.9
1616
setproctitle==1.2.2

searx/network/client.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,18 @@
3030
LOOP = None
3131
SSLCONTEXTS = {}
3232
TRANSPORT_KWARGS = {
33-
'backend': 'asyncio',
33+
# use anyio :
34+
# * https://github.com/encode/httpcore/issues/344
35+
# * https://github.com/encode/httpx/discussions/1511
36+
'backend': 'anyio',
3437
'trust_env': False,
3538
}
3639

3740

3841
# pylint: disable=protected-access
3942
async def close_connections_for_url(
40-
connection_pool: httpcore.AsyncConnectionPool,
41-
url: httpcore._utils.URL ):
43+
connection_pool: httpcore.AsyncConnectionPool, url: httpcore._utils.URL
44+
):
4245

4346
origin = httpcore._utils.url_to_origin(url)
4447
logger.debug('Drop connections for %r', origin)
@@ -63,8 +66,10 @@ def get_sslcontexts(proxy_url=None, cert=None, verify=True, trust_env=True, http
6366
class AsyncHTTPTransportNoHttp(httpcore.AsyncHTTPTransport):
6467
"""Block HTTP request"""
6568

66-
async def arequest(self, method, url, headers=None, stream=None, ext=None):
67-
raise httpcore.UnsupportedProtocol("HTTP protocol is disabled")
69+
async def handle_async_request(
70+
self, method, url, headers=None, stream=None, extensions=None
71+
):
72+
raise httpcore.UnsupportedProtocol('HTTP protocol is disabled')
6873

6974

7075
class AsyncProxyTransportFixed(AsyncProxyTransport):
@@ -79,59 +84,63 @@ class AsyncProxyTransportFixed(AsyncProxyTransport):
7984
* self._response_closed(self, connection)
8085
8186
Note: AsyncProxyTransport inherit from AsyncConnectionPool
82-
83-
Note: the API is going to change on httpx 0.18.0
84-
see https://github.com/encode/httpx/pull/1522
8587
"""
8688

87-
async def arequest(self, method, url, headers=None, stream=None, ext=None):
89+
async def handle_async_request(
90+
self, method, url, headers=None, stream=None, extensions=None
91+
):
8892
retry = 2
8993
while retry > 0:
9094
retry -= 1
9195
try:
92-
return await super().arequest(method, url, headers, stream, ext)
96+
return await super().handle_async_request(
97+
method, url, headers=headers, stream=stream, extensions=extensions
98+
)
9399
except (ProxyConnectionError, ProxyTimeoutError, ProxyError) as e:
94-
raise httpcore.ProxyError(e)
100+
raise httpx.ProxyError(e)
95101
except OSError as e:
96102
# socket.gaierror when DNS resolution fails
97-
raise httpcore.NetworkError(e)
103+
raise httpx.NetworkError(e)
98104
except httpcore.RemoteProtocolError as e:
99105
# in case of httpcore.RemoteProtocolError: Server disconnected
100106
await close_connections_for_url(self, url)
101107
logger.warning('httpcore.RemoteProtocolError: retry', exc_info=e)
102108
# retry
103109
except (httpcore.NetworkError, httpcore.ProtocolError) as e:
110+
# workaround before anyio was the default backend:
104111
# httpcore.WriteError on HTTP/2 connection leaves a new opened stream
105112
# then each new request creates a new stream and raise the same WriteError
113+
# most probably not necessary: https://github.com/encode/httpcore/issues/357
106114
await close_connections_for_url(self, url)
107115
raise e
108116

109117

110118
class AsyncHTTPTransportFixed(httpx.AsyncHTTPTransport):
111119
"""Fix httpx.AsyncHTTPTransport"""
112120

113-
async def arequest(self, method, url, headers=None, stream=None, ext=None):
121+
async def handle_async_request(
122+
self, method, url, headers=None, stream=None, extensions=None
123+
):
114124
retry = 2
115125
while retry > 0:
116126
retry -= 1
117127
try:
118-
return await super().arequest(method, url, headers, stream, ext)
128+
return await super().handle_async_request(
129+
method, url, headers=headers, stream=stream, extensions=extensions
130+
)
119131
except OSError as e:
120132
# socket.gaierror when DNS resolution fails
121133
raise httpcore.ConnectError(e)
122-
except httpcore.CloseError as e:
123-
# httpcore.CloseError: [Errno 104] Connection reset by peer
124-
# raised by _keepalive_sweep()
125-
# from https://github.com/encode/httpcore/blob/4b662b5c42378a61e54d673b4c949420102379f5/httpcore/_backends/asyncio.py#L198 # pylint: disable=line-too-long
126-
await close_connections_for_url(self._pool, url)
127-
logger.warning('httpcore.CloseError: retry', exc_info=e)
128-
# retry
129134
except httpcore.RemoteProtocolError as e:
130135
# in case of httpcore.RemoteProtocolError: Server disconnected
131136
await close_connections_for_url(self._pool, url)
132137
logger.warning('httpcore.RemoteProtocolError: retry', exc_info=e)
133138
# retry
134139
except (httpcore.ProtocolError, httpcore.NetworkError) as e:
140+
# workaround before anyio was the default backend:
141+
# httpcore.WriteError on HTTP/2 connection leaves a new opened stream
142+
# then each new request creates a new stream and raise the same WriteError
143+
# most probably not necessary: https://github.com/encode/httpcore/issues/357
135144
await close_connections_for_url(self._pool, url)
136145
raise e
137146

@@ -206,7 +215,7 @@ def new_client(
206215
if not enable_http and (pattern == 'http' or pattern.startswith('http://')):
207216
continue
208217
if (proxy_url.startswith('socks4://')
209-
or proxy_url.startswith('socks5://')
218+
or proxy_url.startswith('socks5://')
210219
or proxy_url.startswith('socks5h://')
211220
):
212221
mounts[pattern] = get_transport_for_socks_proxy(

0 commit comments

Comments
 (0)