forked from CAFxX/httpcompression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefer.go
74 lines (68 loc) · 2.35 KB
/
prefer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package httpcompression
import (
"fmt"
"sort"
)
// Prefer controls the behavior of the middleware in case both Gzip and Brotli
// can be used to compress a response (i.e. in case the client supports both
// encodings, and the MIME type of the response is allowed for both encodings).
// See the comments on the PreferType constants for the supported values.
func Prefer(prefer PreferType) Option {
return func(c *config) error {
switch prefer {
case PreferServer, PreferClient:
c.prefer = prefer
return nil
default:
return fmt.Errorf("unknown prefer type: %v", prefer)
}
}
}
// PreferType allows to control the choice of compression algorithm when
// multiple algorithms are allowed by both client and server.
type PreferType byte
const (
// PreferServer prefers compressors in the order specified on the server.
// If two or more compressors have the same priority on the server, the client preference is taken into consideration.
// If both server and client do no specify a preference between two or more compressors, the order is determined by the name of the encoding.
// PreferServer is the default.
PreferServer PreferType = iota
// PreferClient prefers compressors in the order specified by the client.
// If two or more compressors have the same priority according to the client, the server priority is taken into consideration.
// If both server and client do no specify a preference between two or more compressors, the order is determined by the name of the encoding.
PreferClient
)
func preferredEncoding(accept codings, comps comps, common []string, prefer PreferType) string {
if len(common) == 0 {
panic("no common encoding")
}
switch prefer {
case PreferServer:
sort.Slice(common, func(i, j int) bool {
ci, cj := comps[common[i]].priority, comps[common[j]].priority
if ci != cj {
return ci > cj // desc
}
ai, aj := accept[common[i]], accept[common[j]]
if ai != aj {
return ai > aj // desc
}
return common[i] < common[j] // asc
})
case PreferClient:
sort.Slice(common, func(i, j int) bool {
ai, aj := accept[common[i]], accept[common[j]]
if ai != aj {
return ai > aj // desc
}
ci, cj := comps[common[i]].priority, comps[common[j]].priority
if ci != cj {
return ci > cj // desc
}
return common[i] < common[j] // asc
})
default:
panic("unknown prefer type")
}
return common[0]
}