mirror of
https://github.com/astaxie/beego.git
synced 2025-02-16 22:47:02 +00:00
refactor to fix encoder type bug
This commit is contained in:
parent
7ccf049a50
commit
39e29caf9b
@ -44,8 +44,8 @@ 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
|
||||||
if cf, ok := encodingCompressMap[encoding]; ok {
|
if cf, ok := encoderMap[encoding]; ok {
|
||||||
outputWriter, err = cf(writer, level)
|
outputWriter, err = cf.encode(writer, level)
|
||||||
} else {
|
} else {
|
||||||
encoding = ""
|
encoding = ""
|
||||||
outputWriter, err = noneCompress(writer, level)
|
outputWriter, err = noneCompress(writer, level)
|
||||||
@ -78,7 +78,6 @@ 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) {
|
func noneCompress(wr io.Writer, level int) (io.Writer, error) {
|
||||||
return wr, nil
|
return wr, nil
|
||||||
@ -90,12 +89,17 @@ func deflateCompress(wr io.Writer, level int) (io.Writer, error) {
|
|||||||
return flate.NewWriter(wr, level)
|
return flate.NewWriter(wr, level)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type acceptEncoder struct {
|
||||||
|
name string
|
||||||
|
encode func(io.Writer, int) (io.Writer, error)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
encodingCompressMap = map[string]compressFunc{ // all the other compress methods will ignore
|
encoderMap = map[string]acceptEncoder{ // all the other compress methods will ignore
|
||||||
"gzip": gzipCompress,
|
"gzip": acceptEncoder{"gzip", gzipCompress},
|
||||||
"deflate": deflateCompress,
|
"deflate": acceptEncoder{"deflate", deflateCompress},
|
||||||
"*": gzipCompress, // * means any compress will accept,we prefer gzip
|
"*": acceptEncoder{"gzip", gzipCompress}, // * means any compress will accept,we prefer gzip
|
||||||
"identity": noneCompress, // identity means none-compress
|
"identity": acceptEncoder{"", noneCompress}, // identity means none-compress
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -125,8 +129,8 @@ func parseEncoding(r *http.Request) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, ok := encodingCompressMap[lastQ.name]; ok {
|
if cf, ok := encoderMap[lastQ.name]; ok {
|
||||||
return lastQ.name
|
return cf.name
|
||||||
} else {
|
} else {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -36,10 +36,10 @@ func Test_ExtractEncoding(t *testing.T) {
|
|||||||
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": []string{"gzip;q=0,deflate"}}}) != "deflate" {
|
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": []string{"gzip;q=0,deflate"}}}) != "deflate" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": []string{"deflate;q=0.5,gzip;q=0.5,identity"}}}) != "identity" {
|
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": []string{"deflate;q=0.5,gzip;q=0.5,identity"}}}) != "" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": []string{"*"}}}) != "*" {
|
if parseEncoding(&http.Request{Header: map[string][]string{"Accept-Encoding": []string{"*"}}}) != "gzip" {
|
||||||
t.Fail()
|
t.Fail()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user