2016-01-14 22:20:59 +00:00
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
2015-10-12 01:36:21 +00:00
|
|
|
// Use of this source code is governed by a BSD-style
|
2016-01-14 22:20:59 +00:00
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package fcgi implements the FastCGI protocol.
|
|
|
|
// Currently only the responder role is supported.
|
|
|
|
// The protocol is defined at http://www.fastcgi.com/drupal/node/6?q=node/22
|
|
|
|
package phpfpm
|
2015-10-12 01:36:21 +00:00
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// This file defines the raw protocol and some utilities used by the child and
|
|
|
|
// the host.
|
2015-10-12 01:36:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"io"
|
2016-01-14 22:20:59 +00:00
|
|
|
"sync"
|
2015-10-12 01:36:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// recType is a record type, as defined by
|
|
|
|
// http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S8
|
|
|
|
type recType uint8
|
2015-10-12 01:36:21 +00:00
|
|
|
|
|
|
|
const (
|
2016-01-14 22:20:59 +00:00
|
|
|
typeBeginRequest recType = 1
|
|
|
|
typeAbortRequest recType = 2
|
|
|
|
typeEndRequest recType = 3
|
|
|
|
typeParams recType = 4
|
|
|
|
typeStdin recType = 5
|
|
|
|
typeStdout recType = 6
|
|
|
|
typeStderr recType = 7
|
|
|
|
typeData recType = 8
|
|
|
|
typeGetValues recType = 9
|
|
|
|
typeGetValuesResult recType = 10
|
|
|
|
typeUnknownType recType = 11
|
2015-10-12 01:36:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// keep the connection between web-server and responder open after request
|
|
|
|
const flagKeepConn = 1
|
2015-10-12 01:36:21 +00:00
|
|
|
|
|
|
|
const (
|
2016-01-14 22:20:59 +00:00
|
|
|
maxWrite = 65535 // maximum record body
|
|
|
|
maxPad = 255
|
2015-10-12 01:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-01-14 22:20:59 +00:00
|
|
|
roleResponder = iota + 1 // only Responders are implemented.
|
|
|
|
roleAuthorizer
|
|
|
|
roleFilter
|
2015-10-12 01:36:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-01-14 22:20:59 +00:00
|
|
|
statusRequestComplete = iota
|
|
|
|
statusCantMultiplex
|
|
|
|
statusOverloaded
|
|
|
|
statusUnknownRole
|
2015-10-12 01:36:21 +00:00
|
|
|
)
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
const headerLen = 8
|
|
|
|
|
2015-10-12 01:36:21 +00:00
|
|
|
type header struct {
|
|
|
|
Version uint8
|
2016-01-14 22:20:59 +00:00
|
|
|
Type recType
|
2015-10-12 01:36:21 +00:00
|
|
|
Id uint16
|
|
|
|
ContentLength uint16
|
|
|
|
PaddingLength uint8
|
|
|
|
Reserved uint8
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
type beginRequest struct {
|
|
|
|
role uint16
|
|
|
|
flags uint8
|
|
|
|
reserved [5]uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *beginRequest) read(content []byte) error {
|
|
|
|
if len(content) != 8 {
|
|
|
|
return errors.New("fcgi: invalid begin request record")
|
|
|
|
}
|
|
|
|
br.role = binary.BigEndian.Uint16(content)
|
|
|
|
br.flags = content[2]
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-12 01:36:21 +00:00
|
|
|
// for padding so we don't have to allocate all the time
|
|
|
|
// not synchronized because we don't care what the contents are
|
|
|
|
var pad [maxPad]byte
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
func (h *header) init(recType recType, reqId uint16, contentLength int) {
|
2015-10-12 01:36:21 +00:00
|
|
|
h.Version = 1
|
|
|
|
h.Type = recType
|
|
|
|
h.Id = reqId
|
|
|
|
h.ContentLength = uint16(contentLength)
|
|
|
|
h.PaddingLength = uint8(-contentLength & 7)
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// conn sends records over rwc
|
|
|
|
type conn struct {
|
|
|
|
mutex sync.Mutex
|
|
|
|
rwc io.ReadWriteCloser
|
|
|
|
|
|
|
|
// to avoid allocations
|
|
|
|
buf bytes.Buffer
|
|
|
|
h header
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConn(rwc io.ReadWriteCloser) *conn {
|
|
|
|
return &conn{rwc: rwc}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Close() error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
return c.rwc.Close()
|
|
|
|
}
|
|
|
|
|
2015-10-12 01:36:21 +00:00
|
|
|
type record struct {
|
|
|
|
h header
|
|
|
|
buf [maxWrite + maxPad]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rec *record) read(r io.Reader) (err error) {
|
|
|
|
if err = binary.Read(r, binary.BigEndian, &rec.h); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rec.h.Version != 1 {
|
|
|
|
return errors.New("fcgi: invalid header version")
|
|
|
|
}
|
|
|
|
n := int(rec.h.ContentLength) + int(rec.h.PaddingLength)
|
|
|
|
if _, err = io.ReadFull(r, rec.buf[:n]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *record) content() []byte {
|
|
|
|
return r.buf[:r.h.ContentLength]
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
// writeRecord writes and sends a single record.
|
|
|
|
func (c *conn) writeRecord(recType recType, reqId uint16, b []byte) error {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
c.buf.Reset()
|
|
|
|
c.h.init(recType, reqId, len(b))
|
|
|
|
if err := binary.Write(&c.buf, binary.BigEndian, c.h); err != nil {
|
2015-10-12 01:36:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-14 22:20:59 +00:00
|
|
|
if _, err := c.buf.Write(b); err != nil {
|
2015-10-12 01:36:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-14 22:20:59 +00:00
|
|
|
if _, err := c.buf.Write(pad[:c.h.PaddingLength]); err != nil {
|
2015-10-12 01:36:21 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-01-14 22:20:59 +00:00
|
|
|
_, err := c.rwc.Write(c.buf.Bytes())
|
2015-10-12 01:36:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
func (c *conn) writeBeginRequest(reqId uint16, role uint16, flags uint8) error {
|
2015-10-12 01:36:21 +00:00
|
|
|
b := [8]byte{byte(role >> 8), byte(role), flags}
|
2016-01-14 22:20:59 +00:00
|
|
|
return c.writeRecord(typeBeginRequest, reqId, b[:])
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
func (c *conn) writeEndRequest(reqId uint16, appStatus int, protocolStatus uint8) error {
|
2015-10-12 01:36:21 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(b, uint32(appStatus))
|
|
|
|
b[4] = protocolStatus
|
2016-01-14 22:20:59 +00:00
|
|
|
return c.writeRecord(typeEndRequest, reqId, b)
|
2015-10-12 01:36:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
func (c *conn) writePairs(recType recType, reqId uint16, pairs map[string]string) error {
|
|
|
|
w := newWriter(c, recType, reqId)
|
2015-10-12 01:36:21 +00:00
|
|
|
b := make([]byte, 8)
|
|
|
|
for k, v := range pairs {
|
|
|
|
n := encodeSize(b, uint32(len(k)))
|
|
|
|
n += encodeSize(b[n:], uint32(len(v)))
|
|
|
|
if _, err := w.Write(b[:n]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.WriteString(k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := w.WriteString(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readSize(s []byte) (uint32, int) {
|
|
|
|
if len(s) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
size, n := uint32(s[0]), 1
|
|
|
|
if size&(1<<7) != 0 {
|
|
|
|
if len(s) < 4 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
n = 4
|
|
|
|
size = binary.BigEndian.Uint32(s)
|
|
|
|
size &^= 1 << 31
|
|
|
|
}
|
|
|
|
return size, n
|
|
|
|
}
|
|
|
|
|
|
|
|
func readString(s []byte, size uint32) string {
|
|
|
|
if size > uint32(len(s)) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return string(s[:size])
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeSize(b []byte, size uint32) int {
|
|
|
|
if size > 127 {
|
|
|
|
size |= 1 << 31
|
|
|
|
binary.BigEndian.PutUint32(b, size)
|
|
|
|
return 4
|
|
|
|
}
|
|
|
|
b[0] = byte(size)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// bufWriter encapsulates bufio.Writer but also closes the underlying stream when
|
|
|
|
// Closed.
|
|
|
|
type bufWriter struct {
|
|
|
|
closer io.Closer
|
|
|
|
*bufio.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *bufWriter) Close() error {
|
|
|
|
if err := w.Writer.Flush(); err != nil {
|
|
|
|
w.closer.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return w.closer.Close()
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:20:59 +00:00
|
|
|
func newWriter(c *conn, recType recType, reqId uint16) *bufWriter {
|
2015-10-12 01:36:21 +00:00
|
|
|
s := &streamWriter{c: c, recType: recType, reqId: reqId}
|
|
|
|
w := bufio.NewWriterSize(s, maxWrite)
|
|
|
|
return &bufWriter{s, w}
|
|
|
|
}
|
|
|
|
|
|
|
|
// streamWriter abstracts out the separation of a stream into discrete records.
|
|
|
|
// It only writes maxWrite bytes at a time.
|
|
|
|
type streamWriter struct {
|
2016-01-14 22:20:59 +00:00
|
|
|
c *conn
|
|
|
|
recType recType
|
2015-10-12 01:36:21 +00:00
|
|
|
reqId uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *streamWriter) Write(p []byte) (int, error) {
|
|
|
|
nn := 0
|
|
|
|
for len(p) > 0 {
|
|
|
|
n := len(p)
|
|
|
|
if n > maxWrite {
|
|
|
|
n = maxWrite
|
|
|
|
}
|
|
|
|
if err := w.c.writeRecord(w.recType, w.reqId, p[:n]); err != nil {
|
|
|
|
return nn, err
|
|
|
|
}
|
|
|
|
nn += n
|
|
|
|
p = p[n:]
|
|
|
|
}
|
|
|
|
return nn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *streamWriter) Close() error {
|
|
|
|
// send empty record to close the stream
|
|
|
|
return w.c.writeRecord(w.recType, w.reqId, nil)
|
|
|
|
}
|