duanken7168
2015-03-01 12:38
浏览 373
已采纳

Golang。 使用什么? http.ServeFile(..)还是http.FileServer(..)?

I'm a little bit confused. Much of examples shows usage of both: http.ServeFile(..) and http.FileServer(..), but seems they have very close functionality. Also I have found no information about how to set custom NotFound handler.

// This works and strip "/static/" fragment from path
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))

// This works too, but "/static2/" fragment remains and need to be striped manually
http.HandleFunc("/static2/", func(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, r.URL.Path[1:])
})

http.ListenAndServe(":8080", nil)

I've tried to read source code and both of them use serveFile(ResponseWriter, *Request, FileSystem, string, bool) underlying function. However http.FileServer return fileHandler with its own ServeHTTP() method and make some preparation work before serving file (eg path.Clean()).

So why need this separation? Which method better to use? And how can I set custom NotFound handler, for example when requested file not found?

图片转代码服务由CSDN问答提供 功能建议

我有点困惑。 许多示例都显示了这两种用法: http.ServeFile(..) http.FileServer(..),但似乎它们具有非常接近的功能。 我也没有找到有关如何设置自定义NotFound处理程序的信息。

  //这可以从路径
fs中删除“ / static /”片段:= http.FileServer(http.Dir(“ static”))
http.Handle(  “ /static/",http.StripPrefix("/static/”,fs))
 
 //也可以,但是“ / static2 /”片段仍然存在,需要手动进行分割
http.HandleFunc(“ /  static2 /“,func(w http.ResponseWriter,r * http.Request){
 http.ServeFile(w,r,r.URL.Path [1:])
})
 
http.ListenAndServe(”  :8080“,nil)
   
 
 

我尝试读取源代码,并且它们都使用 serveFile(ResponseWriter,* Request,FileSystem,string, bool)基础功能。 但是, http.FileServer 返回带有自己的 ServeHTTP()方法的 fileHandler ,并在提供文件之前进行一些准备工作(例如path.Clean())

那么为什么需要这种分隔? 哪种方法更好用? 以及如何设置自定义的NotFound处理程序,例如在找不到请求的文件时?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhan5853 2015-03-01 19:40
    已采纳

    The main difference is that http.FileServer does effectively almost 1:1 mapping of an HTTP prefix with a filesystem. In plain english, it serves up an entire directory path. and all its children.

    Say you had a directory called /home/bob/static and you had this setup:

    fs := http.FileServer(http.Dir("/home/bob/static"))
    http.Handle("/static/", http.StripPrefix("/static", fs))
    

    Your server would take requests for e.g. /static/foo/bar and serve whatever is at /home/bob/static/foo/bar (or 404)

    In contrast, the ServeFile is a lower level helper that can be used to implement something similar to FileServer, or implement your own path munging potentially, and any number of things. It simply takes the named local file and sends it over the HTTP connection. By itself, it won't serve a whole directory prefix (unless you wrote a handler that did some lookup similar to FileServer)

    NOTE Serving up a filesystem naively is a potentially dangerous thing (there are potentially ways to break out of the rooted tree) hence I recommend that unless you really know what you're doing, use http.FileServer and http.Dir as they include checks to make sure people can't break out of the FS, which ServeFile doesn't.

    Addendum Your secondary question, how do you do a custom NotFound handler, unfortunately, is not easily answered. Because this is called from internal function serveFile as you noticed, there's no super easy place to break into that. There are potentially some sneaky things like intercepting the response with your own ResponseWriter which intercepts the 404 response code, but I'll leave that exercise to you.

    打赏 评论
  • doudaifu6083 2019-01-31 17:43

    Here a handler which sends a redirect to "/" if file is not found. This comes in handy when adding a fallback for an Angular application, as suggested here, which is served from within a golang service.

    Note: This code is not production ready. Only illustrative (at best :-)

        package main
    
        import "net/http"
    
        type (
            // FallbackResponseWriter wraps an http.Requesthandler and surpresses
            // a 404 status code. In such case a given local file will be served.
            FallbackResponseWriter struct {
                WrappedResponseWriter http.ResponseWriter
                FileNotFound          bool
            }
        )
    
        // Header returns the header of the wrapped response writer
        func (frw *FallbackResponseWriter) Header() http.Header {
            return frw.WrappedResponseWriter.Header()
        }
    
        // Write sends bytes to wrapped response writer, in case of FileNotFound
        // It surpresses further writes (concealing the fact though)
        func (frw *FallbackResponseWriter) Write(b []byte) (int, error) {
            if frw.FileNotFound {
                return len(b), nil
            }
            return frw.WrappedResponseWriter.Write(b)
        }
    
        // WriteHeader sends statusCode to wrapped response writer
        func (frw *FallbackResponseWriter) WriteHeader(statusCode int) {
            Log.Printf("INFO: WriteHeader called with code %d
    ", statusCode)
            if statusCode == http.StatusNotFound {
                Log.Printf("INFO: Setting FileNotFound flag
    ")
                frw.FileNotFound = true
                return
            }
            frw.WrappedResponseWriter.WriteHeader(statusCode)
        }
    
        // AddFallbackHandler wraps the handler func in another handler func covering authentication
        func AddFallbackHandler(handler http.HandlerFunc, filename string) http.HandlerFunc {
            Log.Printf("INFO: Creating fallback handler")
            return func(w http.ResponseWriter, r *http.Request) {
                Log.Printf("INFO: Wrapping response writer in fallback response writer")
                frw := FallbackResponseWriter{
                    WrappedResponseWriter: w,
                    FileNotFound:          false,
                }
                handler(&frw, r)
                if frw.FileNotFound {
                    Log.Printf("INFO: Serving fallback")
                    http.Redirect(w, r, "/", http.StatusSeeOther)
                }
            }
        }
    

    It can be added as in this example (using goji as mux):

        mux.Handle(pat.Get("/*"),
            AddFallbackHandler(http.FileServer(http.Dir("./html")).ServeHTTP, "/"))
    
    
    打赏 评论

相关推荐 更多相似问题