mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	Adapted std lib file server and gutted it
This commit is contained in:
		
							parent
							
								
									9672850d11
								
							
						
					
					
						commit
						ba0d63d722
					
				
							
								
								
									
										78
									
								
								server/fileserver.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								server/fileserver.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,78 @@
 | 
				
			|||||||
 | 
					package server
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"path"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/mholt/caddy/middleware/browse"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// This FileServer is adapted from the one in net/http
 | 
				
			||||||
 | 
					// by the Go authors. Some modifications have been made.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// 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) http.Handler {
 | 
				
			||||||
 | 
						return &fileHandler{root}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type fileHandler struct {
 | 
				
			||||||
 | 
						root http.FileSystem
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (f *fileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
				
			||||||
 | 
						upath := r.URL.Path
 | 
				
			||||||
 | 
						if !strings.HasPrefix(upath, "/") {
 | 
				
			||||||
 | 
							upath = "/" + upath
 | 
				
			||||||
 | 
							r.URL.Path = upath
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						serveFile(w, r, f.root, path.Clean(upath), true)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// name is '/'-separated, not filepath.Separator.
 | 
				
			||||||
 | 
					func serveFile(w http.ResponseWriter, r *http.Request, fs http.FileSystem, name string, redirect bool) {
 | 
				
			||||||
 | 
						f, err := fs.Open(name)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							// TODO expose actual error?
 | 
				
			||||||
 | 
							http.NotFound(w, r)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer f.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						d, err1 := f.Stat()
 | 
				
			||||||
 | 
						if err1 != nil {
 | 
				
			||||||
 | 
							// TODO expose actual error?
 | 
				
			||||||
 | 
							http.NotFound(w, r)
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// use contents of an index file, if present, for directory
 | 
				
			||||||
 | 
						if d.IsDir() {
 | 
				
			||||||
 | 
							for _, indexPage := range browse.IndexPages {
 | 
				
			||||||
 | 
								index := strings.TrimSuffix(name, "/") + "/" + indexPage
 | 
				
			||||||
 | 
								ff, err := fs.Open(index)
 | 
				
			||||||
 | 
								if err == nil {
 | 
				
			||||||
 | 
									defer ff.Close()
 | 
				
			||||||
 | 
									dd, err := ff.Stat()
 | 
				
			||||||
 | 
									if err == nil {
 | 
				
			||||||
 | 
										name = index
 | 
				
			||||||
 | 
										d = dd
 | 
				
			||||||
 | 
										f = ff
 | 
				
			||||||
 | 
										break
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Still a directory? (we didn't find an index file)
 | 
				
			||||||
 | 
						if d.IsDir() {
 | 
				
			||||||
 | 
							http.NotFound(w, r) // 404 instead of 403 to hide the fact that the folder exists
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						http.ServeContent(w, r, d.Name(), d.ModTime(), f)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -101,7 +101,7 @@ func (s *Server) Log(v ...interface{}) {
 | 
				
			|||||||
// 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 (s *Server) buildStack() error {
 | 
					func (s *Server) buildStack() error {
 | 
				
			||||||
	s.fileServer = http.FileServer(http.Dir(s.config.Root))
 | 
						s.fileServer = FileServer(http.Dir(s.config.Root))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, start := range s.config.Startup {
 | 
						for _, start := range s.config.Startup {
 | 
				
			||||||
		err := start()
 | 
							err := start()
 | 
				
			||||||
@ -112,7 +112,7 @@ func (s *Server) buildStack() error {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// 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
 | 
				
			||||||
	// exists in the parser and config levels, but until full
 | 
						// exists at the parser and config levels, but until full
 | 
				
			||||||
	// support is implemented, this is all we do right here.
 | 
						// support is implemented, this is all we do right here.
 | 
				
			||||||
	s.compile(s.config.Middleware["/"])
 | 
						s.compile(s.config.Middleware["/"])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user