Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

Handle OPTIONS content-length #83

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Maxime-J
Copy link

Fixes #82, with a new test reflecting that.

Note that the method to remove the content-length header is slightly changed:

  • hasOwnProperty is probably better
  • delete is more straightforward than using filterHeaders().
    By the way, that function was only used here but I leaved it in lib/util.js, in case you would like to keep it.

if (headers['content-length']) {
headers = filterHeaders(headers, 'content-length')
if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') {
if (headers.hasOwnProperty('content-length')) {
Copy link
Contributor

@Uzlopak Uzlopak Mar 13, 2023

Choose a reason for hiding this comment

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

No. If you want to use hasOwnProperty, than do it with

Object.prototype.hasOwnProperty.call(headers, 'content-length')

But imho the better way is

'content-length' in headers

Copy link
Author

Choose a reason for hiding this comment

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

Right, safer and better to use in

headers = filterHeaders(headers, 'content-length')
if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') {
if (headers.hasOwnProperty('content-length')) {
delete headers['content-length']
Copy link
Contributor

Choose a reason for hiding this comment

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

Using filterHeaders has maybe some deeper reason.

Copy link
Author

Choose a reason for hiding this comment

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

Here's the context when it was introduced #38 (for the same undici reason I raised about OPTIONS)

-We could think it was made that way to prevent object mutation but headers are taken from a spread, so no risks of modifying elsewhere, and headers object is already mutated

headers['x-forwarded-host'] = headers.host
headers.host = url.hostname

-filterHeaders ensures a case insensitive removal, but first, headers are supposed to already be lowercase and anyway, a case-sensitive comparison is made before calling it.

Are you ok with delete then ?
But I can obviously leave it as it was, the priority being to fix that OPTIONS issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

delete is usually slower than just creating a new Object.
Maybe just fix the OPTIONS and keep that code and just open a new issue to discuss it.

@Maxime-J Maxime-J marked this pull request as draft March 15, 2023 17:42
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[undici] Preflight requests from Safari
2 participants