Broken shareFilesFromDirectory() ?
I was having trouble serving files from a location using shareFilesFromDirectory and tracked it down to a couple of lines in Socket+File.swift. This may be completely the wrong solution and it seems odd that no-one's picked up on it but it's working for me now so if it helps anyone ...
line 10:
#if os(iOS) || os(tvOS) || os (Linux) || os (macOS) // ka added macOS
and line 44:
#if os(iOS) || os(tvOS) || os (Linux) || os (macOS) // KA: Added macOS
This broke for me as well on the latest version... 1.4.6.
However, I tried your fix and it didn't work.
Turns out the bug was introduced in this commit... perhaps we need better testing on here. https://github.com/httpswift/swifter/commit/18d34f2451230eb7b7603d11c1de58c8a045dfb3
Hey, @Joebayld @kimaldis Thanks for reporting this. I would need more details about the issue, can you provide with a sample project with the reproduced bug?
I've dropped a stripped down version of the project into my dropbox here . sorry about any junk left in there.
built out of the box it fails when you browse to http://localhost:8082/root/<foo.html. Editing the two lines I've marked in Socket+File.swift to include (macOS) should fix it. I found I had to clean the build folder to get Socket+File.swift to compile
You'll need a folder with some html in an NSHomeDirectory ( "/Users/
macOS Mojave, 10.14.4, Xcode Version 10.2 (10E125), by the way
@kimaldis Thanks for the sample project! I'll take a look as soon as I can.
How goes this? Need any help? Perhaps we make a release without the changes made to 'shareFilesFromDirectory()' as this is a breaking change?
@kimaldis I found the source of your error! Your error was coming from the impossibility of the sendfile function to write into the socket, see below:
https://github.com/httpswift/swifter/blob/3367b63158fdf14d4eb9697552c42f87bf72fc95/XCode/Sources/Socket%2BFile.swift#L39-L53
If you put a breakpoint in line 50 you will see the error being returned for the function and it's
Operation not permitted.
So I started to investigate a little and I noticed this in this thread:
Sandboxed apps cannot read files from outside the sandbox, except where explicitly given permission by the user via an Open File dialog, which returns a "security scoped URL".
When I went to your app Capabilities you had the Sandbox enabled, so once I disabled it the app started to work fine.
Also in the DemoServer you can test that in the stable branch the shareFilesFromDirectory() it's working perfectly if you try to create files inside the directory inside the SwiterSampleOSX.
PD: I tested your demo project using the latest changes in the stable branch.
@Joebayld I don't know exactly what are you doing and how but as I explained above I tested the shareFilesFromDirectory() in the DemoServer.swift and it's working perfectly. So please check it out your problem or try to create sample project with the reproduced bug in order to help you.
I'm sharing files that are inside my App Bundle via shareFilesFromDirectory(Bundle.main.resourcePath! + "/dist/assets")..
I had sandbox disabled and it still didn't work. I can try to make a test project - unless you just try sharing a file from inside the app bundle on your test project?
Update - still not working for me in 1.4.6. Below is how I'm using the server.. the index.html file references static assets. I constantly get a error 404 not found in version 1.4.6.
@Joebayld I can confirm that's working fine as in the DemoServer.swift when you run the SwifterSampleOSX macOS app the dir is the one in the main bundle. But you can replace it with your line and it should be there a logo.png we have in the app.
Change this:
let server = demoServer(try String.File.currentWorkingDirectory())
to this in the main.swift:
let server = demoServer(Bundle.main.resourcePath!)
And run the sample project. Then you can try to get the file in the address http://localhost:9080/public/logo.png. Also, I created another file myself in the path and it's working fine.
What I recommend you is after you get the Podfile pointing to stable clear your Derived Data dir and clean and build your project again.
If that's doesn't solve your problem I recommend you put a breakpoint in the shareFilesFromDirectory and see what's happening with your file there or create a sample project in order to see the issue closely.
I think found the issue.. My assets folder has nested sub-folders. Turns out that doesn't work in this new update.. Would that be considered a bug? If you share the folder you would think it shares all sub-folders..
The shareFilesFromDirectory only goes down one level.
@Vkt0r Here is a test project for my scenario. https://www.dropbox.com/s/z836ap3vl6bvghh/Swifter_146_Issue.zip?dl=0
I intentionally don't have the swifter framework linked. I did that so you can try 1.4.5 and 1.4.6 to see the difference.
Hey, @Joebayld I run your project with the latest changes in stable specifying in the Podfile:
pod 'Swifter', :git => 'https://github.com/httpswift/swifter', :branch => 'stable'
Your project and the one of @kimaldis made me realize that we need to change the shareFilesFromDirectory function to return the errors happening as by default is returning a 404 in case of any and this is not useful for anyone experimenting any kind of error.
So once I tracked the error that the function was throwing the rest was easy, take a look:
No such file or directory
So changing your following line:
server["assets/:path"] = shareFilesFromDirectory(Bundle.main.resourcePath! + "/dist/assets")
for this one:
server["assets/:path"] = shareFilesFromDirectory(Bundle.main.resourcePath! + "/dist/assets/img")
Solves the problem trying to access to http://localhost:8081/assets/thumb_up.png. With another route, I don't have any problem at all as it was always working rendering the index.html on http://localhost:8081/.
As you saw above your problem was a matter of specifying the correct path for your route. You mentioned before that was working in 1.4.5 and I believed you as in that version the HttpRouter was resolving routes incorrectly as I experimented myself when we released the last version. Hope this helps you.
Hey @Vkt0r,
Understood. I see your workaround but unfortunately I don’t know that if that solves my issue here. I’m sharing a few folders and to do it that way would require creating a new route for each folder. In prior versions it would allow sharing a root folder and would also share sub-folders as well. (To navigate to sub-folders you would have slashes in the url)
It’s odd that it’s throwing that error as the directory exists.. I’ll try to dive into this a little more. For example: In the past I would navigate to http://localhost:8081/assets/img/thumb_up.png and http://localhost:8081/assets/js/some_code.js with no issues. To do that now I’d have to share each folder separately - and it would also be an issue if I wanted to share files in sub-folders.
Thanks for working on this!
@Joebayld See this please #405 and my comment in the issue. That's the same issue you're having now!