-
Notifications
You must be signed in to change notification settings - Fork 704
/
scgi.go
183 lines (157 loc) · 3.56 KB
/
scgi.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
package web
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/http/cgi"
"strconv"
"strings"
)
type scgiBody struct {
reader io.Reader
conn io.ReadWriteCloser
closed bool
}
func (b *scgiBody) Read(p []byte) (n int, err error) {
if b.closed {
return 0, errors.New("SCGI read after close")
}
return b.reader.Read(p)
}
func (b *scgiBody) Close() error {
b.closed = true
return b.conn.Close()
}
type scgiConn struct {
fd io.ReadWriteCloser
req *http.Request
headers http.Header
wroteHeaders bool
}
func (conn *scgiConn) WriteHeader(status int) {
if !conn.wroteHeaders {
conn.wroteHeaders = true
var buf bytes.Buffer
text := http.StatusText(status)
fmt.Fprintf(&buf, "HTTP/1.1 %d %s\r\n", status, text)
for k, v := range conn.headers {
for _, i := range v {
buf.WriteString(k + ": " + i + "\r\n")
}
}
buf.WriteString("\r\n")
conn.fd.Write(buf.Bytes())
}
}
func (conn *scgiConn) Header() http.Header {
return conn.headers
}
func (conn *scgiConn) Write(data []byte) (n int, err error) {
if !conn.wroteHeaders {
conn.WriteHeader(200)
}
if conn.req.Method == "HEAD" {
return 0, errors.New("Body Not Allowed")
}
return conn.fd.Write(data)
}
func (conn *scgiConn) Close() { conn.fd.Close() }
func (conn *scgiConn) finishRequest() 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.fd.Write(buf.Bytes())
}
return nil
}
func (s *Server) readScgiRequest(fd io.ReadWriteCloser) (*http.Request, error) {
reader := bufio.NewReader(fd)
line, err := reader.ReadString(':')
if err != nil {
return nil, err
}
length, err := strconv.Atoi(line[0 : len(line)-1])
if err != nil {
return nil, err
}
if length > 16384 {
return nil, errors.New("Max header size is 16k")
}
headerData := make([]byte, length)
_, err = reader.Read(headerData)
if err != nil {
return nil, err
}
b, err := reader.ReadByte()
if err != nil {
return nil, err
}
// discard the trailing comma
if b != ',' {
return nil, errors.New("SCGI protocol error: missing comma")
}
headerList := bytes.Split(headerData, []byte{0})
headers := map[string]string{}
for i := 0; i < len(headerList)-1; i += 2 {
headers[string(headerList[i])] = string(headerList[i+1])
}
httpReq, err := cgi.RequestFromMap(headers)
if err != nil {
return nil, err
}
if httpReq.ContentLength > 0 {
httpReq.Body = &scgiBody{
reader: io.LimitReader(reader, httpReq.ContentLength),
conn: fd,
}
} else {
httpReq.Body = &scgiBody{reader: reader, conn: fd}
}
return httpReq, nil
}
func (s *Server) handleScgiRequest(fd io.ReadWriteCloser) {
defer fd.Close()
req, err := s.readScgiRequest(fd)
if err != nil {
s.Logger.Println("Error reading SCGI request: %q", err.Error())
return
}
sc := scgiConn{fd, req, make(map[string][]string), false}
s.routeHandler(req, &sc)
sc.finishRequest()
}
func (s *Server) listenAndServeScgi(addr string) error {
var l net.Listener
var err error
//if the path begins with a "/", assume it's a unix address
if strings.HasPrefix(addr, "/") {
l, err = net.Listen("unix", addr)
} else {
l, err = net.Listen("tcp", addr)
}
//save the listener so it can be closed
s.l = l
if err != nil {
s.Logger.Println("SCGI listen error", err.Error())
return err
}
for {
fd, err := l.Accept()
if err != nil {
s.Logger.Println("SCGI accept error", err.Error())
return err
}
go s.handleScgiRequest(fd)
}
return nil
}