socket hang up error when using with got and http-proxy
- Dominant language
- JavaScript
- Stars
- 192
- Forks
- 40
- PR merge metrics
- No merged PRs in 30d
Description
Node: v14.16.0
I have set up an http proxy using the [`http-proxy` module](https://www.npmjs.com/package/http-proxy) and verified it is working using curl.
I am now trying to use `hpagent` with `got` to send a request via the proxy. However I am getting a `socket hang up` error back from `got`.
My best guess is _something_ to do with how `hpagent` is creating the connection - hence why I'm raising the issue here. I hope someone can either point out the mistake I have made, or some suggestions on how to track this down further.
I've added a bunch of debug to http-proxy, hpagent and got, but everything looks 'normal'. On the proxy, I do see a socket get created, but the socket gets closed before it reaches any of the proxy code. As far as I can tell, that's happening within the core node.js code - somewhat harder to debug if you're not set up to do so.
To reproduce, the following code creates an HTTP server on port 9000 and the proxy on 8000.
```
var http = require('http'), httpProxy = require('http-proxy');
httpProxy.createProxyServer({target:'http://localhost:9000'}).listen(8000);
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.write('request successfully proxied!' + '\n' + JSON.stringify(req.headers, true, 2));
res.end();
}).listen(9000);
```
The client code is:
```
const got = require("got");
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');
let options = {
retry: 0,
agent: {
http: new HttpProxyAgent({proxy:"http://localhost:8000" })
}
}
got("http://localhost:9000", options).then(res => { console.log(res.body)}).catch(err => { console.log(err.toString())})
```
As you can see, it's a fairly default configuration.
With the proxy code running, you can verify that part is working using curl:
```
[~]$ export http_proxy=http://localhost:8000
[~]$ curl http://localhost:9000
request successfully proxied!
{
"proxy-connection": "Keep-Alive",
"accept": "*/*",
"user-agent": "curl/7.64.1",
"host": "localhost:9000",
"connection": "close"
}%
```
But running the `got` code which uses `hpagent`, you get:
```
[~]$ node client.js
RequestError: socket hang up
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start with the provided client.js reproduction and the proxy/server setup on ports 8000 and 9000. Run the curl and got/hpagent requests, then inspect the connection behavior around the socket hang up and compare the agent configuration with got's documented request path. Done means identifying the cause and providing a verified fix or clear usage guidance.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 32/100