tus / tus-js-client

A pure JavaScript client for the tus resumable upload protocol
https://tus.io/
MIT License
2.12k stars 316 forks source link

Wrong status code check when Location header is set? #731

Closed jezzdk closed 4 weeks ago

jezzdk commented 4 weeks ago

Question I'm trying to implement the direct creator uploads method from Cloudflare, and I'm following the guide, but I've run into some problems.

When my endpoint returns a "Location:" header, the response code is automatically set to 302 because that header specifies a redirect. However, the tus-js-client only checks if the status code is 200 or throws an error.

I'm using PHP on the backend, and PHP forces a 302 if the "Location:" header is set. It also seems to be the standard behavior. In any case I'm not able to force a status code of 200, so I'm not able to use this upload method even though all the documentation says I should. Is there something I can do to make this work?

Setup details Please provide following details, if applicable to your situation:

Acconut commented 4 weeks ago

The Location header is not exclusive used for redirects with 3xx responses. It can also be used with the 201 status code to indicate the URL of a newly created resource, which how tus uses the header. This is in line with RFC 9110, which defines the schematics of HTTP and, in particular, the Location header: https://www.rfc-editor.org/rfc/rfc9110.html#name-location (As a brief side note: RFC 2616 has been obsoleted through many revisions, so it's better to use the newer RFCs that replaced it).

From the standard's point of view, tus' use of Location and 201 is not unusual or incorrect. I would suggest you to fix the PHP part generating the response. It appears odd to me that PHP would not allow you to customize the status code.

jezzdk commented 4 weeks ago

Thanks for the reply. I didn't think about setting the status code to 201 instead of 200, but PHP allows me to do that and that actually makes it work. Edit: I should've looked into the inStatusCategory() function. I would've realized that 201 would make it pass...