From 6e2957df824d25d14aad9f9890bac448c632d539 Mon Sep 17 00:00:00 2001 From: Fareed Dudhia Date: Wed, 16 Jul 2014 19:16:15 +0100 Subject: [PATCH] Added HEAD to RouterGroup.Static --- gin.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/gin.go b/gin.go index 4d768723..67448635 100644 --- a/gin.go +++ b/gin.go @@ -11,8 +11,11 @@ import ( "html/template" "log" "math" + "mime" "net/http" + "os" "path" + "path/filepath" "sync" ) @@ -309,6 +312,19 @@ func (group *RouterGroup) Static(p, root string) { fileServer.ServeHTTP(c.Writer, c.Request) c.Request.URL.Path = original }) + + group.HEAD(p, func(c *Context) { + fp := path.Join(root, c.Params.ByName("filepath")) + + info, err := os.Stat(fp) + if err != nil || info == nil { + c.Abort(404) + } else { + c.Writer.Header().Set("Content-Type", mime.TypeByExtension(filepath.Ext(fp))) + c.Writer.Header().Set("Last-Modified", info.ModTime().Format(http.TimeFormat)) + c.Abort(200) + } + }) } func (group *RouterGroup) combineHandlers(handlers []HandlerFunc) []HandlerFunc {