Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add response proxy headers to ClientResponse #7152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

galaxyfeeder
Copy link
Contributor

@galaxyfeeder galaxyfeeder commented Dec 30, 2022

What do these changes do?

Add to the ClientResponse the headers and raw headers of the underlying CONNECT call in a proxied HTTPS request.

The headers and raw headers from the response are added to the ResponseHandler and later when processing the request are added to the response.

Are there changes in behavior for the user?

No.

Related issue number

Closes #6078

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt
    • The format is <Name> <Surname>.
    • Please keep alphabetical order, the file is sorted by names.
  • Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> for example (588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the pr
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: "Fix issue with non-ascii contents in doctest text files."

@codecov
Copy link

codecov bot commented Dec 30, 2022

Codecov Report

Merging #7152 (6b7cdf4) into master (283861d) will decrease coverage by 0.03%.
The diff coverage is 76.08%.

@@            Coverage Diff             @@
##           master    #7152      +/-   ##
==========================================
- Coverage   97.37%   97.34%   -0.04%     
==========================================
  Files         106      106              
  Lines       31093    31138      +45     
  Branches     3875     3880       +5     
==========================================
+ Hits        30278    30312      +34     
- Misses        613      624      +11     
  Partials      202      202              
Flag Coverage Δ
CI-GHA 97.25% <76.08%> (-0.04%) ⬇️
OS-Linux 96.90% <76.08%> (-0.04%) ⬇️
OS-Windows 95.32% <76.08%> (-0.03%) ⬇️
OS-macOS 96.47% <76.08%> (-0.04%) ⬇️
Py-3.10.9 96.99% <76.08%> (-0.04%) ⬇️
Py-3.11.0 95.56% <76.08%> (-0.03%) ⬇️
Py-3.7.15 96.72% <76.08%> (-0.04%) ⬇️
Py-3.7.9 95.20% <76.08%> (-0.03%) ⬇️
Py-3.8.10 95.10% <76.08%> (-0.03%) ⬇️
Py-3.8.15 96.62% <76.08%> (-0.04%) ⬇️
Py-3.9.13 95.09% <76.08%> (-0.04%) ⬇️
Py-3.9.16 96.63% <76.08%> (-0.05%) ⬇️
Py-pypy7.3.10 94.13% <65.21%> (-0.05%) ⬇️
VM-macos 96.47% <76.08%> (-0.04%) ⬇️
VM-ubuntu 96.90% <76.08%> (-0.04%) ⬇️
VM-windows 95.32% <76.08%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tests/test_proxy_functional.py 63.90% <25.00%> (-1.00%) ⬇️
aiohttp/client_reqrep.py 97.44% <81.81%> (-0.28%) ⬇️
aiohttp/client_proto.py 96.96% <100.00%> (+0.25%) ⬆️
aiohttp/connector.py 94.64% <100.00%> (+0.01%) ⬆️
tests/test_client_proto.py 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Dec 30, 2022
@galaxyfeeder
Copy link
Contributor Author

I'm unsure about:

  • when this will be able to be released?
  • better test the solution with the current tests for the proxy

@galaxyfeeder galaxyfeeder marked this pull request as ready for review December 30, 2022 13:07
@galaxyfeeder galaxyfeeder marked this pull request as draft December 30, 2022 16:43
@galaxyfeeder galaxyfeeder force-pushed the response_proxy_headers branch 2 times, most recently from a352299 to cc56d61 Compare December 30, 2022 16:54
@galaxyfeeder galaxyfeeder marked this pull request as ready for review December 30, 2022 17:56
@DavidRomanovizc
Copy link
Contributor

Hello, @galaxyfeeder this problem is still relevant?

proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
url = "https://www.google.com.ua/search?q=aiohttp proxy"

resp = await sess.get(url, proxy=proxy.url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to use the async with context manager because it ensures that resources will be properly closed after the async with block completes, even if an exception occurs. The code will also become more readable, because it does not require explicit invocation of closing methods (close(), release(), etc.).

Here's what the test implementation might look like:

async def test_proxy_https_proxy_headers(make_proxy_test_server: Any) -> None:
    async with aiohttp.ClientSession() as session:
        async with make_proxy_test_server() as proxy:
            proxy.return_value = {"status": 200, "body": b"1" * (2 ** 20)}
            url = "https://www.google.com.ua/search?q=aiohttp proxy"
            async with session.get(url, proxy=proxy.url) as resp:
                assert resp.proxy_headers == {"proxy-agent": "TestProxy/1.0"}
                assert resp.headers == {}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed (note you can make code suggestions with the first toolbar button, which can then just be comitted).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thank you, I'll take it note

@galaxyfeeder
Copy link
Contributor Author

Hi @DavidRomanovizc, it is still relevant for us. Right now we are currently running a fork which includes this changes. Reading the proxy headers coming from underlying CONNECT allows us to debug the connection with proxy provider, mainly it allows us to receive information about the connection done through the proxy (outgoing IP address and performance data).

@Dreamsorcerer
Copy link
Member

I'd like @webknjaz to glance over this first, as I believe he's more familiar with the proxy stuff. I only have some minor comments.

@@ -881,6 +882,21 @@ async def xtest_proxy_from_env_https_with_auth(
assert r2.headers["Proxy-Authorization"] == auth.encode()


@pytest.mark.xfail
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the xfail? Surely the test should be working?

proxy.return_value = {"status": 200, "body": b"1" * (2**20)}
url = "https://www.google.com.ua/search?q=aiohttp proxy"

resp = await sess.get(url, proxy=proxy.url)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed (note you can make code suggestions with the first toolbar button, which can then just be comitted).

Comment on lines +887 to +890
self._proxy_headers = protocol.proxy_headers if protocol is not None else None
self._raw_proxy_headers = (
protocol.raw_proxy_headers if protocol is not None else None
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self._proxy_headers = protocol.proxy_headers if protocol is not None else None
self._raw_proxy_headers = (
protocol.raw_proxy_headers if protocol is not None else None
)
if protocol is None:
self._proxy_headers = None
self._raw_proxy_headers = None
else:
self._proxy_headers = protocol.proxy_headers
self._raw_proxy_headers = protocol.raw_proxy_headers

@galaxyfeeder
Copy link
Contributor Author

@Dreamsorcerer I've missed your review in my notifications. I will review your comments as soon as I have some spare time and I will sync with master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided There is a change note present in this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose CONNECT response headers when using a proxy
4 participants