1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:30:56 +00:00

code bug fixed

This commit is contained in:
JessonChan 2015-09-22 12:19:31 +08:00
parent b9fb3a62f5
commit dc38b324e0

View File

@ -31,98 +31,110 @@ func serverStaticRouter(ctx *context.Context) {
return return
} }
requestPath := filepath.Clean(ctx.Input.Request.URL.Path) requestPath := filepath.Clean(ctx.Input.Request.URL.Path)
i := 0
for prefix, staticDir := range StaticDir { // special processing : favicon.ico/robots.txt can be in any static dir
if len(prefix) == 0 { if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
continue
if utils.FileExists("./" + requestPath) {
http.ServeFile(ctx.ResponseWriter, ctx.Request, "./"+requestPath)
return
} }
if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
for _, staticDir := range StaticDir {
file := path.Join(staticDir, requestPath) file := path.Join(staticDir, requestPath)
if utils.FileExists(file) { if utils.FileExists(file) {
http.ServeFile(ctx.ResponseWriter, ctx.Request, file) http.ServeFile(ctx.ResponseWriter, ctx.Request, file)
return return
} }
i++ }
if i == len(StaticDir) {
http.NotFound(ctx.ResponseWriter, ctx.Request) http.NotFound(ctx.ResponseWriter, ctx.Request)
return return
} }
for prefix, staticDir := range StaticDir {
if len(prefix) == 0 {
continue continue
} }
if strings.HasPrefix(requestPath, prefix) { if strings.HasPrefix(requestPath, prefix) {
if len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' { if len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' {
continue continue
} }
file := path.Join(staticDir, requestPath[len(prefix):]) filePath := path.Join(staticDir, requestPath[len(prefix):])
finfo, err := os.Stat(file) fileInfo, err := os.Stat(filePath)
if err != nil { if err != nil {
if RunMode == "dev" { if RunMode == "dev" {
Warn("Can't find the file:", file, err) Warn("Can't find the file:", filePath, err)
} }
http.NotFound(ctx.ResponseWriter, ctx.Request) http.NotFound(ctx.ResponseWriter, ctx.Request)
return return
} }
//if the request is dir and DirectoryIndex is false then //if the request is dir and DirectoryIndex is false then
if finfo.IsDir() { if fileInfo.IsDir() {
if !DirectoryIndex { if !DirectoryIndex {
exception("403", ctx) exception("403", ctx)
return return
} else if ctx.Input.Request.URL.Path[len(ctx.Input.Request.URL.Path)-1] != '/' { }
if ctx.Input.Request.URL.Path[len(ctx.Input.Request.URL.Path)-1] != '/' {
http.Redirect(ctx.ResponseWriter, ctx.Request, ctx.Input.Request.URL.Path+"/", 302) http.Redirect(ctx.ResponseWriter, ctx.Request, ctx.Input.Request.URL.Path+"/", 302)
return return
} }
} else if strings.HasSuffix(requestPath, "/index.html") { }
file := path.Join(staticDir, requestPath)
if utils.FileExists(file) { if strings.HasSuffix(requestPath, "/index.html") {
oFile, err := os.Open(file) if utils.FileExists(filePath) {
fileReader, err := os.Open(filePath)
if err != nil { if err != nil {
if RunMode == "dev" { if RunMode == "dev" {
Warn("Can't open the file:", file, err) Warn("Can't open the file:", filePath, err)
} }
http.NotFound(ctx.ResponseWriter, ctx.Request) http.NotFound(ctx.ResponseWriter, ctx.Request)
return
} }
defer oFile.Close() defer fileReader.Close()
http.ServeContent(ctx.ResponseWriter, ctx.Request, file, finfo.ModTime(), oFile) http.ServeContent(ctx.ResponseWriter, ctx.Request, filePath, fileInfo.ModTime(), fileReader)
return return
} }
} }
//This block obtained from (https://github.com/smithfox/beego) - it should probably get merged into astaxie/beego after a pull request
isStaticFileToCompress := false isStaticFileToCompress := false
if StaticExtensionsToGzip != nil && len(StaticExtensionsToGzip) > 0 { lowerFileName := strings.ToLower(filePath)
for _, statExtension := range StaticExtensionsToGzip { for _, statExtension := range StaticExtensionsToGzip {
if strings.HasSuffix(strings.ToLower(file), strings.ToLower(statExtension)) { if strings.HasSuffix(lowerFileName, statExtension) {
isStaticFileToCompress = true isStaticFileToCompress = true
break break
}
} }
} }
if isStaticFileToCompress { if !isStaticFileToCompress {
var contentEncoding string http.ServeFile(ctx.ResponseWriter, ctx.Request, filePath)
if EnableGzip { return
contentEncoding = getAcceptEncodingZip(ctx.Request) }
//to compress file
var contentEncoding string
if EnableGzip {
contentEncoding = getAcceptEncodingZip(ctx.Request)
}
memZipFile, err := openMemZipFile(filePath, contentEncoding)
if err != nil {
if RunMode == "dev" {
Warn("Can't compress the file:", filePath, err)
} }
http.NotFound(ctx.ResponseWriter, ctx.Request)
return
}
memzipfile, err := openMemZipFile(file, contentEncoding) if contentEncoding == "gzip" {
if err != nil { ctx.Output.Header("Content-Encoding", "gzip")
return } else if contentEncoding == "deflate" {
} ctx.Output.Header("Content-Encoding", "deflate")
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 { } else {
http.ServeFile(ctx.ResponseWriter, ctx.Request, file) ctx.Output.Header("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
} }
return
http.ServeContent(ctx.ResponseWriter, ctx.Request, filePath, fileInfo.ModTime(), memZipFile)
} }
} }
} }