Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-69152: add method get_proxy_response_headers to HTTPConnection class#104248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 20 commits intopython:mainfromnametkin:new
May 16, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
20 commits
Select commitHold shift + click to select a range
d0d9349
add method get_proxy_response_headers
nametkinMay 6, 2023
f39feb9
add method get_proxy_response_headers
nametkinMay 6, 2023
0be0d47
📜🤖 Added by blurb_it.
blurb-it[bot]May 8, 2023
cdff360
Delete trailing whitespaces
nametkinMay 8, 2023
7288cf6
update docs and news
nametkinMay 10, 2023
f49b56c
merge commit
nametkinMay 10, 2023
b46e44f
Merge branch 'python:main' into new
nametkinMay 10, 2023
2dfc326
delete Misc/NEWS.d/next/Library/2023-05-08-11-52-56.gh-issue-69152.tl…
nametkinMay 10, 2023
70ad871
Merge branch 'new' of https://github.com/OneMoreZanuda/cpython into new
nametkinMay 10, 2023
03a5887
Merge branch 'main' into new
nametkinMay 10, 2023
a7af79d
Merge branch 'main' into new
nametkinMay 10, 2023
f1410b5
Merge branch 'main' into new
nametkinMay 11, 2023
b49ec81
Merge branch 'main' into new
nametkinMay 11, 2023
c0f50b9
Merge branch 'main' into new
nametkinMay 11, 2023
fb77b60
Merge branch 'main' into new
nametkinMay 12, 2023
9023028
Merge branch 'main' into new
nametkinMay 13, 2023
63da7f3
Merge branch 'main' into new
nametkinMay 13, 2023
6101344
Merge branch 'main' into new
nametkinMay 15, 2023
5625bfa
Merge branch 'main' into new
nametkinMay 15, 2023
a21af19
reworded news and add ReST formatting.
gpsheadMay 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletionsDoc/library/http.client.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -394,6 +394,17 @@ HTTPConnection Objects
one will be automatically generated and transmitted if not provided in
the headers argument.


.. method:: HTTPConnection.get_proxy_response_headers()

Returns a dictionary with the headers of the response received from
the proxy server to the CONNECT request.

If the CONNECT request was not sent, the method returns an empty dictionary.

.. versionadded:: 3.12


.. method:: HTTPConnection.connect()

Connect to the server specified when the object was created. By default,
Expand Down
37 changes: 29 additions & 8 deletionsLib/http/client.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -221,8 +221,9 @@ def _read_headers(fp):
break
return headers

def parse_headers(fp, _class=HTTPMessage):
"""Parses only RFC2822 headers from a file pointer.
def _parse_header_lines(header_lines, _class=HTTPMessage):
"""
Parses only RFC2822 headers from header lines.

email Parser wants to see strings rather than bytes.
But a TextIOWrapper around self.rfile would buffer too many bytes
Expand All@@ -231,10 +232,15 @@ def parse_headers(fp, _class=HTTPMessage):
to parse.

"""
headers = _read_headers(fp)
hstring = b''.join(headers).decode('iso-8859-1')
hstring = b''.join(header_lines).decode('iso-8859-1')
return email.parser.Parser(_class=_class).parsestr(hstring)

def parse_headers(fp, _class=HTTPMessage):
"""Parses only RFC2822 headers from a file pointer."""

headers = _read_headers(fp)
return _parse_header_lines(headers, _class)


class HTTPResponse(io.BufferedIOBase):

Expand DownExpand Up@@ -858,7 +864,7 @@ def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
self._tunnel_host = None
self._tunnel_port = None
self._tunnel_headers = {}
self._proxy_response_headers = None
self._raw_proxy_headers = None

(self.host, self.port) = self._get_hostport(host, port)

Expand DownExpand Up@@ -945,11 +951,11 @@ def _tunnel(self):
try:
(version, code, message) = response._read_status()

self._proxy_response_headers =parse_headers(response.fp)
self._raw_proxy_headers =_read_headers(response.fp)

if self.debuglevel > 0:
forhdr, valin self._proxy_response_headers.items():
print("header:", hdr + ":", val)
forheaderin self._raw_proxy_headers:
print('header:', header.decode())

if code != http.HTTPStatus.OK:
self.close()
Expand All@@ -958,6 +964,21 @@ def _tunnel(self):
finally:
response.close()

def get_proxy_response_headers(self):
"""
Returns a dictionary with the headers of the response
received from the proxy server to the CONNECT request
sent to set the tunnel.

If the CONNECT request was not sent, the method returns
an empty dictionary.
"""
return (
_parse_header_lines(self._raw_proxy_headers)
if self._raw_proxy_headers is not None
else {}
)

def connect(self):
"""Connect to the host and port specified in __init__."""
sys.audit("http.client.connect", self, self.host, self.port)
Expand Down
2 changes: 1 addition & 1 deletionLib/test/test_httplib.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2401,7 +2401,7 @@ def test_proxy_response_headers(self):
self.conn.set_tunnel('destination.com')

self.conn.request('PUT', '/', '')
headers = self.conn._proxy_response_headers
headers = self.conn.get_proxy_response_headers()
self.assertIn(expected_header, headers.items())

def test_tunnel_leak(self):
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
Addedattribute '_proxy_response_headers' toHTTPConnection class. This
attribute containsthe headersof the proxy server response to the CONNECT
request.
Added:meth:`http.client.HTTPConnection.get_proxy_response_headers` that
provides access totheHTTPheaderson a proxy server response to the
``CONNECT``request.

[8]ページ先頭

©2009-2025 Movatter.jp