perry-mitchell / webdav-client

WebDAV client written in Typescript for NodeJS and the browser
MIT License
668 stars 146 forks source link

Calling stat request on directory path caused 301 Moved Permanently #290

Open bitrevo opened 2 years ago

bitrevo commented 2 years ago

My project need to create directories on webdav server and the createDirectory do not work with recursive option because of the CORS preflight request.

For example, a recursive call to create a path /a/b/c, multiple stat requests are made to defect if the directory /a and /a/b are exists.

The library send out the request with the path /a instead of /a/ which caused webdav server responses 301 Moved Permanently, it is expected behavior regarding the MDN doc (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSExternalRedirectNotAllowed).

Is it make sense to fix the createDirectory API call by add the ending slash to the stat request to avoid the CORS error?

perry-mitchell commented 2 years ago

@bitrevo You're right, collections should be queried with a trailing slash. Several sources I've checked (no RFC that I found, yet) seem to suggest to do so to remove ambiguity and that beyond that the server will return 301 to normalise paths as it chooses.

This needs to be fixed, yes, but you should also review your CORS configuration. A 301 should not result in a CORS error, and it sounds like the service is not correctly configured for web-based access.

bitrevo commented 2 years ago

@perry-mitchell Thanks for the information. I found the issue caused by my Apache webdav backend.

The browser will send OPTIONS request to the new directory path for cors preflight checking, and the backend should response 200 even if the target directory not exists. The default setting of Apache will send out response 301 if accessing a directory path without the ending slash. (/a to /a/)

Adding DirectorySlash Off to Apache configuration fixed this for me. Reference: https://stackoverflow.com/questions/13354238/how-to-disable-301-redirect-that-adds-trailing-slash-to-directory-name-in-apache

It's not a bug but fix it will get around this issue for similar cases.