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