swifter icon indicating copy to clipboard operation
swifter copied to clipboard

Static pages broken?

Open kimaldis opened this issue 7 years ago • 8 comments

static files appear not to be getting served:

let server = HttpServer()
server["/files/:path"] = directoryBrowser( PublicDir )

do {
    let port = 8082
    try server.start( in_port_t(port) )
    print( "HTTP Server started on \(port)")
} catch {
    print( "failed to start web server \(error)" )
}

Browse to http://localhost:8082/files and see a correct list of files and directories. Click on an html file and I get an empty page.

Browse to http://localhost:8082/files/test.html, where test.html exists. Again, I get an empty page.

Thoughts appreciated.

kimaldis avatar Oct 23 '18 08:10 kimaldis

Anyone have thoughts on this? Am I missing something obvious?

kimaldis avatar Oct 27 '18 15:10 kimaldis

What HTTP response code do you get on the http://localhost:8082/files/test.html: 200, 400 or 500 ? Did you check file permissions ?

I can see in https://github.com/httpswift/swifter/blob/b33295680aea4fae0d88f07024da35b4d9f33ca2/Sources/Files.swift#L41 that if a read error occurs, it is currently ignored. It should return something appropriate, like a 403 on permission error.

marcc-orange avatar Oct 29 '18 08:10 marcc-orange

File permissions checked right through the tree, set to 0777 while testing. I'm struggling to find a response code anywhere.

I did find that. the closure, return { r in ... in shareFilesFromDirectory() isn't getting called at all. Currently trying to figure out why that may be.

kimaldis avatar Oct 29 '18 10:10 kimaldis

I did find that. the closure, return { r in ... in shareFilesFromDirectory() isn't getting called at all. Currently trying to figure out why that may be.

My bad, I was misunderstanding the way this was working.

kimaldis avatar Oct 29 '18 11:10 kimaldis

writer.write( file ) on line 41 throws an error; "sendfile: Operation not permitted". test.html is in '/Users/kimaldis/Library/Containers/com.kim-aldis.EarwigServer/Data/htdocs'. The file has 0777 permissions as does the tree leading up to it. I'm serving from this folder, returned by NSHomeDirectory() + "/htdocs" because App Sandboxing, which seems to be new in Mojave/Xcode 10, wasn't allowing me to serve from much of anywhere else. I've enabled network incoming and outgoing access but there doesn't seem to be much else obvious there to try. Digging continues.

kimaldis avatar Oct 29 '18 16:10 kimaldis

sendfile() is returning -1 in Socket, line 45 of Socket+File.swift. As I said, I have Incoming & Outgoing connections enabled in the App Sandbox and all file permissions are good. I'm not sure where else I might look.

This is Mojave, Xcode 10, btw.

kimaldis avatar Oct 29 '18 17:10 kimaldis

I think I've found it. sendfile() at line 45 of Socket+File.swift fails but using sendfileImpl() instead seems to work. I changed two lines in Socket+File.swift:

Line 10 #if os(iOS) || os(tvOS) || os (Linux) || os (macOS)

and

Line 42 #if os(iOS) || os(tvOS) || os (Linux) || os (macOS)

My understanding of sockets isn't good enough that I can be sure what's going on or, indeed, that this won't have unwanted side effects but it's working for now.

This also fixes the problem I was having with directoryBrowser().

Thanks, Marc, for the input, it got me to the right place.

kimaldis avatar Oct 31 '18 09:10 kimaldis

Hey @Vkt0r - I'm just having this exact issue right now, and was wondering if there's any chance of making this change? It does seem that sendFile() is not permitted when app sandboxing is enabled. Is there a reason not to use sendfileImpl() for macOS?

KristinaLauwers avatar Aug 13 '20 14:08 KristinaLauwers