source: frontend/node_modules/proxy-from-env/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[9af201e]1# proxy-from-env
2
3![Build Status](https://github.com/Rob--W/proxy-from-env/actions/workflows/run-tests.yaml/badge.svg?branch=master)
4[![Coverage Status](https://coveralls.io/repos/github/Rob--W/proxy-from-env/badge.svg?branch=master)](https://coveralls.io/github/Rob--W/proxy-from-env?branch=master)
5
6`proxy-from-env` is a Node.js package that exports a function (`getProxyForUrl`)
7that takes an input URL (a string, an instance of
8[`URL`](https://nodejs.org/docs/latest/api/url.html#the-whatwg-url-api),
9or [`url.parse`](https://nodejs.org/docs/latest/api/url.html#url_url_parsing)'s
10return value) and returns the desired proxy URL (also a string) based on
11standard proxy environment variables. If no proxy is set, an empty string is
12returned.
13
14If your application makes important (security) decisions based on the URL, be
15consistent in the mechanism to parse and validate URLs, as differences in URL
16parsing behavior can affect the outcome of proxy resolution.
17Strings are parsed with the standard `URL` API, as of `proxy-from-env@2.0.0`.
18Older versions relied on the (now deprecated) `url.parse` method instead.
19
20Invalid values in environment variables are not handled by the library
21([#41](https://github.com/Rob--W/proxy-from-env/issues/41)).
22
23It is your responsibility to actually proxy the request using the given URL.
24
25Installation:
26
27```sh
28npm install proxy-from-env
29```
30
31## Example
32This example shows how the data for a URL can be fetched via the
33[`http` module](https://nodejs.org/api/http.html), in a proxy-aware way.
34
35warning: this simple example works for http requests only. To support https,
36you must establish a proxy tunnel via the
37[http `connect` method](https://developer.mozilla.org/en-us/docs/web/http/reference/methods/connect).
38
39```javascript
40import http from 'node:test';
41import { getProxyForUrl } from 'proxy-from-env';
42// ^ or: var getProxyForUrl = require('proxy-from-env').getProxyForUrl;
43
44var some_url = 'http://example.com/something';
45
46// // Example, if there is a proxy server at 10.0.0.1:1234, then setting the
47// // http_proxy environment variable causes the request to go through a proxy.
48// process.env.http_proxy = 'http://10.0.0.1:1234';
49//
50// // But if the host to be proxied is listed in NO_PROXY, then the request is
51// // not proxied (but a direct request is made).
52// process.env.no_proxy = 'example.com';
53
54var proxy_url = getProxyForUrl(some_url); // <-- Our magic.
55if (proxy_url) {
56 // Should be proxied through proxy_url.
57 var parsed_some_url = new URL(some_url);
58 var parsed_proxy_url = new URL(proxy_url);
59 // A HTTP proxy is quite simple. It is similar to a normal request, except the
60 // path is an absolute URL, and the proxied URL's host is put in the header
61 // instead of the server's actual host.
62 httpOptions = {
63 protocol: parsed_proxy_url.protocol,
64 hostname: parsed_proxy_url.hostname,
65 port: parsed_proxy_url.port,
66 path: parsed_some_url.href,
67 headers: {
68 Host: parsed_some_url.host, // = host name + optional port.
69 },
70 };
71} else {
72 // Direct request.
73 httpOptions = some_url;
74}
75http.get(httpOptions, function(res) {
76 var responses = [];
77 res.on('data', function(chunk) { responses.push(chunk); });
78 res.on('end', function() { console.log(responses.join('')); });
79});
80```
81
82### Full proxy support
83The simple example above works for http requests only. To support https, you
84must establish a proxy tunnel via the
85[http `connect` method](https://developer.mozilla.org/en-us/docs/web/http/reference/methods/connect).
86
87An example of that is shown in the
88[`https-proxy-agent` npm package](https://www.npmjs.com/package/https-proxy-agent).
89The [`proxy-agent` npm package](https://www.npmjs.com/package/proxy-agent)
90combines `https-proxy-agent` and `proxy-from-env` to offer a `http.Agent` that
91supports proxies from environment variables.
92
93### Built-in proxy support
94Node.js is working on built-in support for proxy environment variables,
95currently behind `NODE_USE_ENV_PROXY=1` or `--use-env-proxy`. For details, see:
96
97- https://github.com/nodejs/node/issues/57872
98- https://nodejs.org/api/http.html#built-in-proxy-support
99
100
101## Environment variables
102The environment variables can be specified in all lowercase or all uppercase,
103with lowercase taking precedence over the uppercase variant. A variable that is
104not set has the same meaning as a variable that is set but has no value.
105
106### NO\_PROXY
107
108`NO_PROXY` is a list of host names (optionally with a port). If the input URL
109matches any of the entries in `NO_PROXY`, then the input URL should be fetched
110by a direct request (i.e. without a proxy).
111
112Matching follows the following rules:
113
114- `NO_PROXY=*` disables all proxies.
115- Space and commas may be used to separate the entries in the `NO_PROXY` list.
116- If `NO_PROXY` does not contain any entries, then proxies are never disabled.
117- If a port is added after the host name, then the ports must match. If the URL
118 does not have an explicit port name, the protocol's default port is used.
119- Generally, the proxy is only disabled if the host name is an exact match for
120 an entry in the `NO_PROXY` list. The only exceptions are entries that start
121 with a dot or with a wildcard; then the proxy is disabled if the host name
122 ends with the entry.
123
124See `test.js` for examples of what should match and what does not.
125
126### \*\_PROXY
127
128The environment variable used for the proxy depends on the protocol of the URL.
129For example, `https://example.com` uses the "https" protocol, and therefore the
130proxy to be used is `HTTPS_PROXY` (_NOT_ `HTTP_PROXY`, which is _only_ used for
131http:-URLs).
132
133The library is not limited to http(s), other schemes such as
134`FTP_PROXY` (ftp:),
135`WSS_PROXY` (wss:),
136`WS_PROXY` (ws:)
137are also supported.
138
139If present, `ALL_PROXY` is used as fallback if there is no other match.
140
141
142## External resources
143The exact way of parsing the environment variables is not codified in any
144standard. This library is designed to be compatible with formats as expected by
145existing software.
146The following resources were used to determine the desired behavior:
147
148- cURL:
149 https://curl.haxx.se/docs/manpage.html#ENVIRONMENT
150 https://github.com/curl/curl/blob/4af40b3646d3b09f68e419f7ca866ff395d1f897/lib/url.c#L4446-L4514
151 https://github.com/curl/curl/blob/4af40b3646d3b09f68e419f7ca866ff395d1f897/lib/url.c#L4608-L4638
152
153- wget:
154 https://www.gnu.org/software/wget/manual/wget.html#Proxies
155 http://git.savannah.gnu.org/cgit/wget.git/tree/src/init.c?id=636a5f9a1c508aa39e35a3a8e9e54520a284d93d#n383
156 http://git.savannah.gnu.org/cgit/wget.git/tree/src/retr.c?id=93c1517c4071c4288ba5a4b038e7634e4c6b5482#n1278
157
158- W3:
159 https://www.w3.org/Daemon/User/Proxies/ProxyClients.html
160
161- Python's urllib:
162 https://github.com/python/cpython/blob/936135bb97fe04223aa30ca6e98eac8f3ed6b349/Lib/urllib/request.py#L755-L782
163 https://github.com/python/cpython/blob/936135bb97fe04223aa30ca6e98eac8f3ed6b349/Lib/urllib/request.py#L2444-L2479
Note: See TracBrowser for help on using the repository browser.