forked from hoisie/web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fcgi.go
295 lines (255 loc) · 6.57 KB
/
fcgi.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package web
import (
"bytes"
"bufio"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"os"
)
const (
fcgiBeginRequest = iota + 1
fcgiAbortRequest
fcgiEndRequest
fcgiParams
fcgiStdin
fcgiStdout
fcgiStderr
fcgiData
fcgiGetValues
fcgiGetValuesResult
fcgiUnknownType
fcgiMaxType = fcgiUnknownType
)
const (
fcgiRequestComplete = iota
fcgiCantMpxConn
fcgiOverloaded
fcgiUnknownRole
)
type fcgiHeader struct {
Version uint8
Type uint8
RequestId uint16
ContentLength uint16
PaddingLength uint8
Reserved uint8
}
func (h fcgiHeader) bytes() []byte {
order := binary.BigEndian
buf := make([]byte, 8)
buf[0] = h.Version
buf[1] = h.Type
order.PutUint16(buf[2:4], h.RequestId)
order.PutUint16(buf[4:6], h.ContentLength)
buf[6] = h.PaddingLength
buf[7] = h.Reserved
return buf
}
func newFcgiRecord(typ int, requestId int, data []byte) []byte {
var record bytes.Buffer
l := len(data)
// round to the nearest 8
padding := make([]byte, uint8(-l&7))
hdr := fcgiHeader{
Version: 1,
Type: uint8(typ),
RequestId: uint16(requestId),
ContentLength: uint16(l),
PaddingLength: uint8(len(padding)),
}
//write the header
record.Write(hdr.bytes())
record.Write(data)
record.Write(padding)
return record.Bytes()
}
type fcgiEndReq struct {
appStatus uint32
protocolStatus uint8
reserved [3]uint8
}
func (er fcgiEndReq) bytes() []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint32(buf, er.appStatus)
buf[4] = er.protocolStatus
return buf
}
type fcgiConn struct {
requestId uint16
fd io.ReadWriteCloser
headers map[string][]string
wroteHeaders bool
}
func (conn *fcgiConn) fcgiWrite(data []byte) (err os.Error) {
l := len(data)
// round to the nearest 8
padding := make([]byte, uint8(-l&7))
hdr := fcgiHeader{
Version: 1,
Type: fcgiStdout,
RequestId: conn.requestId,
ContentLength: uint16(l),
PaddingLength: uint8(len(padding)),
}
//write the header
hdrBytes := hdr.bytes()
_, err = conn.fd.Write(hdrBytes)
if err != nil {
return err
}
_, err = conn.fd.Write(data)
if err != nil {
return err
}
_, err = conn.fd.Write(padding)
if err != nil {
return err
}
return err
}
func (conn *fcgiConn) Write(data []byte) (n int, err os.Error) {
var buf bytes.Buffer
if !conn.wroteHeaders {
conn.wroteHeaders = true
for k, v := range conn.headers {
for _, i := range v {
buf.WriteString(k + ": " + i + "\r\n")
}
}
buf.WriteString("\r\n")
conn.fcgiWrite(buf.Bytes())
}
err = conn.fcgiWrite(data)
if err != nil {
return 0, err
}
return len(data), nil
}
func (conn *fcgiConn) StartResponse(status int) {
var buf bytes.Buffer
text := statusText[status]
fmt.Fprintf(&buf, "HTTP/1.1 %d %s\r\n", status, text)
conn.fcgiWrite(buf.Bytes())
}
func (conn *fcgiConn) SetHeader(hdr string, val string, unique bool) {
if _, contains := conn.headers[hdr]; !contains {
conn.headers[hdr] = []string{val}
return
}
if unique {
//just overwrite the first value
conn.headers[hdr][0] = val
} else {
newHeaders := make([]string, len(conn.headers)+1)
copy(newHeaders, conn.headers[hdr])
newHeaders[len(newHeaders)-1] = val
conn.headers[hdr] = newHeaders
}
}
func (conn *fcgiConn) complete() {
content := fcgiEndReq{appStatus: 200, protocolStatus: fcgiRequestComplete}.bytes()
l := len(content)
hdr := fcgiHeader{
Version: 1,
Type: fcgiEndRequest,
RequestId: uint16(conn.requestId),
ContentLength: uint16(l),
PaddingLength: 0,
}
conn.fd.Write(hdr.bytes())
conn.fd.Write(content)
}
func (conn *fcgiConn) Close() {}
func readFcgiParamSize(data []byte, index int) (int, int) {
var size int
var shift = 0
if data[index]>>7 == 0 {
size = int(data[index])
shift = 1
} else {
var s uint32
binary.Read(bytes.NewBuffer(data[index:index+4]), binary.BigEndian, &s)
s ^= 1 << 31
size = int(s)
shift = 4
}
return size, shift
}
//read the fcgi parameters contained in data, and store them in storage
func readFcgiParams(data []byte, storage map[string]string) {
for idx := 0; len(data) > idx; {
keySize, shift := readFcgiParamSize(data, idx)
idx += shift
valSize, shift := readFcgiParamSize(data, idx)
idx += shift
key := data[idx : idx+keySize]
idx += keySize
val := data[idx : idx+valSize]
idx += valSize
storage[string(key)] = string(val)
}
}
func handleFcgiConnection(fd io.ReadWriteCloser) {
br := bufio.NewReader(fd)
var req *Request
var fc *fcgiConn
var body bytes.Buffer
headers := map[string]string{}
for {
var h fcgiHeader
err := binary.Read(br, binary.BigEndian, &h)
if err == os.EOF {
break
}
if err != nil {
log.Stderrf("FCGI Error", err.String())
break
}
content := make([]byte, h.ContentLength)
br.Read(content)
//read padding
if h.PaddingLength > 0 {
padding := make([]byte, h.PaddingLength)
br.Read(padding)
}
switch h.Type {
case fcgiBeginRequest:
fc = &fcgiConn{h.RequestId, fd, make(map[string][]string), false}
case fcgiParams:
if h.ContentLength > 0 {
readFcgiParams(content, headers)
}
case fcgiStdin:
if h.ContentLength > 0 {
body.Write(content)
} else if h.ContentLength == 0 {
req = newRequestCgi(headers, &body)
routeHandler(req, fc)
fc.complete()
}
case fcgiData:
if h.ContentLength > 0 {
body.Write(content)
}
case fcgiAbortRequest:
}
}
}
func listenAndServeFcgi(addr string) {
l, err := net.Listen("tcp", addr)
if err != nil {
log.Stderrf("FCGI listen error", err.String())
return
}
for {
fd, err := l.Accept()
if err != nil {
log.Stderrf("FCGI accept error", err.String())
break
}
go handleFcgiConnection(fd)
}
}