-
Notifications
You must be signed in to change notification settings - Fork 6
/
protocol.go
210 lines (192 loc) · 4.35 KB
/
protocol.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
package gofcgisrv
import (
"bytes"
"encoding/binary"
"errors"
"io"
)
type requestId uint16
type recordType uint8
const fcgiVersion byte = 1
// Request types.
const (
fcgiBeginRequest recordType = iota + 1
fcgiAbortRequest
fcgiEndRequest
fcgiParams
fcgiStdin
fcgiStdout
fcgiStderr
fcgiData
fcgiGetValues
fcgiGetValuesResult
fcgiUnknown
fcgiMaxtype = fcgiUnknown
)
// Roles
const (
fcgiResponder uint16 = iota + 1
fcgiAuthorizer
fcgiFilter
)
// ProtocolStatus
const (
fcgiRequestComplete uint8 = iota
fcgiCanMpxConn
fcgiOverloaded
fcgiUnknownRole
)
// Variable names
const (
fcgiMaxConns = "FCGI_MAX_CONNS"
fcgiMaxReqs = "FCGI_MAX_REQS"
fcgiMpxsConns = "FCGI_MPXS_CONNS"
)
var pad [7]byte
type record struct {
Type recordType
Id requestId
Content []byte
}
func writeRecord(w io.Writer, rec record) error {
clength := len(rec.Content)
if clength > 0xffff {
return errors.New("Content too large for record")
}
// Padding
plength := (-uint16(clength)) & 7
// Should we check for failure? Let's not bother for the first set, as
// we'll always be doing this into a buffer.
binary.Write(w, binary.BigEndian, fcgiVersion)
binary.Write(w, binary.BigEndian, rec.Type)
binary.Write(w, binary.BigEndian, rec.Id)
binary.Write(w, binary.BigEndian, uint16(clength))
binary.Write(w, binary.BigEndian, uint8(plength))
w.Write([]byte{0})
if _, err := w.Write(rec.Content); err != nil {
return err
}
if plength != 0 {
if _, err := w.Write(pad[:plength]); err != nil {
return err
}
}
return nil
}
func _read(r io.Reader, data interface{}) error {
return binary.Read(r, binary.BigEndian, data)
}
func readRecord(r io.Reader) (record, error) {
var rec record
var version byte
var clength uint16
var plength uint8
if err := _read(r, &version); err != nil {
return rec, err
} else if version != fcgiVersion {
return rec, errors.New("Unknown version")
}
if err := _read(r, &rec.Type); err != nil {
return rec, err
}
if err := _read(r, &rec.Id); err != nil {
return rec, err
}
if err := _read(r, &clength); err != nil {
return rec, err
}
if err := _read(r, &plength); err != nil {
return rec, err
}
// Skip one byte
if _, err := r.Read(pad[:1]); err != nil {
return rec, err
}
if clength != 0 {
rec.Content = make([]byte, clength)
if _, err := r.Read(rec.Content); err != nil {
return rec, err
}
}
if plength != 0 {
if _, err := r.Read(pad[:plength]); err != nil {
return rec, err
}
}
return rec, nil
}
func writePairLen(w io.Writer, val int) error {
if val <= 127 {
return binary.Write(w, binary.BigEndian, uint8(val))
}
return binary.Write(w, binary.BigEndian, uint32(val)|0x80000000)
}
func readPairLen(r io.Reader) (int, error) {
var buf [4]byte
if _, err := r.Read(buf[:1]); err != nil {
return 0, err
}
if buf[0] > 127 {
if _, err := r.Read(buf[1:]); err != nil {
return 0, err
}
buf[0] &= 0x7f
return int(binary.BigEndian.Uint32(buf[:])), nil
}
return int(buf[0]), nil
}
func writeNameValue(w io.Writer, name, value string) error {
buffer := bytes.NewBuffer(nil)
if err := writePairLen(buffer, len(name)); err != nil {
return err
}
if err := writePairLen(buffer, len(value)); err != nil {
return err
}
if _, err := io.WriteString(buffer, name); err != nil {
return err
}
if _, err := io.WriteString(buffer, value); err != nil {
return err
}
_, err := io.Copy(w, buffer)
return err
}
func readNameValue(r io.Reader) (name, value string, err error) {
nameLen, err := readPairLen(r)
if err != nil {
return name, value, err
}
valueLen, err := readPairLen(r)
if err != nil {
return name, value, err
}
if nameLen > 0 {
nameBytes := make([]byte, nameLen)
_, err = r.Read(nameBytes)
if err != nil {
return
}
name = string(nameBytes)
}
if valueLen > 0 {
valueBytes := make([]byte, valueLen)
_, err = r.Read(valueBytes)
if err != nil {
return
}
value = string(valueBytes)
}
return name, value, nil
}
func writeGetValues(w io.Writer, names ...string) error {
buffer := bytes.NewBuffer(nil)
for _, name := range names {
writeNameValue(buffer, name, "")
}
return writeRecord(w, record{fcgiGetValues, 0, buffer.Bytes()})
}
func writeBeginRequest(w io.Writer, reqId requestId, role uint16, flags byte) error {
content := [8]byte{byte(role >> 8), byte(role & 0xff), flags, 0, 0, 0, 0, 0}
return writeRecord(w, record{fcgiBeginRequest, reqId, content[:]})
}