Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http: throw error when attempting to write to response with no body #47656

Closed
wants to merge 3 commits into from

Conversation

mertcanaltin
Copy link
Member

@mertcanaltin mertcanaltin commented Apr 21, 2023

Previously, when the _hasBody property of the response object was false, the code simply ignored any data being written to the response. This behavior was changed to throw an error instead, which is more appropriate for responses that are not allowed to have a body

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Apr 21, 2023
@ronag
Copy link
Member

ronag commented Apr 21, 2023

This needs a test. After which, I think you will notice that this does not work since all implementations override this method.

@mertcanaltin
Copy link
Member Author

mertcanaltin commented Apr 21, 2023

This needs a test. After which, I think you will notice that this does not work since all implementations override this method.

Do I need to create a new test, I would appreciate it if you could inform me about it.

@ronag
Copy link
Member

ronag commented Apr 21, 2023

Yes, please create a new test.

@mertcanaltin
Copy link
Member Author

Yes, please create a new test.

I added a test

@ronag
Copy link
Member

ronag commented Apr 21, 2023

Yes, please create a new test.

I added a test

The test is failing.

Copy link
Member

@ronag ronag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should test the public api not the internal one.

@HinataKah0
Copy link
Contributor

HinataKah0 commented Apr 22, 2023

I think you can try looking into how the response body is sent, the API that devs use to write to the response body. It should be this API. From there, you can try to trace the code and understand how it works.

@HinataKah0
Copy link
Contributor

HinataKah0 commented Apr 23, 2023

Ah, actually I meant about where to start to understand about the necessary changes.

What I suggested is: because we want to prevent writing to the response body when the request method is HEAD, we can start by looking at the write API itself.

You can search for _implicitHeader in the codebase and find that OutgoingMessage's _implicitHeader is overridden by ServerResponse. So I think writing changes in _implicitHeader is not going to work.

About writing test, I think you can refer to existing tests, you can search in the codebase with the keyword "test-http" and there are plenty of them.

And you can try running the tests in local first, see this.

I hope that helps 😄

server.listen(0, () => {
const port = server.address().port;
const options = {
method: 'GET',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to verify that HEAD can't write. So I am not sure why we are only testing GET here.

@mertcanaltin mertcanaltin requested a review from ronag April 24, 2023 07:17
lib/_http_outgoing.js Outdated Show resolved Hide resolved
@mertcanaltin mertcanaltin changed the title http: update outgoingMessage.prototype._implicitHeader method http: throw error when attempting to write to response with no body Apr 25, 2023
@ronag ronag added the semver-major PRs that contain breaking changes and should be released in the next major version. label Apr 25, 2023
@mertcanaltin mertcanaltin requested a review from ronag April 27, 2023 12:14
@mertcanaltin
Copy link
Member Author

@ronag
does defining the error as a class make it flexible for further steps?

class HttpBodyNotAllowedError extends Error {
  constructor(message) {
    super(message);
    this.name = 'HttpBodyNotAllowedError';
    this.code = 'ERR_HTTP_BODY_NOT_ALLOWED';
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants