1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-29 18:21:29 +00:00

encoding should be specify

This commit is contained in:
JessonChan 2015-11-10 11:20:13 +08:00
parent 0bc70e88f0
commit 891be34fc6

View File

@ -23,7 +23,7 @@ import (
"strconv"
"strings"
"gopkg.in/bufio.v1"
"bytes"
)
// WriteFile reads from file and writes to writer by the specific encoding(gzip/deflate)
@ -35,7 +35,7 @@ func WriteFile(encoding string, writer io.Writer, file *os.File) (bool, string,
// WriteBody reads writes content to writer by the specific encoding(gzip/deflate)
func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string, error) {
return writeLevel(encoding, writer, bufio.NewBuffer(content), flate.BestSpeed)
return writeLevel(encoding, writer, bytes.NewBuffer(content), flate.BestSpeed)
}
// writeLevel reads from reader,writes to writer by specific encoding and compress level
@ -84,6 +84,15 @@ type q struct {
value float64
}
var (
encodingMap = map[string]string{ // all the other compress methods will ignore
"gzip": "gzip",
"deflate": "deflate",
"*": "gzip", // * means any compress will accept,we prefer gzip
"identity": "", // identity means none-compress
}
)
func parseEncoding(r *http.Request) string {
acceptEncoding := r.Header.Get("Accept-Encoding")
if acceptEncoding == "" {
@ -110,11 +119,5 @@ func parseEncoding(r *http.Request) string {
}
}
}
if lastQ.name == "*" {
return "gzip"
}
if lastQ.name == "identity" {
return ""
}
return lastQ.name
return encodingMap[lastQ.name]
}