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
8f421936
Commit
8f421936
authored
Nov 10, 2015
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better compress func design
parent
891be34f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
15 deletions
+25
-15
acceptencoder.go
acceptencoder/acceptencoder.go
+25
-15
No files found.
acceptencoder/acceptencoder.go
View file @
8f421936
...
...
@@ -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
)
{
var
outputWriter
io
.
Writer
var
err
error
switch
encoding
{
case
"gzip"
:
outputWriter
,
err
=
gzip
.
NewWriterLevel
(
writer
,
level
)
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
if
cf
,
ok
:=
encodingCompressMap
[
encoding
];
ok
{
outputWriter
,
err
=
cf
(
writer
,
level
)
}
else
{
encoding
=
""
outputWriter
=
writer
.
(
io
.
Writer
)
outputWriter
,
err
=
noneCompress
(
writer
,
level
)
}
if
err
!=
nil
{
return
false
,
""
,
err
...
...
@@ -83,13 +78,24 @@ 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
}
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
(
encoding
Map
=
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
encoding
CompressMap
=
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
}
)
...
...
@@ -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
""
}
}
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