Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
39e29caf
Commit
39e29caf
authored
Nov 10, 2015
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor to fix encoder type bug
parent
7ccf049a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
12 deletions
+16
-12
acceptencoder.go
acceptencoder/acceptencoder.go
+14
-10
acceptencoder_test.go
acceptencoder/acceptencoder_test.go
+2
-2
No files found.
acceptencoder/acceptencoder.go
View file @
39e29caf
...
...
@@ -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
)
{
var
outputWriter
io
.
Writer
var
err
error
if
cf
,
ok
:=
encod
ingCompress
Map
[
encoding
];
ok
{
outputWriter
,
err
=
cf
(
writer
,
level
)
if
cf
,
ok
:=
encod
er
Map
[
encoding
];
ok
{
outputWriter
,
err
=
cf
.
encode
(
writer
,
level
)
}
else
{
encoding
=
""
outputWriter
,
err
=
noneCompress
(
writer
,
level
)
...
...
@@ -78,7 +78,6 @@ type q struct {
name
string
value
float64
}
type
compressFunc
func
(
io
.
Writer
,
int
)
(
io
.
Writer
,
error
)
func
noneCompress
(
wr
io
.
Writer
,
level
int
)
(
io
.
Writer
,
error
)
{
return
wr
,
nil
...
...
@@ -90,12 +89,17 @@ func deflateCompress(wr io.Writer, level int) (io.Writer, error) {
return
flate
.
NewWriter
(
wr
,
level
)
}
type
acceptEncoder
struct
{
name
string
encode
func
(
io
.
Writer
,
int
)
(
io
.
Writer
,
error
)
}
var
(
encod
ingCompressMap
=
map
[
string
]
compressFunc
{
// all the other compress methods will ignore
"gzip"
:
gzipCompress
,
"deflate"
:
deflateCompress
,
"*"
:
gzipCompress
,
// * means any compress will accept,we prefer gzip
"identity"
:
noneCompress
,
// identity means none-compress
encod
erMap
=
map
[
string
]
acceptEncoder
{
// all the other compress methods will ignore
"gzip"
:
acceptEncoder
{
"gzip"
,
gzipCompress
}
,
"deflate"
:
acceptEncoder
{
"deflate"
,
deflateCompress
}
,
"*"
:
acceptEncoder
{
"gzip"
,
gzipCompress
}
,
// * means any compress will accept,we prefer gzip
"identity"
:
acceptEncoder
{
""
,
noneCompress
},
// identity means none-compress
}
)
...
...
@@ -125,8 +129,8 @@ func parseEncoding(r *http.Request) string {
}
}
}
if
_
,
ok
:=
encodingCompress
Map
[
lastQ
.
name
];
ok
{
return
lastQ
.
name
if
cf
,
ok
:=
encoder
Map
[
lastQ
.
name
];
ok
{
return
cf
.
name
}
else
{
return
""
}
...
...
acceptencoder/acceptencoder_test.go
View file @
39e29caf
...
...
@@ -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"
{
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
()
}
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
()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment