Skip to content

Commit

Permalink
add cache for gzip compress result
Browse files Browse the repository at this point in the history
  • Loading branch information
Lqlsoftware committed Apr 25, 2018
1 parent 53c3847 commit de4f1a8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 16 deletions.
57 changes: 41 additions & 16 deletions src/gopcap/http/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,59 @@ import (
// Default GET method
// root/URL
func DefaultGETHandler(request *HttpRequest, response *HttpResponse) {
dat, err := ioutil.ReadFile("root" + *request.url)
if err != nil {
response.stateCode = NotFound
response.contents = []byte("<html>ERROR 404!</html>")
return
}
isGzip := false
cachePath := "root/_temp" + *request.url

// gzip 压缩
// 检查支持 gzip 压缩
if encoding,ok := (*request.header)["Accept-Encoding"];ok {
encodes := strings.Split(encoding, ", ")
// 检查浏览器是否支持gzip压缩
for _,v := range encodes {
// 支持gzip压缩
if v == "gzip" {
// 压缩数据
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(dat)
w.Flush()
dat = b.Bytes()

// 设置返回header 通知浏览器压缩格式
(*response.header)["Content-Encoding"] = "gzip"
isGzip = true
break
}
}
}

// 检查缓存是否有已压缩文件
var dat []byte
var err error
if isGzip && checkFileIsExist(cachePath) {
// 设置返回header 通知浏览器压缩格式
(*response.header)["Content-Encoding"] = "gzip"

// 直接返回缓存数据
dat,err = ioutil.ReadFile(cachePath)
} else {
// 读入文件
dat, err = ioutil.ReadFile("root" + *request.url)
if err != nil {
response.stateCode = NotFound
response.contents = []byte("<html>ERROR 404!</html>")
return
}

// gzip 压缩
if isGzip {
// 设置返回header 通知浏览器压缩格式
(*response.header)["Content-Encoding"] = "gzip"

// 压缩数据
var b bytes.Buffer
w := gzip.NewWriter(&b)
w.Write(dat)
w.Flush()
dat = b.Bytes()

// 检查是否为静态text类文件
if checkType(*request.url) {
// 缓存
ioutil.WriteFile(cachePath, dat, 0666)
}
}
}
// 设置Content-Type
(*response.header)["Content-Type"] = getContentType(*request.url) + "; charset=utf-8"
response.stateCode = OK
Expand Down
19 changes: 19 additions & 0 deletions src/gopcap/http/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package http

import (
"os"
"regexp"
)

func checkFileIsExist(filename string) bool {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}

func checkType(url string) bool {
ct := getContentType(url)
ok,_ := regexp.MatchString("^text/|application/\\d*\\D*$", ct)
return ok
}

0 comments on commit de4f1a8

Please sign in to comment.