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.
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
10 // This file defines the raw protocol and some utilities used by the child and
24 typeBeginRequest = iota + 1
37 // keep the connection between web-server and responder open after request
38 const flagKeepConn = 1
41 maxWrite = 65535 // maximum record body
46 roleResponder = iota + 1 // only Responders are implemented.
52 statusRequestComplete = iota
69 type beginRequest struct {
75 func (br *beginRequest) read(content []byte) os.Error {
76 if len(content) != 8 {
77 return os.NewError("fcgi: invalid begin request record")
79 br.role = binary.BigEndian.Uint16(content)
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
88 func (h *header) init(recType uint8, reqId uint16, contentLength int) {
92 h.ContentLength = uint16(contentLength)
93 h.PaddingLength = uint8(-contentLength & 7)
96 // conn sends records over rwc
99 rwc io.ReadWriteCloser
101 // to avoid allocations
106 func newConn(rwc io.ReadWriteCloser) *conn {
107 return &conn{rwc: rwc}
110 func (c *conn) Close() os.Error {
112 defer c.mutex.Unlock()
118 buf [maxWrite + maxPad]byte
121 func (rec *record) read(r io.Reader) (err os.Error) {
122 if err = binary.Read(r, binary.BigEndian, &rec.h); err != nil {
125 if rec.h.Version != 1 {
126 return os.NewError("fcgi: invalid header version")
128 n := int(rec.h.ContentLength) + int(rec.h.PaddingLength)
129 if _, err = io.ReadFull(r, rec.buf[:n]); err != nil {
135 func (r *record) content() []byte {
136 return r.buf[:r.h.ContentLength]
139 // writeRecord writes and sends a single record.
140 func (c *conn) writeRecord(recType uint8, reqId uint16, b []byte) os.Error {
142 defer c.mutex.Unlock()
144 c.h.init(recType, reqId, len(b))
145 if err := binary.Write(&c.buf, binary.BigEndian, c.h); err != nil {
148 if _, err := c.buf.Write(b); err != nil {
151 if _, err := c.buf.Write(pad[:c.h.PaddingLength]); err != nil {
154 _, err := c.rwc.Write(c.buf.Bytes())
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[:])
163 func (c *conn) writeEndRequest(reqId uint16, appStatus int, protocolStatus uint8) os.Error {
165 binary.BigEndian.PutUint32(b, uint32(appStatus))
166 b[4] = protocolStatus
167 return c.writeRecord(typeEndRequest, reqId, b)
170 func (c *conn) writePairs(recType uint8, reqId uint16, pairs map[string]string) os.Error {
171 w := newWriter(c, recType, reqId)
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 {
179 if _, err := w.WriteString(k); err != nil {
182 if _, err := w.WriteString(v); err != nil {
190 func readSize(s []byte) (uint32, int) {
194 size, n := uint32(s[0]), 1
195 if size&(1<<7) != 0 {
200 size = binary.BigEndian.Uint32(s)
206 func readString(s []byte, size uint32) string {
207 if size > uint32(len(s)) {
210 return string(s[:size])
213 func encodeSize(b []byte, size uint32) int {
216 binary.BigEndian.PutUint32(b, size)
223 // bufWriter encapsulates bufio.Writer but also closes the underlying stream when
225 type bufWriter struct {
230 func (w *bufWriter) Close() os.Error {
231 if err := w.Writer.Flush(); err != nil {
235 return w.closer.Close()
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}
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 {
252 func (w *streamWriter) Write(p []byte) (int, os.Error) {
259 if err := w.c.writeRecord(w.recType, w.reqId, p[:n]); err != nil {
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)