【Golang】Go 标准库 http.FileServer 实现静态文件服务

http.FileServer 方法属于标准库 net/http,返回一个使用 FileSystem 接口 root 提供文件访问服务的 HTTP 处理器。可以方便的实现静态文件服务器。

http.ListenAndServe(":8080", http.FileServer(http.Dir("/files/path")))

访问 http://127.0.0.1:8080,即可看到类似 Nginx 中 autoindex 目录浏览功能。

源码解析

我们现在开始将上述的那仅有的一行代码进行剖析,看看到底是如何实现的。源码中英文注释也比较详细,可以参考。

我们先看 http.Dir(),再看 http.FileServer(),而 http.ListenAndServe() 监听 TCP 端口并提供路由服务,此处不赘述。

http.Dir()

从以下源码我们可以看出,type Dir string 实现了 type FileSystem interface 的接口函数 Open,http.Dir("/") 实际返回的是 http.Dir 类型,将字符串路径转换成文件系统。
// A Dir implements FileSystem using the native file system restricted to a
// specific directory tree.
//
// While the FileSystem.Open method takes ‘/’-separated paths, a Dir’s string
// value is a filename on the native file system, not a URL, so it is separated
// by filepath.Separator, which isn’t necessarily ‘/’.
//
// Note that Dir will allow access to files and directories starting with a
// period, which could expose sensitive directories like a .git directory or
// sensitive files like .htpasswd. To exclude files with a leading period,
// remove the files/directories from the server or create a custom FileSystem
// implementation.
//
// An empty Dir is treated as “.”.

type Dir string

// A FileSystem implements access to a collection of named files.
// The elements in a file path are separated by slash (’/’, U+002F)
// characters, regardless of host operating system convention.

type FileSystem interface {
	Open(name string) (File, error)
}

// Open implements FileSystem using os.Open, opening files for reading rooted
// and relative to the directory d.

func (d Dir) Open(name string) (File, error) {
	if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
		return nil, errors.New("http: invalid character in file path")
	}
	dir := string(d)
	if dir == "" {
		dir = "."
	}
	fullName := filepath.Join(dir, filepath.FromSlash(path.Clean("/"+name)))
	f, err := os.Open(fullName)
	if err != nil {
		return nil, mapDirOpenError(err, fullName)
	}
	return f, nil
}
http.FileServer()

http.FileServer() 方法返回的是 fileHandler 实例,而 fileHandler 结构体实现了 Handler 接口的方法 ServeHTTP()。ServeHTTP 方法内的核心是 serveFile() 方法。

// FileServer returns a handler that serves HTTP requests
// with the contents of the file system rooted at root.
//
// To use the operating system’s file system implementation,
// use http.Dir:
//
// http.Handle("/", http.FileServer(http.Dir("/tmp")))
//
// As a special case, the returned file server redirects any request
// ending in “/index.html” to the same path, without the final
// “index.html”.

func FileServer(root FileSystem) Handler {
	return &fileHandler{root}
}

fileHandler的定义:

type fileHandler struct {
	root FileSystem
}

fileHandler实现了ServeHTTP(w ResponseWriter, r *Request)方法:

func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
	upath := r.URL.Path
	if !strings.HasPrefix(upath, "/") {
		upath = "/" + upath
		r.URL.Path = upath
	}
	serveFile(w, r, f.root, path.Clean(upath), true)
}
serveFile()

serveFile() 方法判断,如果访问路径是目录,则列出目录内容,如果是文件则使用 serveContent() 方法输出文件内容。serveContent() 方法则是个读取文件内容并输出的方法,此处不再贴代码。

// name is ‘/’-separated, not filepath.Separator.

func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirect bool) {

	// 中间代码已省略

	if d.IsDir() {
		if checkIfModifiedSince(r, d.ModTime()) == condFalse {
			writeNotModified(w)
			return
		}
		w.Header().Set("Last-Modified", d.ModTime().UTC().Format(TimeFormat))
		dirList(w, r, f)
		return
	}

	// serveContent will check modification time
	sizeFunc := func() (int64, error) { return d.Size(), nil }
	serveContent(w, r, d.Name(), d.ModTime(), sizeFunc, f)
}
参考

https://golang.org/src/net/http/fs.go

发布了349 篇原创文章 · 获赞 14 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/LU_ZHAO/article/details/105304982