forked from hoisie/web
/
fcgi.go
201 lines (172 loc) · 4.3 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
package web
import (
"bytes"
"bufio"
"encoding/binary"
"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
}
type fcgiEndRequest struct {
appStatus uint32
protocolStatus uint8
reserved [3]uint8
}
func (er fcgiEndRequest) bytes() []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint32(buf, er.appStatus)
buf[4] = er.protocolStatus
return buf
}
type fcgiConn struct {
requestId uint16
fd net.Conn
}
func (conn *fcgiConn) WriteString(s string) os.Error {
content := bytes.NewBufferString(s).Bytes()
l := len(content)
// 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)),
}
conn.fd.Write(hdr.bytes())
conn.fd.Write(content)
conn.fd.Write(padding)
return nil
}
func (conn *fcgiConn) StartResponse(status int) os.Error {
return nil
}
func (conn *fcgiConn) Close() os.Error {
content := fcgiEndRequest{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)
conn.fd.Close()
return nil
}
func readFcgiParams(data []byte) {
var params = make(map[string]string)
for idx := 0; len(data) > idx; {
var keySize int = int(data[idx])
if keySize>>7 == 0 {
idx += 1
} else {
binary.Read(bytes.NewBuffer(data[idx:idx+4]), binary.BigEndian, &keySize)
idx += 4
}
var valSize int = int(data[idx])
if valSize>>7 == 0 {
idx += 1
} else {
binary.Read(bytes.NewBuffer(data[idx:idx+4]), binary.BigEndian, &valSize)
idx += 4
}
key := data[idx : idx+keySize]
idx += keySize
val := data[idx : idx+valSize]
idx += valSize
println(string(key), ":", string(val))
params[string(key)] = string(val)
}
}
func handleFcgiRequest(fd net.Conn) {
br := bufio.NewReader(fd)
var fc *fcgiConn
for {
var h fcgiHeader
err := binary.Read(br, binary.BigEndian, &h)
if err != nil {
log.Stderrf(err.String())
break
}
content := make([]byte, h.ContentLength)
n, err := br.Read(content)
//read padding
if h.PaddingLength > 0 {
padding := make([]byte, h.PaddingLength)
n, err = br.Read(padding)
println("read padding", n)
}
switch h.Type {
case FcgiBeginRequest:
fc = &fcgiConn{h.RequestId, fd}
case FcgiParams:
readFcgiParams(content)
case FcgiStdin:
fc.WriteString("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello")
fc.Close()
case FcgiData:
println("data!")
case FcgiAbortRequest:
println("abort!")
}
}
}
func listenAndServeFcgi(addr string) {
println("listening and serving scgi!")
l, err := net.Listen("tcp", addr)
if err != nil {
log.Stderrf(err.String())
return
}
for {
fd, err := l.Accept()
if err != nil {
log.Stderrf(err.String())
break
}
go handleFcgiRequest(fd)
}
}