scalajettyunfiltered

Serving static files without directory browsing using Unfiltered and Jetty


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.


Solution

  • 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)