Commit 10ab0378 authored by Matthew Holt's avatar Matthew Holt

Moved fileServer and browse.IndexPages into middleware package

parent 540a651f
...@@ -4,7 +4,9 @@ CHANGES ...@@ -4,7 +4,9 @@ CHANGES
- basicauth: Support for legacy htpasswd files - basicauth: Support for legacy htpasswd files
- browse: JSON response with file listing given Accept header - browse: JSON response with file listing given Accept header
- core: Caddyfile as command line argument - core: Caddyfile as command line argument
- errors, log: Roll log files after certain size or age
- templates: Added .StripExt and .StripHTML methods - templates: Added .StripExt and .StripHTML methods
- Internal improvements and minor bug fixes
0.7.5 (August 5, 2015) 0.7.5 (August 5, 2015)
......
...@@ -56,7 +56,7 @@ type Listing struct { ...@@ -56,7 +56,7 @@ type Listing struct {
// And which order // And which order
Order string Order string
// User defined costum variables // Optional custom variables for use in browse templates
User interface{} User interface{}
middleware.Context middleware.Context
...@@ -138,15 +138,6 @@ func (fi FileInfo) HumanModTime(format string) string { ...@@ -138,15 +138,6 @@ func (fi FileInfo) HumanModTime(format string) string {
return fi.ModTime.Format(format) return fi.ModTime.Format(format)
} }
var IndexPages = []string{
"index.html",
"index.htm",
"index.txt",
"default.html",
"default.htm",
"default.txt",
}
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string, ignoreIndexes bool, vars interface{}) (Listing, error) { func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string, ignoreIndexes bool, vars interface{}) (Listing, error) {
var fileinfos []FileInfo var fileinfos []FileInfo
var urlPath = r.URL.Path var urlPath = r.URL.Path
...@@ -155,7 +146,7 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s ...@@ -155,7 +146,7 @@ func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root s
// Directory is not browsable if it contains index file // Directory is not browsable if it contains index file
if !ignoreIndexes { if !ignoreIndexes {
for _, indexName := range IndexPages { for _, indexName := range middleware.IndexPages {
if name == indexName { if name == indexName {
return Listing{}, errors.New("Directory contains index file, not browsable!") return Listing{}, errors.New("Directory contains index file, not browsable!")
} }
......
package server package middleware
import ( import (
"net/http" "net/http"
"os" "os"
"path" "path"
"strings" "strings"
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/browse"
) )
// This file contains a standard way for Caddy middleware
// to load files from the file system given a request
// URI and path to site root. Other middleware that load
// files should use these facilities.
// FileServer implements a production-ready file server
// and is the 'default' handler for all requests to Caddy.
// It simply loads and serves the URI requested. If Caddy is
// run without any extra configuration/directives, this is the
// only middleware handler that runs. It is not in its own
// folder like most other middleware handlers because it does
// not require a directive. It is a special case.
//
// FileServer is adapted from the one in net/http by // FileServer is adapted from the one in net/http by
// the Go authors. Significant modifications have been made. // the Go authors. Significant modifications have been made.
// //
// // Original license:
// License:
// //
// Copyright 2009 The Go Authors. All rights reserved. // Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
func FileServer(root http.FileSystem, hide []string) middleware.Handler { func FileServer(root http.FileSystem, hide []string) Handler {
return &fileHandler{root: root, hide: hide} return &fileHandler{root: root, hide: hide}
} }
...@@ -82,7 +91,7 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st ...@@ -82,7 +91,7 @@ func (fh *fileHandler) serveFile(w http.ResponseWriter, r *http.Request, name st
// use contents of an index file, if present, for directory // use contents of an index file, if present, for directory
if d.IsDir() { if d.IsDir() {
for _, indexPage := range browse.IndexPages { for _, indexPage := range IndexPages {
index := strings.TrimSuffix(name, "/") + "/" + indexPage index := strings.TrimSuffix(name, "/") + "/" + indexPage
ff, err := fh.root.Open(index) ff, err := fh.root.Open(index)
if err == nil { if err == nil {
...@@ -134,3 +143,14 @@ func redirect(w http.ResponseWriter, r *http.Request, newPath string) { ...@@ -134,3 +143,14 @@ func redirect(w http.ResponseWriter, r *http.Request, newPath string) {
} }
http.Redirect(w, r, newPath, http.StatusMovedPermanently) http.Redirect(w, r, newPath, http.StatusMovedPermanently)
} }
// IndexPages is a list of pages that may be understood as
// the "index" files to directories.
var IndexPages = []string{
"index.html",
"index.htm",
"index.txt",
"default.html",
"default.htm",
"default.txt",
}
...@@ -20,7 +20,7 @@ type virtualHost struct { ...@@ -20,7 +20,7 @@ type virtualHost struct {
// on its config. This method should be called last before // on its config. This method should be called last before
// ListenAndServe begins. // ListenAndServe begins.
func (vh *virtualHost) buildStack() error { func (vh *virtualHost) buildStack() error {
vh.fileServer = FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile}) vh.fileServer = middleware.FileServer(http.Dir(vh.config.Root), []string{vh.config.ConfigFile})
// TODO: We only compile middleware for the "/" scope. // TODO: We only compile middleware for the "/" scope.
// Partial support for multiple location contexts already // Partial support for multiple location contexts already
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment