-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilters.go
169 lines (140 loc) · 4.03 KB
/
filters.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package webpipes
import "compress/gzip"
import "compress/flate"
import "io"
import "net/http"
import "strconv"
import "strings"
func rot13(b byte) byte {
if 'a' <= b && b <= 'z' {
b = 'a' + ((b-'a')+13)%26
}
if 'A' <= b && b <= 'Z' {
b = 'A' + ((b-'A')+13)%26
}
return b
}
type rot13Reader struct {
source io.Reader
}
func (r13 *rot13Reader) Read(b []byte) (ret int, err error) {
r, e := r13.source.Read(b)
for i := 0; i < r; i++ {
b[i] = rot13(b[i])
}
return r, e
}
// Rot13 any alphabetic content in the output stream
var Rot13Filter Filter = func(conn *Conn, req *http.Request, reader io.ReadCloser, writer io.WriteCloser) bool {
go func() {
rot13 := &rot13Reader{reader}
io.Copy(writer, rot13)
writer.Close()
reader.Close()
}()
return true
}
// Perform an identity transformation on the content stream
var IdentityFilter Filter = func(conn *Conn, req *http.Request, reader io.ReadCloser, writer io.WriteCloser) bool {
go func() {
io.Copy(writer, reader)
writer.Close()
reader.Close()
}()
return true
}
//////////////////////////////////////////////////////////////////////////////
// Compression components
// This component checks the Accept-Encoding header, and the HTTP 1.0
// equivalents to determine if compression is possible, and utilizes
// whichever has the highest qval. If compression is not possible, then
// control is passed to the next pipe in the pipeline
var CompressionPipe Pipe = func(conn *Conn, req *http.Request) bool {
// TODO: Support HTTP/1.0
if req.ProtoAtLeast(1, 1) {
// Process "Accept-Encoding" to see if any compressions are valid
header := req.Header.Get("Accept-Encoding")
if header == "" {
// No accepted encodings, must use plain text
return true
}
// Generate a map from encodingType -> qvalue (hardcoded to 1.0)
encMap := make(map[string]float64)
encValues := strings.Split(header, ",")
for _, encValue := range encValues {
var qVal string
// Just shamelessly strip the qvalue, as the spec is confusing atm.
if idx := strings.Index(encValue, ";"); idx != -1 {
encValue = encValue[0:idx]
if qidx := strings.Index(encValue, "q="); idx != -1 {
qVal = encValue[qidx:]
}
}
var encType string = strings.ToLower(strings.TrimSpace(encValue))
var quality float64
if qnum, err := strconv.ParseFloat(qVal, 64); err != nil && len(qVal) > 0 {
quality = qnum
} else {
// Default to 1.0 quality when not specified
quality = 1.0
}
encMap[encType] = quality
}
// Check to see if we can deflate/gzip the output
var filter Filter
gz, gzok := encMap["gzip"]
fl, flok := encMap["deflate"]
if gzok && flok {
if gz >= fl {
filter = GzipFilter
} else {
filter = FlateFilter
}
} else if gzok {
filter = GzipFilter
} else if flok {
filter = FlateFilter
}
if filter != nil {
// Grab a content reader and writer for the filter function
reader := conn.NewContentReader()
writer := conn.NewContentWriter()
if reader == nil || writer == nil {
// TODO: Output a message to the error log
conn.HTTPStatusResponse(http.StatusInternalServerError)
return true
}
return filter(conn, req, reader, writer)
}
}
// Nothing to be done
return true
}
// Perform unconditional 'gzip' compression of the content stream
var GzipFilter Filter = func(conn *Conn, req *http.Request, reader io.ReadCloser, writer io.WriteCloser) bool {
zipw := gzip.NewWriter(writer)
conn.SetHeader("Content-Encoding", "gzip")
go func() {
io.Copy(zipw, reader)
zipw.Close()
reader.Close()
writer.Close()
}()
return true
}
// Perform unconditional 'flate' compression of the content stream
var FlateFilter Filter = func(conn *Conn, req *http.Request, reader io.ReadCloser, writer io.WriteCloser) bool {
zipw, err := flate.NewWriter(writer, flate.DefaultCompression)
if err != nil {
conn.HTTPStatusResponse(http.StatusInternalServerError)
return true
}
conn.SetHeader("Content-Encoding", "deflate")
go func() {
io.Copy(zipw, reader)
zipw.Close()
reader.Close()
writer.Close()
}()
return true
}