Skip to content

Proxied servers are not notified of aborted client requests #85

@Maiquu

Description

@Maiquu

Environment

node: 22.17.1
httpxy: 0.1.7

Reproduction

  1. Run following code where 2 servers are started. One acts as a proxy server for the other.
import { createServer } from "node:http";
import { setTimeout as delay } from 'node:timers/promises';
import { createProxyServer } from "httpxy";

const proxy = createProxyServer({});

const server = createServer(async (req, res) => {
  try {
    await proxy.web(req, res, {
      target: 'http://localhost:4000',
    });
  } catch (error) {
    console.error(error);
    res.statusCode = 500;
    res.end("Proxy error: " + error.toString());
  }
});

server.listen(3000, () => {
  console.log("Proxy is listening on http://localhost:3000");
});

const proxiedServer = createServer(async (req, res) => {
  req.on('close', () => {
    console.log('Request closed')
    if (req.aborted) {
      console.log('Request aborted by client')
    }
  })
  await delay(3000)

  res.writeHead(200, { 'Content-Type': 'text/plain' })
  res.end('Done')
})

proxiedServer.listen(4000, () => {
  console.log('Target is listening on http://localhost:4000')
})
  1. Execute curl http://localhost:3000 and abort request before its over
  2. Execute curl http://localhost:4000 and abort request before its over and observe differences

Describe the bug

When request is aborted by client, proxied server is not notified of this.

Additional context

I managed to fix this on my side via adding following to src/middleware/web-incoming.ts. Can open a PR.

@@ -114,6 +114,10 @@ const stream = defineProxyMiddleware(
       proxyReq.abort();
     });
 
+    res.on("close", function () {
+      proxyReq.abort();
+    })
+
     // handle errors in proxy and incoming request, just like for forward proxy
     const proxyError = createErrorHandler(proxyReq, options.target);
     req.on("error", proxyError);

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions