OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / http / fcgi / fcgi.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package fcgi implements the FastCGI protocol.
6 // Currently only the responder role is supported.
7 // The protocol is defined at http://www.fastcgi.com/drupal/node/6?q=node/22
8 package fcgi
9
10 // This file defines the raw protocol and some utilities used by the child and
11 // the host.
12
13 import (
14         "bufio"
15         "bytes"
16         "encoding/binary"
17         "io"
18         "os"
19         "sync"
20 )
21
22 const (
23         // Packet Types
24         typeBeginRequest = iota + 1
25         typeAbortRequest
26         typeEndRequest
27         typeParams
28         typeStdin
29         typeStdout
30         typeStderr
31         typeData
32         typeGetValues
33         typeGetValuesResult
34         typeUnknownType
35 )
36
37 // keep the connection between web-server and responder open after request
38 const flagKeepConn = 1
39
40 const (
41         maxWrite = 65535 // maximum record body
42         maxPad   = 255
43 )
44
45 const (
46         roleResponder = iota + 1 // only Responders are implemented.
47         roleAuthorizer
48         roleFilter
49 )
50
51 const (
52         statusRequestComplete = iota
53         statusCantMultiplex
54         statusOverloaded
55         statusUnknownRole
56 )
57
58 const headerLen = 8
59
60 type header struct {
61         Version       uint8
62         Type          uint8
63         Id            uint16
64         ContentLength uint16
65         PaddingLength uint8
66         Reserved      uint8
67 }
68
69 type beginRequest struct {
70         role     uint16
71         flags    uint8
72         reserved [5]uint8
73 }
74
75 func (br *beginRequest) read(content []byte) os.Error {
76         if len(content) != 8 {
77                 return os.NewError("fcgi: invalid begin request record")
78         }
79         br.role = binary.BigEndian.Uint16(content)
80         br.flags = content[2]
81         return nil
82 }
83
84 // for padding so we don't have to allocate all the time
85 // not synchronized because we don't care what the contents are
86 var pad [maxPad]byte
87
88 func (h *header) init(recType uint8, reqId uint16, contentLength int) {
89         h.Version = 1
90         h.Type = recType
91         h.Id = reqId
92         h.ContentLength = uint16(contentLength)
93         h.PaddingLength = uint8(-contentLength & 7)
94 }
95
96 // conn sends records over rwc
97 type conn struct {
98         mutex sync.Mutex
99         rwc   io.ReadWriteCloser
100
101         // to avoid allocations
102         buf bytes.Buffer
103         h   header
104 }
105
106 func newConn(rwc io.ReadWriteCloser) *conn {
107         return &conn{rwc: rwc}
108 }
109
110 func (c *conn) Close() os.Error {
111         c.mutex.Lock()
112         defer c.mutex.Unlock()
113         return c.rwc.Close()
114 }
115
116 type record struct {
117         h   header
118         buf [maxWrite + maxPad]byte
119 }
120
121 func (rec *record) read(r io.Reader) (err os.Error) {
122         if err = binary.Read(r, binary.BigEndian, &rec.h); err != nil {
123                 return err
124         }
125         if rec.h.Version != 1 {
126                 return os.NewError("fcgi: invalid header version")
127         }
128         n := int(rec.h.ContentLength) + int(rec.h.PaddingLength)
129         if _, err = io.ReadFull(r, rec.buf[:n]); err != nil {
130                 return err
131         }
132         return nil
133 }
134
135 func (r *record) content() []byte {
136         return r.buf[:r.h.ContentLength]
137 }
138
139 // writeRecord writes and sends a single record.
140 func (c *conn) writeRecord(recType uint8, reqId uint16, b []byte) os.Error {
141         c.mutex.Lock()
142         defer c.mutex.Unlock()
143         c.buf.Reset()
144         c.h.init(recType, reqId, len(b))
145         if err := binary.Write(&c.buf, binary.BigEndian, c.h); err != nil {
146                 return err
147         }
148         if _, err := c.buf.Write(b); err != nil {
149                 return err
150         }
151         if _, err := c.buf.Write(pad[:c.h.PaddingLength]); err != nil {
152                 return err
153         }
154         _, err := c.rwc.Write(c.buf.Bytes())
155         return err
156 }
157
158 func (c *conn) writeBeginRequest(reqId uint16, role uint16, flags uint8) os.Error {
159         b := [8]byte{byte(role >> 8), byte(role), flags}
160         return c.writeRecord(typeBeginRequest, reqId, b[:])
161 }
162
163 func (c *conn) writeEndRequest(reqId uint16, appStatus int, protocolStatus uint8) os.Error {
164         b := make([]byte, 8)
165         binary.BigEndian.PutUint32(b, uint32(appStatus))
166         b[4] = protocolStatus
167         return c.writeRecord(typeEndRequest, reqId, b)
168 }
169
170 func (c *conn) writePairs(recType uint8, reqId uint16, pairs map[string]string) os.Error {
171         w := newWriter(c, recType, reqId)
172         b := make([]byte, 8)
173         for k, v := range pairs {
174                 n := encodeSize(b, uint32(len(k)))
175                 n += encodeSize(b[n:], uint32(len(k)))
176                 if _, err := w.Write(b[:n]); err != nil {
177                         return err
178                 }
179                 if _, err := w.WriteString(k); err != nil {
180                         return err
181                 }
182                 if _, err := w.WriteString(v); err != nil {
183                         return err
184                 }
185         }
186         w.Close()
187         return nil
188 }
189
190 func readSize(s []byte) (uint32, int) {
191         if len(s) == 0 {
192                 return 0, 0
193         }
194         size, n := uint32(s[0]), 1
195         if size&(1<<7) != 0 {
196                 if len(s) < 4 {
197                         return 0, 0
198                 }
199                 n = 4
200                 size = binary.BigEndian.Uint32(s)
201                 size &^= 1 << 31
202         }
203         return size, n
204 }
205
206 func readString(s []byte, size uint32) string {
207         if size > uint32(len(s)) {
208                 return ""
209         }
210         return string(s[:size])
211 }
212
213 func encodeSize(b []byte, size uint32) int {
214         if size > 127 {
215                 size |= 1 << 31
216                 binary.BigEndian.PutUint32(b, size)
217                 return 4
218         }
219         b[0] = byte(size)
220         return 1
221 }
222
223 // bufWriter encapsulates bufio.Writer but also closes the underlying stream when
224 // Closed.
225 type bufWriter struct {
226         closer io.Closer
227         *bufio.Writer
228 }
229
230 func (w *bufWriter) Close() os.Error {
231         if err := w.Writer.Flush(); err != nil {
232                 w.closer.Close()
233                 return err
234         }
235         return w.closer.Close()
236 }
237
238 func newWriter(c *conn, recType uint8, reqId uint16) *bufWriter {
239         s := &streamWriter{c: c, recType: recType, reqId: reqId}
240         w, _ := bufio.NewWriterSize(s, maxWrite)
241         return &bufWriter{s, w}
242 }
243
244 // streamWriter abstracts out the separation of a stream into discrete records.
245 // It only writes maxWrite bytes at a time.
246 type streamWriter struct {
247         c       *conn
248         recType uint8
249         reqId   uint16
250 }
251
252 func (w *streamWriter) Write(p []byte) (int, os.Error) {
253         nn := 0
254         for len(p) > 0 {
255                 n := len(p)
256                 if n > maxWrite {
257                         n = maxWrite
258                 }
259                 if err := w.c.writeRecord(w.recType, w.reqId, p[:n]); err != nil {
260                         return nn, err
261                 }
262                 nn += n
263                 p = p[n:]
264         }
265         return nn, nil
266 }
267
268 func (w *streamWriter) Close() os.Error {
269         // send empty record to close the stream
270         return w.c.writeRecord(w.recType, w.reqId, nil)
271 }