Reject HTTP upgrade request

There’s this one part of Node.js’s HTTP module where you can allow the client to upgrade a connection: https://nodejs.org/api/http.html#http_event_upgrade

Is it possible to reject the upgrade request?

I tried manually writing an HTTP response and closing the socket, but that always results in a 502 response sent with Server: awselb/2.0.

I’m not clear on what you want here, exactly. It sounds like the request is being rejected already. socket.destroy() is the way I would do it.

1 Like

I want to send an HTTP response in response to the request. It’s okay if the connection has to close afterwards, but I want information to be transmitted in the rejection.

Here’s what I can see about the layers of HTTP proxying involved:

  1. The origin server, listening on $PORT in your container. You can program this to ignore a protocol upgrade and respond with HTTP.
  2. Some private, probably glitch-specific thing. Evidence suggests that this piece doesn’t support responding to an upgrade request with HTTP. Whatever it does, it makes the next layer unhappy. In non-upgrade HTTP requests, this thing adds x-powered-by: Express.
  3. awselb/2.0, which is fairly transparent. It only shows up when anything upstream does something wrong. But it’s unknown if it supports responding to an upgrade request with HTTP.
  4. The user agent. You can program this to accept HTTP responses.

Side note: another thing that happens is either (2) or (3) replaces custom status messages with the default ones (e.g., “Not Found” for 404). But that’s relatively minor.

I’m sorry that’s a problem for you, wh0. We’ll add this to the list of things we would like to look into, but it’s not going to be very high priority right now, so I wouldn’t expect the behavior to change soon.

Thanks, I’ll work around it.

  • custom header instead of status code+message
  • when app wants to reject, complete upgrade but close immediately
  • modify user agent to check header even on 101 Switching Protocols