-
Notifications
You must be signed in to change notification settings - Fork 237
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
fix(proxy): workaround undici issue with DELETE
and content-length of 0
#605
Conversation
@@ -240,6 +240,43 @@ describe("", () => { | |||
|
|||
expect(resText).toEqual(message); | |||
}); | |||
|
|||
it("can proxy DELETE request", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests seem to be pass even by commenting out the changes in utils/proxy we might add something that fails without it.
@@ -57,6 +57,12 @@ export async function proxyRequest( | |||
opts.headers, | |||
); | |||
|
|||
// DELETE method must always have an empty object in the body, and a "content-length" header. | |||
if (method === "DELETE" && body === undefined) { | |||
body = Buffer.from("{}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By HTTP spec, it is not disallowed. However conventionally, HTTP servers fail when DELETE
omits the body. I'm not sure using a JavaScript HTTP object would be best solution.
Thanks for working on this PR dear @jasenmichael. I'm not sure if you have already followed but this is relevant issue in undici that causes error (nodejs/undici#2046) With nodejs/undici#2305, Node.js should now allow content-length of |
DELETE
and content-length of 0
So nodejs/undici#2305 has been merged since https://github.com/nodejs/undici/releases/tag/v5.27.1. |
it doesn't depend on any specific version of undici. it is only a dev dependency of the repository itself for another reason (miniflare used in test) |
Thanks for your PR dear @jasenmichael. earlier in comments, new tests pass with/without the changes (#605 (comment)) also we cannot do it automatically to provide a probably unwanted body with data of This is an important issue and I hope to find a fix but let's track in #375 |
Thank you, and yes I realized quickly this pr was not an actual solution, but more of a "use-case workaround", and would break any proxy that requires a DELETE request body to be empty. After further debugging, I have NOT been able to re-create the issue in a nitro app. Proxied DELETE requests work fine directly in a nitro app, but not through Nuxt3 (both using the same versions of nitro and h3). I spun up the nitro app using the same routeRules used in Nuxt3. here is my routeRules configuration:
Here is the error that Nuxt3 gives when making a proxied DELETE request:
NOTE: When using Nuxt3, I see DELETE requests fail in the browser network tab, but if I edit and re-send with an empty object in the body it succeeds. My pr circumvents this only. Since this is a proxy, I do not have control over DELETE requests, so sending an empty body is not an option. Another point to make is - my server (that I am proxying to) does not require a body for DELETE requests, but still succeeds when I send an empty body. A proxied DELETE request through Nuxt3 works ONLY if there is something in the body. My conclusion is - the failure only occurs when a proxied DELETE request is made through nuxt3 with an empty body. related: #375 cheers, |
Thanks for kind words and more details. Well I have to say it is now even more interesting! I will try to make same reproduction as you have with Nuxt. If you have better one, please feel free to share it in #375 that would make help a lot in order to followup. |
Tested with nuxt 3.10 and node 20.11.0 and delete now works fine. |
Linked issue
❓ Type of change
Description
This pull request addresses the issue of proxy DELETE requests failing when no body or "content-length" header is present.
example error (this PR solves) when making a proxy DELETE request with nuxt3/nitro using h3:
Fixes:
async sendProxy (./node_modules/h3/dist/index.mjs)
Checklist