http_client_request 0.1.2.1__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: http_client_request
3
- Version: 0.1.2.1
3
+ Version: 0.1.3
4
4
  Summary: http.client request extension.
5
5
  License: MIT
6
6
  Keywords: http.client,request
@@ -4,7 +4,7 @@
4
4
  from __future__ import annotations
5
5
 
6
6
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
7
- __version__ = (0, 1, 2)
7
+ __version__ = (0, 1, 3)
8
8
  __all__ = [
9
9
  "CONNECTION_POOL", "HTTPConnection", "HTTPSConnection", "HTTPResponse",
10
10
  "ConnectionPool", "request",
@@ -15,14 +15,14 @@ from collections import defaultdict, deque, UserString
15
15
  from collections.abc import Buffer, Callable, Iterable, Mapping
16
16
  from http.client import (
17
17
  HTTPConnection as BaseHTTPConnection, HTTPSConnection as BaseHTTPSConnection,
18
- HTTPResponse as BaseHTTPResponse, ImproperConnectionState,
18
+ HTTPResponse as BaseHTTPResponse,
19
19
  )
20
20
  from http.cookiejar import CookieJar
21
21
  from http.cookies import BaseCookie
22
22
  from inspect import signature
23
23
  from os import PathLike
24
24
  from select import select
25
- from socket import socket
25
+ from socket import MSG_PEEK, MSG_DONTWAIT
26
26
  from types import EllipsisType
27
27
  from typing import cast, overload, Any, Final, Literal
28
28
  from urllib.error import HTTPError
@@ -64,9 +64,19 @@ def is_ipv6(host: str, /) -> bool:
64
64
  return False
65
65
 
66
66
 
67
- def sock_buf_readable(sock: socket, /) -> bool:
68
- rlist, *_ = select([sock], (), (), 0)
69
- return bool(rlist)
67
+ def ensure_available_connection(conn: BaseHTTPConnection, /):
68
+ if sock := conn.sock:
69
+ if getattr(sock, "_closed", True):
70
+ conn.close()
71
+ else:
72
+ try:
73
+ if select([sock], [], [], 0)[0]:
74
+ sock.recv(1, MSG_PEEK | MSG_DONTWAIT)
75
+ conn.close()
76
+ except ValueError:
77
+ conn.close()
78
+ except BlockingIOError:
79
+ pass
70
80
 
71
81
 
72
82
  try:
@@ -108,7 +118,7 @@ class HTTPResponse(BaseHTTPResponse):
108
118
  if pool and connection and not (
109
119
  200 <= self.status < 300 and
110
120
  (length := self.length) and
111
- length - self._pos - len(fp.peek()) - sock_bufsize(connection.sock) > 1024 * 1024 * 10
121
+ length - self._pos - len(fp.peek()) - sock_bufsize(connection.sock) > 1024 * 1024 * 10 # 10 MB
112
122
  ):
113
123
  try:
114
124
  self.read()
@@ -185,19 +195,6 @@ class HTTPConnectionMixin:
185
195
  def state(self: Any, /) -> str:
186
196
  return self._HTTPConnection__state
187
197
 
188
- def getresponse(self: Any, /) -> HTTPResponse:
189
- return cast(HTTPResponse, super().getresponse()) # type: ignore
190
-
191
- def putrequest(self: Any, /, *args, **kwds):
192
- excs: list[Exception] = []
193
- for _ in range(5):
194
- try:
195
- return super().putrequest(*args, **kwds) # type: ignore
196
- except (ConnectionResetError, BrokenPipeError, ImproperConnectionState) as e:
197
- excs.append(e)
198
- self.close()
199
- raise ExceptionGroup("too many retries", excs)
200
-
201
198
  def set_tunnel(self: Any, /, host=None, port=None, headers=None):
202
199
  has_sock = self.sock is not None
203
200
  if not host:
@@ -452,16 +449,14 @@ def request[T](
452
449
  connection.set_tunnel(*http_proxy)
453
450
  elif pool:
454
451
  connection.set_tunnel()
455
- try:
456
- connection.request(
457
- method,
458
- urlunsplit(urlp._replace(scheme="", netloc="")),
459
- body,
460
- headers_,
461
- )
462
- except BrokenPipeError:
463
- continue
464
- response = connection.getresponse()
452
+ ensure_available_connection(connection)
453
+ connection.request(
454
+ method,
455
+ urlunsplit(urlp._replace(scheme="", netloc="")),
456
+ body,
457
+ headers_,
458
+ )
459
+ response = cast(HTTPResponse, connection.getresponse())
465
460
  if pool and headers_.get("connection") == "keep-alive":
466
461
  setattr(response, "pool", pool)
467
462
  setattr(response, "connection", connection)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "http_client_request"
3
- version = "0.1.2.1"
3
+ version = "0.1.3"
4
4
  description = "http.client request extension."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"