mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 20:51:29 +00:00
code bug fixed
This commit is contained in:
parent
b9fb3a62f5
commit
dc38b324e0
@ -31,81 +31,98 @@ 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 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
|
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)
|
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) {
|
|
||||||
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
|
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 {
|
||||||
|
http.ServeFile(ctx.ResponseWriter, ctx.Request, filePath)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if isStaticFileToCompress {
|
//to compress file
|
||||||
var contentEncoding string
|
var contentEncoding string
|
||||||
if EnableGzip {
|
if EnableGzip {
|
||||||
contentEncoding = getAcceptEncodingZip(ctx.Request)
|
contentEncoding = getAcceptEncodingZip(ctx.Request)
|
||||||
}
|
}
|
||||||
|
|
||||||
memzipfile, err := openMemZipFile(file, contentEncoding)
|
memZipFile, err := openMemZipFile(filePath, contentEncoding)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if RunMode == "dev" {
|
||||||
|
Warn("Can't compress the file:", filePath, err)
|
||||||
|
}
|
||||||
|
http.NotFound(ctx.ResponseWriter, ctx.Request)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,15 +131,10 @@ func serverStaticRouter(ctx *context.Context) {
|
|||||||
} else if contentEncoding == "deflate" {
|
} else if contentEncoding == "deflate" {
|
||||||
ctx.Output.Header("Content-Encoding", "deflate")
|
ctx.Output.Header("Content-Encoding", "deflate")
|
||||||
} else {
|
} 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)
|
http.ServeContent(ctx.ResponseWriter, ctx.Request, filePath, fileInfo.ModTime(), memZipFile)
|
||||||
|
|
||||||
} else {
|
|
||||||
http.ServeFile(ctx.ResponseWriter, ctx.Request, file)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user