mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Merge pull request #248 from hacdias/master
browse: Option to ignore indexes
This commit is contained in:
		
						commit
						66fb8f031b
					
				@ -19,6 +19,7 @@ func Browse(c *Controller) (middleware.Middleware, error) {
 | 
				
			|||||||
	browse := browse.Browse{
 | 
						browse := browse.Browse{
 | 
				
			||||||
		Root:          c.Root,
 | 
							Root:          c.Root,
 | 
				
			||||||
		Configs:       configs,
 | 
							Configs:       configs,
 | 
				
			||||||
 | 
							IgnoreIndexes: false,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return func(next middleware.Handler) middleware.Handler {
 | 
						return func(next middleware.Handler) middleware.Handler {
 | 
				
			||||||
 | 
				
			|||||||
@ -26,6 +26,7 @@ type Browse struct {
 | 
				
			|||||||
	Next          middleware.Handler
 | 
						Next          middleware.Handler
 | 
				
			||||||
	Root          string
 | 
						Root          string
 | 
				
			||||||
	Configs       []Config
 | 
						Configs       []Config
 | 
				
			||||||
 | 
						IgnoreIndexes bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Config is a configuration for browsing in a particular path.
 | 
					// Config is a configuration for browsing in a particular path.
 | 
				
			||||||
@ -142,18 +143,20 @@ var IndexPages = []string{
 | 
				
			|||||||
	"default.txt",
 | 
						"default.txt",
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string) (Listing, error) {
 | 
					func directoryListing(files []os.FileInfo, r *http.Request, canGoUp bool, root string, ignoreIndexes bool) (Listing, error) {
 | 
				
			||||||
	var fileinfos []FileInfo
 | 
						var fileinfos []FileInfo
 | 
				
			||||||
	var urlPath = r.URL.Path
 | 
						var urlPath = r.URL.Path
 | 
				
			||||||
	for _, f := range files {
 | 
						for _, f := range files {
 | 
				
			||||||
		name := f.Name()
 | 
							name := f.Name()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Directory is not browsable if it contains index file
 | 
							// Directory is not browsable if it contains index file
 | 
				
			||||||
 | 
							if !ignoreIndexes {
 | 
				
			||||||
			for _, indexName := range IndexPages {
 | 
								for _, indexName := range 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!")
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if f.IsDir() {
 | 
							if f.IsDir() {
 | 
				
			||||||
			name += "/"
 | 
								name += "/"
 | 
				
			||||||
@ -234,7 +237,7 @@ func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		// Assemble listing of directory contents
 | 
							// Assemble listing of directory contents
 | 
				
			||||||
		listing, err := directoryListing(files, r, canGoUp, b.Root)
 | 
							listing, err := directoryListing(files, r, canGoUp, b.Root, b.IgnoreIndexes)
 | 
				
			||||||
		if err != nil { // directory isn't browsable
 | 
							if err != nil { // directory isn't browsable
 | 
				
			||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user