2014-04-12 05:18:18 +00:00
|
|
|
// Beego (http://beego.me/)
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @description beego is an open-source, high-performance web framework for the Go programming language.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @link http://github.com/astaxie/beego for the canonical source repository
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-04-12 05:18:18 +00:00
|
|
|
// @license http://github.com/astaxie/beego/blob/master/LICENSE
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-06-25 02:39:37 +00:00
|
|
|
// @authors astaxie
|
2014-04-05 16:18:21 +00:00
|
|
|
package beego
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/context"
|
|
|
|
"github.com/astaxie/beego/middleware"
|
|
|
|
"github.com/astaxie/beego/utils"
|
|
|
|
)
|
|
|
|
|
2014-06-10 17:11:32 +00:00
|
|
|
func serverStaticRouter(ctx *context.Context) {
|
2014-04-08 09:43:25 +00:00
|
|
|
requestPath := path.Clean(ctx.Input.Request.URL.Path)
|
2014-04-05 16:18:21 +00:00
|
|
|
for prefix, staticDir := range StaticDir {
|
|
|
|
if len(prefix) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-16 14:27:53 +00:00
|
|
|
if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
|
2014-04-05 16:18:21 +00:00
|
|
|
file := path.Join(staticDir, requestPath)
|
|
|
|
if utils.FileExists(file) {
|
|
|
|
http.ServeFile(ctx.ResponseWriter, ctx.Request, file)
|
2014-06-10 17:11:32 +00:00
|
|
|
return
|
2014-07-16 14:27:53 +00:00
|
|
|
} else {
|
|
|
|
http.NotFound(ctx.ResponseWriter, ctx.Request)
|
|
|
|
return
|
2014-04-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(requestPath, prefix) {
|
|
|
|
if len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
file := path.Join(staticDir, requestPath[len(prefix):])
|
|
|
|
finfo, err := os.Stat(file)
|
|
|
|
if err != nil {
|
|
|
|
if RunMode == "dev" {
|
|
|
|
Warn(err)
|
|
|
|
}
|
|
|
|
http.NotFound(ctx.ResponseWriter, ctx.Request)
|
2014-06-10 17:11:32 +00:00
|
|
|
return
|
2014-04-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
//if the request is dir and DirectoryIndex is false then
|
|
|
|
if finfo.IsDir() && !DirectoryIndex {
|
|
|
|
middleware.Exception("403", ctx.ResponseWriter, ctx.Request, "403 Forbidden")
|
2014-06-10 17:11:32 +00:00
|
|
|
return
|
2014-04-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request
|
|
|
|
isStaticFileToCompress := false
|
|
|
|
if StaticExtensionsToGzip != nil && len(StaticExtensionsToGzip) > 0 {
|
|
|
|
for _, statExtension := range StaticExtensionsToGzip {
|
|
|
|
if strings.HasSuffix(strings.ToLower(file), strings.ToLower(statExtension)) {
|
|
|
|
isStaticFileToCompress = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if isStaticFileToCompress {
|
|
|
|
var contentEncoding string
|
|
|
|
if EnableGzip {
|
|
|
|
contentEncoding = getAcceptEncodingZip(ctx.Request)
|
|
|
|
}
|
|
|
|
|
|
|
|
memzipfile, err := openMemZipFile(file, contentEncoding)
|
|
|
|
if err != nil {
|
2014-06-10 17:11:32 +00:00
|
|
|
return
|
2014-04-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if contentEncoding == "gzip" {
|
|
|
|
ctx.Output.Header("Content-Encoding", "gzip")
|
|
|
|
} else if contentEncoding == "deflate" {
|
|
|
|
ctx.Output.Header("Content-Encoding", "deflate")
|
|
|
|
} else {
|
|
|
|
ctx.Output.Header("Content-Length", strconv.FormatInt(finfo.Size(), 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
http.ServeContent(ctx.ResponseWriter, ctx.Request, file, finfo.ModTime(), memzipfile)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
http.ServeFile(ctx.ResponseWriter, ctx.Request, file)
|
|
|
|
}
|
2014-06-10 17:11:32 +00:00
|
|
|
return
|
2014-04-05 16:18:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|