Serving static files without directory browsing using Unfiltered and Jetty

236 views Asked by At

How can I have unfiltered-jetty serve static files without allowing directory browsing?

Jetty has the dirAllowed setting, but it does not seem easily accessible from Unfiltered.

2

There are 2 answers

0
Dr.Haribo On BEST ANSWER

This is working with Unfiltered 0.8.4 which uses Jetty 8:

import org.eclipse.jetty.server.handler.{HandlerCollection,ContextHandler}
import org.eclipse.jetty.server.Handler

  def disableDirBrowsing(hc: Array[Handler]) {
    hc.map { h =>
      h match {
        case nested: HandlerCollection => disableDirBrowsing(nested.getHandlers)
        case c: ContextHandler =>
          c.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false")
        case _ => // ignore everything else
      }
    }
  }

If srv is your Unfiltered server object after adding contexts to it, you can now disable directory browsing like so:

disableDirBrowsing(srv.underlying.getHandlers)
1
Scott On

This is not a full answer but I bet you can put it together by looking in 2 places:
1. the val unfiltered.jetty.Server.underlying of type org.eclipse.jetty.server.Server in the unfiltered-jetty code
2. 'Configuring a File Server' in the Jetty 8(i think) wiki. Maybe that resource_handler.setDirectoriesListed(true) call?