1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 23:40:56 +00:00
Beego/staticfile.go

139 lines
3.8 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-04-05 16:18:21 +00:00
package beego
import (
"net/http"
"os"
"path"
"path/filepath"
2014-04-05 16:18:21 +00:00
"strconv"
"strings"
"github.com/astaxie/beego/context"
"github.com/astaxie/beego/utils"
)
2014-06-10 17:11:32 +00:00
func serverStaticRouter(ctx *context.Context) {
2014-08-04 07:31:27 +00:00
if ctx.Input.Method() != "GET" && ctx.Input.Method() != "HEAD" {
return
}
requestPath := filepath.Clean(ctx.Input.Request.URL.Path)
2015-09-22 04:19:31 +00:00
// special processing : favicon.ico/robots.txt can be in any static dir
if requestPath == "/favicon.ico" || requestPath == "/robots.txt" {
2015-09-22 05:46:20 +00:00
file := path.Join(".", requestPath)
if utils.FileExists(file) {
http.ServeFile(ctx.ResponseWriter, ctx.Request, file)
2015-09-22 04:19:31 +00:00
return
2014-04-05 16:18:21 +00:00
}
2015-09-22 04:19:31 +00:00
for _, staticDir := range StaticDir {
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-04-05 16:18:21 +00:00
}
2015-09-22 04:19:31 +00:00
}
http.NotFound(ctx.ResponseWriter, ctx.Request)
return
}
for prefix, staticDir := range StaticDir {
if len(prefix) == 0 {
2015-09-08 14:07:44 +00:00
continue
2014-04-05 16:18:21 +00:00
}
if strings.HasPrefix(requestPath, prefix) {
if len(requestPath) > len(prefix) && requestPath[len(prefix)] != '/' {
continue
}
2015-09-22 04:19:31 +00:00
filePath := path.Join(staticDir, requestPath[len(prefix):])
fileInfo, err := os.Stat(filePath)
2014-04-05 16:18:21 +00:00
if err != nil {
if RunMode == "dev" {
2015-09-22 04:19:31 +00:00
Warn("Can't find the file:", filePath, err)
2014-04-05 16:18:21 +00:00
}
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
2015-09-22 04:19:31 +00:00
if fileInfo.IsDir() {
if !DirectoryIndex {
2015-02-26 15:34:43 +00:00
exception("403", ctx)
return
2015-09-22 04:19:31 +00:00
}
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
}
2015-09-22 04:19:31 +00:00
}
if strings.HasSuffix(requestPath, "/index.html") {
2015-09-22 05:48:34 +00:00
fileReader, err := os.Open(filePath)
if err != nil {
if RunMode == "dev" {
Warn("Can't open the file:", filePath, err)
}
2015-09-22 05:48:34 +00:00
http.NotFound(ctx.ResponseWriter, ctx.Request)
return
}
2015-09-22 05:48:34 +00:00
defer fileReader.Close()
http.ServeContent(ctx.ResponseWriter, ctx.Request, filePath, fileInfo.ModTime(), fileReader)
return
2014-04-05 16:18:21 +00:00
}
isStaticFileToCompress := false
2015-09-22 04:19:31 +00:00
for _, statExtension := range StaticExtensionsToGzip {
if strings.HasSuffix(strings.ToLower(filePath), strings.ToLower(statExtension)) {
2015-09-22 04:19:31 +00:00
isStaticFileToCompress = true
break
2014-04-05 16:18:21 +00:00
}
}
2015-09-22 04:19:31 +00:00
if !isStaticFileToCompress {
http.ServeFile(ctx.ResponseWriter, ctx.Request, filePath)
return
}
2014-04-05 16:18:21 +00:00
2015-09-22 04:19:31 +00:00
//to compress file
var contentEncoding string
if EnableGzip {
contentEncoding = getAcceptEncodingZip(ctx.Request)
}
2014-04-05 16:18:21 +00:00
2015-09-22 04:19:31 +00:00
memZipFile, err := openMemZipFile(filePath, contentEncoding)
if err != nil {
if RunMode == "dev" {
Warn("Can't compress the file:", filePath, err)
2014-04-05 16:18:21 +00:00
}
2015-09-22 04:19:31 +00:00
http.NotFound(ctx.ResponseWriter, ctx.Request)
return
}
2014-04-05 16:18:21 +00:00
2015-09-22 04:19:31 +00:00
if contentEncoding == "gzip" {
ctx.Output.Header("Content-Encoding", "gzip")
} else if contentEncoding == "deflate" {
ctx.Output.Header("Content-Encoding", "deflate")
2014-04-05 16:18:21 +00:00
} else {
2015-09-22 04:19:31 +00:00
ctx.Output.Header("Content-Length", strconv.FormatInt(fileInfo.Size(), 10))
2014-04-05 16:18:21 +00:00
}
2015-09-22 04:19:31 +00:00
http.ServeContent(ctx.ResponseWriter, ctx.Request, filePath, fileInfo.ModTime(), memZipFile)
2015-09-22 06:15:41 +00:00
return
2014-04-05 16:18:21 +00:00
}
}
}