swifter
swifter copied to clipboard
Cannot access root path anymore with directoryBrowser
I used to be able to browse the root path of directoryBrowser, but now I can't. If I access "http://localhost:8080/files/images" it works, but if I try to access "http://localhost:8080/files" it doesn't work anymore.
I don't remember what version of Swifter I was using when it worked, but now it is the 1.4.7.
This is what I had:
server["/files/:path"] = directoryBrowser(FileUtils.documentsDirectory.path)
Now I have to declare another route "/files" and create a 2nd version of directoryBrowser without the guard at the beginning.
server["/files/:path"] = directoryBrowser(FileUtils.documentsDirectory.path)
server["/files"] = directoryBrowserFixed(FileUtils.documentsDirectory.path)
public func directoryBrowserFixed(_ dir: String) -> ((HttpRequest) -> HttpResponse) {
return { request in
// workaround
let (_, value) = request.params.first ?? ("", "")
// guard let (_, value) = request.params.first else {
// return HttpResponse.notFound
// }
let filePath = dir + String.pathSeparator + value
...
Hey, @pakerwreah Thanks for reporting this. Unfortunately this behavior has changed as definition of routes like /files/:path
was returning incorrect results treating :path
as a wildcard and causing more problems in the resolution of several routes, see the discussion in #405 for more information.
But if what are you looking for is list a directory you can do it easily but in another way. For example, if you go to the DemoServer.swift
the file you will see we are using the following line of code:
server["/files/:path"] = directoryBrowser("/")
If you replace it instead with the following line:
server["/files/:path"] = directoryBrowser(publicDir)
Note:
publicDir
is equal toString.File.currentWorkingDirectory()
And then you use the .
to enter the directory like this route http://localhost:9080/files/.
it should list the directory successfully:
.
..
logo.png
Swifter.framework
SwifterSampleOSX
SwifterSampleOSX.swiftmodule
Please note the browsers use a cache, etc so to test the routes you should make the request using Postman, Paw, curl in terminal or another tool you want.
It works if I type:
curl -v --path-as-is http://localhost:8080/files/.
But unfortunately it's not very useful that way.
@pakerwreah Can you more specific please about how are you using the directoryBrowser
specifically? Why is not very useful using the .
sequence?
I use directoryBrowser to explore and download files from my iOS app documents folder. My database file for example is at the root folder.
This functionality is also used by my test team. It's not very productive to tell them to open a terminal and use curl.
If you type .../files/.
on the web browser it automatically normalizes the path to .../files
, the same way as curl, but using curl we can pass this flag --path-as-is
so it won't change the path and it works.
ps.: files without extension are also not downloading anymore.
Swifter is still very useful for me because I've built a SQL console with it to query my app's database directly. I've also made a network interceptor using websocket. But as for browsing files, maybe I should search for a FTP server.
@pakerwreah I understand your use now, thanks for sharing it. I think the topic of the wildcard to match all the routes in #405 would help you and solve your problem. Unfortunately, I cannot promise a deadline for when it would come. I would do my best to try to integrate it soon, maybe somebody else can come with a solution, let's see...