1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00

Moved fileServer and browse.IndexPages into middleware package

This commit is contained in:
Matthew Holt 2015-09-19 20:34:23 -06:00
parent 540a651fdf
commit 10ab037833
5 changed files with 33 additions and 20 deletions

2
dist/CHANGES.txt vendored
View file

@ -4,7 +4,9 @@ CHANGES
- basicauth: Support for legacy htpasswd files
- browse: JSON response with file listing given Accept header
- core: Caddyfile as command line argument
- errors, log: Roll log files after certain size or age
- templates: Added .StripExt and .StripHTML methods
- Internal improvements and minor bug fixes
0.7.5 (August 5, 2015)

View file

@ -56,7 +56,7 @@ type Listing struct {
// And which order
Order string
// User defined costum variables
// Optional custom variables for use in browse templates
User interface{}
middleware.Context
@ -138,15 +138,6 @@ func (fi FileInfo) HumanModTime(format string) string {
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) {
var fileinfos []FileInfo
var urlPath = r.URL.Path
@ -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
if !ignoreIndexes {
for _, indexName := range IndexPages {
for _, indexName := range middleware.IndexPages {
if name == indexName {
return Listing{}, errors.New("Directory contains index file, not browsable!")
}

View file

@ -1,25 +1,34 @@
package server
package middleware
import (
"net/http"
"os"
"path"
"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
// the Go authors. Significant modifications have been made.
//
//
// License:
// Original license:
//
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// 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}
}
@ -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
if d.IsDir() {
for _, indexPage := range browse.IndexPages {
for _, indexPage := range IndexPages {
index := strings.TrimSuffix(name, "/") + "/" + indexPage
ff, err := fh.root.Open(index)
if err == nil {
@ -134,3 +143,14 @@ func redirect(w http.ResponseWriter, r *http.Request, newPath string) {
}
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",
}

View file

@ -20,7 +20,7 @@ type virtualHost struct {
// on its config. This method should be called last before
// ListenAndServe begins.
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.
// Partial support for multiple location contexts already