mirror of
https://github.com/astaxie/beego.git
synced 2024-11-26 02:41:29 +00:00
better compress func design
This commit is contained in:
parent
891be34fc6
commit
8f42193610
@ -44,16 +44,11 @@ func WriteBody(encoding string, writer io.Writer, content []byte) (bool, string,
|
|||||||
func writeLevel(encoding string, writer io.Writer, reader io.Reader, level int) (bool, string, error) {
|
func writeLevel(encoding string, writer io.Writer, reader io.Reader, level int) (bool, string, error) {
|
||||||
var outputWriter io.Writer
|
var outputWriter io.Writer
|
||||||
var err error
|
var err error
|
||||||
switch encoding {
|
if cf, ok := encodingCompressMap[encoding]; ok {
|
||||||
case "gzip":
|
outputWriter, err = cf(writer, level)
|
||||||
outputWriter, err = gzip.NewWriterLevel(writer, level)
|
} else {
|
||||||
case "deflate":
|
|
||||||
outputWriter, err = flate.NewWriter(writer, level)
|
|
||||||
default:
|
|
||||||
// all the other compress methods will ignore
|
|
||||||
// such as the deprecated compress and chrome-only sdch
|
|
||||||
encoding = ""
|
encoding = ""
|
||||||
outputWriter = writer.(io.Writer)
|
outputWriter, err = noneCompress(writer, level)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, "", err
|
return false, "", err
|
||||||
@ -83,13 +78,24 @@ type q struct {
|
|||||||
name string
|
name string
|
||||||
value float64
|
value float64
|
||||||
}
|
}
|
||||||
|
type compressFunc func(io.Writer, int) (io.Writer, error)
|
||||||
|
|
||||||
|
func noneCompress(wr io.Writer, level int) (io.Writer, error) {
|
||||||
|
return wr, nil
|
||||||
|
}
|
||||||
|
func gzipCompress(wr io.Writer, level int) (io.Writer, error) {
|
||||||
|
return gzip.NewWriterLevel(wr, level)
|
||||||
|
}
|
||||||
|
func deflateCompress(wr io.Writer, level int) (io.Writer, error) {
|
||||||
|
return flate.NewWriter(wr, level)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
encodingMap = map[string]string{ // all the other compress methods will ignore
|
encodingCompressMap = map[string]compressFunc{ // all the other compress methods will ignore
|
||||||
"gzip": "gzip",
|
"gzip": gzipCompress,
|
||||||
"deflate": "deflate",
|
"deflate": deflateCompress,
|
||||||
"*": "gzip", // * means any compress will accept,we prefer gzip
|
"*": gzipCompress, // * means any compress will accept,we prefer gzip
|
||||||
"identity": "", // identity means none-compress
|
"identity": noneCompress, // identity means none-compress
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -119,5 +125,9 @@ func parseEncoding(r *http.Request) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return encodingMap[lastQ.name]
|
if _, ok := encodingCompressMap[lastQ.name]; ok {
|
||||||
|
return lastQ.name
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user