Skip to content

Behavior with 304/204 status code #344

Description

@S-Abhishek

Hey,

I see that for 304 / 204 status code responses - if we send a body as well - it gets sent to the client issuing the request, which based on the MDN docs and the RFC it uses as the reference shouldn't send the body.

Here is a request sent by the client.

$ curl http://localhost:5000/ -v
*   Trying 127.0.0.1:5000...
* Connected to localhost (127.0.0.1) port 5000 (#0)
> GET / HTTP/1.1
> Host: localhost:5000
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 304 Not Modified
< Date: Sat, 26 Apr 2025 07:08:28 GMT
< Content-Length: 11
< 
* Excess found: excess = 11 url = / (zero-length body)
* Connection #0 to host localhost left intact

And the hyper-express app:

const HyperExpress = require('../');
const webserver = new HyperExpress.Server();

webserver.get('/', (request, response) => {
    response.status(304).send('Hello World');
})

webserver.listen(5000)
    .then((socket) => console.log('Webserver started on port 5000'))
    .catch((error) => console.log('Failed to start webserver on port 5000', error));

I tried the same scenario on Express App and it works as intended.

$ curl http://localhost:5001/ -v
*   Trying 127.0.0.1:5001...
* Connected to localhost (127.0.0.1) port 5001 (#0)
> GET / HTTP/1.1
> Host: localhost:5001
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 304 No Content
< X-Powered-By: Express
< ETag: W/"b-Ck1VqNd45QIvq3AZd8XYQLvEhtA"
< Date: Sat, 26 Apr 2025 07:08:58 GMT
< Connection: keep-alive
< Keep-Alive: timeout=5
< 
* Connection #0 to host localhost left intact

For resolution, we could log a warning and prevent sending the response to the client (or raise an error - but this could be disruptive).

On cursory look at the code, I think we have to add logic here (utilising this call this._raw_response.endWithoutBody();) to handle this - I can raise a PR for this fix.

Let me know your thoughts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions