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

View file

@ -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 {
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
// 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!")
} }

View file

@ -1,25 +1,34 @@
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
// 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) {
} }
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",
}

View file

@ -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