aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/web/client.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2025-06-22 18:50:56 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2025-06-22 19:04:42 +0300
commitc7cbc6d480c5488ff6e921c709680fd2c1340a10 (patch)
tree10843f44b67c0fb5717ad555556064095f701d8c /contrib/python/Twisted/py3/twisted/web/client.py
parent26d391cdb94d2ce5efc8d0cc5cea7607dc363c0b (diff)
downloadydb-c7cbc6d480c5488ff6e921c709680fd2c1340a10.tar.gz
Intermediate changes
commit_hash:28750b74281710ec1ab5bdc2403c8ab24bdd164b
Diffstat (limited to 'contrib/python/Twisted/py3/twisted/web/client.py')
-rw-r--r--contrib/python/Twisted/py3/twisted/web/client.py60
1 files changed, 42 insertions, 18 deletions
diff --git a/contrib/python/Twisted/py3/twisted/web/client.py b/contrib/python/Twisted/py3/twisted/web/client.py
index cfd945d5d40..d3cd11fb845 100644
--- a/contrib/python/Twisted/py3/twisted/web/client.py
+++ b/contrib/python/Twisted/py3/twisted/web/client.py
@@ -15,7 +15,7 @@ import zlib
from dataclasses import dataclass
from functools import wraps
from http.cookiejar import CookieJar
-from typing import TYPE_CHECKING, Iterable, Optional
+from typing import TYPE_CHECKING, Hashable, Iterable, Optional
from urllib.parse import urldefrag, urljoin, urlunparse as _urlunparse
from zope.interface import implementer
@@ -26,7 +26,13 @@ from twisted.internet import defer, protocol, task
from twisted.internet.abstract import isIPv6Address
from twisted.internet.defer import Deferred
from twisted.internet.endpoints import HostnameEndpoint, wrapClientTLS
-from twisted.internet.interfaces import IOpenSSLContextFactory, IProtocol
+from twisted.internet.interfaces import (
+ IAddress,
+ IOpenSSLContextFactory,
+ IProtocol,
+ IReactorTime,
+ IStreamClientEndpoint,
+)
from twisted.logger import Logger
from twisted.python.compat import nativeString, networkString
from twisted.python.components import proxyForInterface
@@ -137,7 +143,7 @@ class URI:
scheme, netloc, path, params, query, fragment = http.urlparse(uri)
if defaultPort is None:
- if scheme == b"https":
+ if scheme in {b"https", b"wss"}:
defaultPort = 443
else:
defaultPort = 80
@@ -658,7 +664,7 @@ class _HTTP11ClientFactory(protocol.Factory):
self._quiescentCallback, self._metadata
)
- def buildProtocol(self, addr):
+ def buildProtocol(self, addr: IAddress) -> HTTP11ClientProtocol:
return HTTP11ClientProtocol(self._quiescentCallback)
@@ -777,7 +783,9 @@ class HTTPConnectionPool:
self._connections = {}
self._timeouts = {}
- def getConnection(self, key, endpoint):
+ def getConnection(
+ self, key: Hashable, endpoint: IStreamClientEndpoint
+ ) -> Deferred[HTTP11ClientProtocol]:
"""
Supply a connection, newly created or retrieved from the pool, to be
used for one HTTP request.
@@ -815,7 +823,9 @@ class HTTPConnectionPool:
return self._newConnection(key, endpoint)
- def _newConnection(self, key, endpoint):
+ def _newConnection(
+ self, key: Hashable, endpoint: IStreamClientEndpoint
+ ) -> Deferred[HTTP11ClientProtocol]:
"""
Create a new connection.
@@ -826,7 +836,10 @@ class HTTPConnectionPool:
self._putConnection(key, protocol)
factory = self._factory(quiescentCallback, repr(endpoint))
- return endpoint.connect(factory)
+ result: Deferred[HTTP11ClientProtocol] = endpoint.connect(
+ factory
+ ) # type:ignore[assignment]
+ return result
def _removeConnection(self, key, connection):
"""
@@ -892,7 +905,7 @@ class _AgentBase:
@ivar _pool: The L{HTTPConnectionPool} used to manage HTTP connections.
"""
- def __init__(self, reactor, pool):
+ def __init__(self, reactor: IReactorTime, pool: HTTPConnectionPool | None) -> None:
if pool is None:
pool = HTTPConnectionPool(reactor, False)
self._reactor = reactor
@@ -910,8 +923,15 @@ class _AgentBase:
return b"%b:%d" % (host, port)
def _requestWithEndpoint(
- self, key, endpoint, method, parsedURI, headers, bodyProducer, requestPath
- ):
+ self,
+ key: tuple[bytes, bytes, int],
+ endpoint: IStreamClientEndpoint,
+ method: bytes,
+ parsedURI: URI,
+ headers: Headers | None,
+ bodyProducer: IBodyProducer | None,
+ requestPath: bytes,
+ ) -> Deferred[IResponse]:
"""
Issue a new request, given the endpoint and the path sent as part of
the request.
@@ -935,7 +955,7 @@ class _AgentBase:
d = self._pool.getConnection(key, endpoint)
- def cbConnected(proto):
+ def cbConnected(proto: HTTP11ClientProtocol) -> Deferred[IResponse]:
return proto.request(
Request._construct(
method,
@@ -947,8 +967,7 @@ class _AgentBase:
)
)
- d.addCallback(cbConnected)
- return d
+ return d.addCallback(cbConnected)
@implementer(IAgentEndpointFactory)
@@ -988,7 +1007,7 @@ class _StandardEndpointFactory:
self._connectTimeout = connectTimeout
self._bindAddress = bindAddress
- def endpointForURI(self, uri):
+ def endpointForURI(self, uri: URI) -> IStreamClientEndpoint:
"""
Connect directly over TCP for C{b'http'} scheme, and TLS for
C{b'https'}.
@@ -996,7 +1015,6 @@ class _StandardEndpointFactory:
@param uri: L{URI} to connect to.
@return: Endpoint to connect to.
- @rtype: L{IStreamClientEndpoint}
"""
kwargs = {}
if self._connectTimeout is not None:
@@ -1015,9 +1033,9 @@ class _StandardEndpointFactory:
)
endpoint = HostnameEndpoint(self._reactor, host, uri.port, **kwargs)
- if uri.scheme == b"http":
+ if uri.scheme in {b"http", b"ws"}:
return endpoint
- elif uri.scheme == b"https":
+ elif uri.scheme in {b"https", b"wss"}:
connectionCreator = self._policyForHTTPS.creatorForNetloc(
uri.host, uri.port
)
@@ -1149,7 +1167,13 @@ class Agent(_AgentBase):
"""
return self._endpointFactory.endpointForURI(uri)
- def request(self, method, uri, headers=None, bodyProducer=None):
+ def request(
+ self,
+ method: bytes,
+ uri: bytes,
+ headers: Optional[Headers] = None,
+ bodyProducer: Optional[IBodyProducer] = None,
+ ) -> Deferred[IResponse]:
"""
Issue a request to the server indicated by the given C{uri}.