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

feat: add proxy-agent #1070

Merged
merged 11 commits into from
Oct 24, 2021
Merged

feat: add proxy-agent #1070

merged 11 commits into from
Oct 24, 2021

Conversation

RafaelGSS
Copy link
Member

@RafaelGSS RafaelGSS commented Oct 21, 2021

Address: #569.

There is a remaining point to be discussed. I've implemented it and tested it against other HTTP libraries (request, axios) and here are a quick HTTP response summary:

Undici

image

Axios

image

Request

image

You will notice that other HTTP libraries answer the HTTP request with Connection: close through the proxy. Nonetheless, undici keeps the connection open with keep-alive. I'm supposing that it's was designed by default, I'm sharing it just to align expectations.


TODO:

  • Add ProxyAgent test
  • Https test
  • Add ProxyAgent types test
  • Docs

@codecov-commenter
Copy link

codecov-commenter commented Oct 21, 2021

Codecov Report

Merging #1070 (f4e177a) into main (a821ba7) will increase coverage by 0.03%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1070      +/-   ##
==========================================
+ Coverage   94.93%   94.96%   +0.03%     
==========================================
  Files          38       39       +1     
  Lines        3709     3734      +25     
==========================================
+ Hits         3521     3546      +25     
  Misses        188      188              
Impacted Files Coverage Δ
lib/core/symbols.js 100.00% <ø> (ø)
index.js 100.00% <100.00%> (ø)
lib/proxy-agent.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a821ba7...f4e177a. Read the comment docs.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Could you add another test with a path as well?

Looks good!

@ronag
Copy link
Member

ronag commented Oct 21, 2021

docs?

@RafaelGSS
Copy link
Member Author

I've improved the test coverage and included API documentation in this new class. There is a remaining check to do before making it ready for review which is the usage with HTTPS. I'll do it and share feedback ASAP.

@@ -9,6 +9,8 @@ the `path` should be `path: 'http:https://upstream.server:port/hello?foo=bar'`.

If you proxy requires basic authentication, you can send it via the `proxy-authorization` header.

> In latest versions of `undici` you can use [`ProxyAgent`](docs/api/ProxyAgent.md) to avoid some settings.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would consider rewording this:

  • after the first release where this will be available, the version in which it is available will not be the "latest" anymore. When this code is released this comment is basically redundant because the ProxyAgent is available and that's all users need to know. This mention should simply go in the release notes. I would also move it to the top as it's probably going to be the preferred way to set it up
  • "avoid some settings" could be reworded as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you think that this entire document should be refactored to use ProxyAgent or a recommendation on top of the file:

Connecting through a proxy is possible by:

- Using [AgentProxy](docs/api/ProxyAgent.md).
- Configuring `Client` or `Pool` constructor.

should be enough?

@@ -0,0 +1,189 @@
'use strict'
Copy link
Contributor

Choose a reason for hiding this comment

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

in the agent implementation I see it's dealing with possibly multiple clients. is it something worth testing as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

If I'm not mistaken, when dealing with Proxy just one client is set.

Example:

setGlobalDispatcher(new ProxyAgent('http:https://localhost:8000/'))

async function main() {
  const res1 = await request('http:https://localhost:3000/undici')
  console.log('response received', res1.statusCode)

  const res2 = await request('http:https://localhost:4000/undici')
  console.log('response received', res2.statusCode)

}
main()

It will only set http:https://localhost:8000 as a client.

setting client http:https://localhost:8000/
response received 200
response received 201

@RafaelGSS

This comment has been minimized.

@RafaelGSS
Copy link
Member Author

Communication with origin HTTPS is now working. It wasn't working because I've missed the Host header and the servers were answering the as bad requests.

@RafaelGSS RafaelGSS marked this pull request as ready for review October 22, 2021 21:14
headers: {
...opts.headers,
host,
'User-Agent': 'undici'
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove this user-agent if it's not strictly needed. If it's strictly required by proxies, let's make sure it's user-configurable (right now is not if I read the code correctly).

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not required by proxies and it's totally user-configurable:

  const {
    statusCode,
    headers,
    trailers,
    body
    // send the request via the http:https://localhost:8000/ HTTP proxy
  } = await request('http:https://localhost:3000/undici', { headers: { 'User-Agent': 'Example/0.2.1' } })

Removed support to pre-defined UserAgent.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@@ -10,13 +10,14 @@ import MockClient = require('./types/mock-client')
import MockPool = require('./types/mock-pool')
import MockAgent = require('./types/mock-agent')
import mockErrors = require('./types/mock-errors')
import ProxyAgent from './types/proxy-agent'
Copy link
Contributor

@RA80533 RA80533 Feb 1, 2022

Choose a reason for hiding this comment

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

This line does not correspond with the actual import of ProxyAgent (index.js#L17).

@RA80533 RA80533 mentioned this pull request Feb 1, 2022
KhafraDev pushed a commit to KhafraDev/undici that referenced this pull request Jun 23, 2022
crysmags pushed a commit to crysmags/undici that referenced this pull request Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants