This commit is contained in:
Javier Feliz 2024-12-28 12:35:22 -05:00
parent 10d8e5bf42
commit 677083b071
6 changed files with 197 additions and 398 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/textproto" "net/textproto"
@ -28,125 +29,135 @@ func (client *FCGIClient) Close() {
client.rwc.Close() client.rwc.Close()
} }
func (client *FCGIClient) writeRecord(recType FCGIRequestType, content []byte) (err error) { func (client *FCGIClient) writeRecord(r *Record) (err error) {
client.mutex.Lock() client.mutex.Lock()
defer client.mutex.Unlock() defer client.mutex.Unlock()
client.buf.Reset() client.buf.Reset()
// Write the record to the connection // Write the record to the connection
rec := NewRecord(recType, content) b, err := r.toBytes()
b, err := rec.toBytes()
_, err = client.rwc.Write(b) _, err = client.rwc.Write(b)
return err return err
} }
func (client *FCGIClient) writeEndRequest(appStatus int, protocolStatus uint8) error { // We write long data such as FCGI_PARAMS or FCGI_STDIN
b := make([]byte, 8) // as a stream. If a content's body is larger than
binary.BigEndian.PutUint32(b, uint32(appStatus)) // maxWrite, we will split it into separate records.
b[4] = protocolStatus // maxWrite is determined by the size of the
return client.writeRecord(FCGI_END_REQUEST, b) // contentLength field in the header (2 bytes)
// Meaning that the max value for contentLength
// we can encode is 65535.
func (c *FCGIClient) writeStream(records []Record) error {
if len(records) == 0 {
return nil
}
log.Printf("Writing %d records of type %d to stream", len(records), records[0].Header.Type)
for _, r := range records {
// Write the piece of the record to the stream
c.writeRecord(&r)
}
// Send an empty record to end the stream.
// all the records should be of the same
// type so we'll just use the type from
// the first item in the slice.
log.Println("Ending stream")
end := NewRecord(records[0].Header.Type, nil)
c.writeRecord(end)
return nil
} }
// Spec: https://www.mit.edu/~yandros/doc/specs/fcgi-spec.html#S3 func readRecord(r io.Reader) (*Record, error) {
// Name value pairs such as: SCRIPT_PATH = /some/path // It's easier to read the header piece of the record
// Should be encoded as such: // into the struct as opposed to doing it piece by
// Name size // piece. But we'll do it this way to be explicit.
// Value size var version uint8
// Name var recType FCGIRecordType
// Value var id uint16
type NameValuePair struct { var contentlength uint16
// Making the length values 32 bit for ease. var paddinglength uint8
// However, when encoding, the rules for var reserved uint8
// how many bytes are used will apply.
NameLength uint32 log.Println("Reading header of response record")
ValueLength uint32 // Let's read the header fields of the record.
// Data binary.Read(r, binary.BigEndian, &version)
NameData string binary.Read(r, binary.BigEndian, &recType)
ValueData string binary.Read(r, binary.BigEndian, &id)
binary.Read(r, binary.BigEndian, &contentlength)
binary.Read(r, binary.BigEndian, &paddinglength)
binary.Read(r, binary.BigEndian, &reserved)
log.Printf("Ver: %d Type: %d ID: %d Length: %d Padding: %d Reserved; %d", version, recType, id, contentlength, paddinglength, reserved)
log.Println("Reading record contents")
readLength := int(contentlength) + int(paddinglength)
content := make([]byte, readLength)
if _, err := io.ReadFull(r, content); err != nil {
return nil, err
}
// Remove any padding from the content
content = content[:contentlength]
rec := Record{}
rec.Header.Version = version
rec.Header.Type = recType
rec.Header.Id = id
rec.Header.ContentLength = contentlength
rec.Header.PaddingLength = paddinglength
rec.Header.Reserved = reserved
rec.Content = content
return &rec, nil
} }
func (client *FCGIClient) writePairs(recType FCGIRequestType, pairs map[string]string) error { func (c *FCGIClient) readResponse() []byte {
// Get ourselves a nice slice to work with var response bytes.Buffer
nvpairs := []NameValuePair{}
for k, v := range pairs { log.Println("-- STARTING STDOUT READ --")
nvpairs = append(nvpairs, NameValuePair{ for {
NameLength: uint32(len(k)), r, err := readRecord(c.rwc)
ValueLength: uint32(len(v)),
NameData: k, if err != nil {
ValueData: v, log.Printf("Encountered error when reading the stream: %s", err.Error())
})
} }
// We'll use this to put together log.Println("Read a record")
// the packet
var buf bytes.Buffer
for _, p := range nvpairs { if r.Header.Type == FCGI_END_REQUEST {
// Let's see how many bytes we have in total. break
// Since we have to leave 8 bytes for encoding
// the sizes, we'll add it to the calculation.
// If the value is larger than what we can
// handle, we'll truncate it
if (8 + p.NameLength + p.ValueLength) > maxWrite {
fmt.Println("We should not have hit this")
p.ValueLength = maxWrite - 8 - p.NameLength
p.ValueData = p.ValueData[:p.ValueLength]
} }
// The high bit of name size and value size is used for signaling response.Write(r.Content)
// how many bytes are used to store the length/size.
// If the size is > 127, we can just use one byte,
// and the high bit will be 0, otherwise, we use
// four bytes and the high bit will be 1
// So if length is encoded in 4 bytes it would look
// something like:
// 10000000000000000000010000100000
if p.NameLength > 127 {
p.NameLength |= 1 << 31
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, p.NameLength)
buf.Write(b)
} else {
buf.Write([]byte{byte(p.NameLength)})
} }
log.Println("-- END STDOUT READ --")
if p.ValueLength > 127 { return response.Bytes()
p.ValueLength |= 1 << 31 }
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, p.ValueLength) // func (c *FCGIClient) Get(req *http.Request, fcgiParams map[string]string) *http.Response {
buf.Write(b) //
} else { // }
buf.Write([]byte{byte(p.ValueLength)})
func (c *FCGIClient) BeginRequest() error {
err := c.writeRecord(NewBeginRequestRecord())
if err != nil {
return err
} }
// Now we just write our values to the buffer
buf.WriteString(p.NameData)
buf.WriteString(p.ValueData)
}
w := newWriter(client, recType)
defer w.Close()
// Send the data
w.Write(buf.Bytes())
w.Flush()
return nil return nil
} }
// Do made the request and returns a io.Reader that translates the data read // Do made the request and returns a io.Reader that translates the data read
// from fcgi responder out of fcgi packet before returning it. // from fcgi responder out of fcgi packet before returning it.
func (client *FCGIClient) Do(req *FCGIRequest) (http.Response, error) { func (client *FCGIClient) Do(req *FCGIRequest) (http.Response, error) {
beginRequestRecord := NewBeginRequestRecord() client.BeginRequest()
err := client.writeRecord(beginRequestRecord.Header.Type, beginRequestRecord.Content)
if err != nil {
return http.Response{}, err
}
err = client.writePairs(FCGI_PARAMS, req.Context) // Write the request context as a stream
if err != nil { log.Println("Sending FCGI_PARAMS")
return http.Response{}, err client.writeStream(req.EncodeContext())
} log.Println("Done")
// body := newWriter(client, FCGI_STDIN) // body := newWriter(client, FCGI_STDIN)
// if req != nil { // if req != nil {
@ -154,9 +165,18 @@ func (client *FCGIClient) Do(req *FCGIRequest) (http.Response, error) {
// } // }
// body.Close() // body.Close()
r := &streamReader{c: client} // Read the app response from the FCGI_STDOUT stream
rb := bufio.NewReader(r) respContent := client.readResponse()
tp := textproto.NewReader(rb)
fmt.Println(parseHttp(respContent))
return parseHttp(respContent)
}
func parseHttp(raw []byte) (http.Response, error) {
log.Println("Parsing http")
bf := bufio.NewReader(bytes.NewReader(raw))
tp := textproto.NewReader(bf)
resp := new(http.Response) resp := new(http.Response)
// Parse the first line of the response. // Parse the first line of the response.
line, err := tp.ReadLine() line, err := tp.ReadLine()
@ -201,10 +221,12 @@ func (client *FCGIClient) Do(req *FCGIRequest) (http.Response, error) {
resp.ContentLength, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64) resp.ContentLength, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
if chunked(resp.TransferEncoding) { if chunked(resp.TransferEncoding) {
resp.Body = io.NopCloser(httputil.NewChunkedReader(rb)) resp.Body = io.NopCloser(httputil.NewChunkedReader(bf))
} else { } else {
resp.Body = io.NopCloser(rb) resp.Body = io.NopCloser(bf)
} }
log.Println("Done")
return *resp, nil return *resp, nil
} }

View File

@ -1,61 +1,10 @@
package fastcgi package fastcgi
import ( import (
"bufio"
"encoding/binary"
"fmt" "fmt"
"io"
"net" "net"
) )
type FCGIRequestType uint8
const FCGI_LISTENSOCK_FILENO uint8 = 0
const FCGI_HEADER_LEN uint8 = 8
const VERSION_1 uint8 = 1
const FCGI_NULL_REQUEST_ID uint8 = 0
const FCGI_KEEP_CONN uint8 = 1
const doubleCRLF = "\r\n\r\n"
const (
FCGI_BEGIN_REQUEST FCGIRequestType = iota + 1
FCGI_ABORT_REQUEST
FCGI_END_REQUEST
FCGI_PARAMS
FCGI_STDIN
FCGI_STDOUT
FCGI_STDERR
FCGI_DATA
FCGI_GET_VALUES
FCGI_GET_VALUES_RESULT
FCGI_UNKNOWN_TYPE
FCGI_MAXTYPE = FCGI_UNKNOWN_TYPE
)
const (
FCGI_RESPONDER uint8 = iota + 1
FCGI_AUTHORIZER
FCGI_FILTER
)
const (
FCGI_REQUEST_COMPLETE uint8 = iota
FCGI_CANT_MPX_CONN
FCGI_OVERLOADED
FCGI_UNKNOWN_ROLE
)
const (
FCGI_MAX_CONNS string = "MAX_CONNS"
FCGI_MAX_REQS string = "MAX_REQS"
FCGI_MPXS_CONNS string = "MPXS_CONNS"
)
const (
maxWrite = 65500 // 65530 may work, but for compatibility
maxPad = 255
)
// Connects to the fcgi responder at the specified network address. // Connects to the fcgi responder at the specified network address.
// See func net.Dial for a description of the network and address parameters. // See func net.Dial for a description of the network and address parameters.
func Dial(network, address string) (fcgi *FCGIClient, err error) { func Dial(network, address string) (fcgi *FCGIClient, err error) {
@ -75,50 +24,6 @@ func Dial(network, address string) (fcgi *FCGIClient, err error) {
return return
} }
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])
}
// 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()
}
func newWriter(c *FCGIClient, recType FCGIRequestType) *bufWriter {
s := &streamWriter{c: c, recType: recType}
w := bufio.NewWriterSize(s, maxWrite)
return &bufWriter{s, w}
}
type badStringError struct { type badStringError struct {
what string what string
str string str string

View File

@ -3,126 +3,17 @@ package fastcgi
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
) )
// -- TYPES --
// These are the types needed to house the data for an FCGI request
type Header struct {
Version uint8
Type FCGIRequestType
Id uint16
ContentLength uint16
PaddingLength uint8
Reserved uint8
}
func (h *Header) init(reqType FCGIRequestType, reqId uint16, l int) {
}
// A record is essentially a "packet" in FastCGI.
// The header lets the server know what type
// of data is being sent, and it expects
// a certain structure depending on
// the type. For example, the
// FCGI_BEGIN_REQUEST record should
// have a body of 8 bytes with:
// - The first byte being the role
// - The second byte being also the role
// - The third byte being the flags
// - The last five bytes are reserved for future use
type Record struct {
Header Header
Content []byte
Buffer bytes.Buffer // Buffer to use when writing to the network
ReadBuffer []byte // Buffer to use when reading a response
}
func NewRecord(t FCGIRequestType, content []byte) *Record {
r := Record{}
r.Header.Version = 1
r.Header.Type = t
r.Header.Id = 1
r.Header.ContentLength = uint16(len(content))
r.Header.PaddingLength = uint8(-len(content) & 7)
r.Content = content
return &r
}
func NewBeginRequestRecord() *Record {
role := uint16(FCGI_RESPONDER)
flags := byte(0)
// Create an 8-byte array as per the FastCGI specification.
var b [8]byte
// Split the 16-bit role into two bytes and assign them.
b[0] = byte(role >> 8) // High byte
b[1] = byte(role) // Low byte
// Set the flags.
b[2] = flags
// The reserved bytes (b[3] to b[7]) will remain zero by default.
// Return a begin request record
return NewRecord(FCGI_BEGIN_REQUEST, b[:])
}
// Turn a record into a byte array so it can be
// sent over the network socket
func (r *Record) toBytes() ([]byte, error) {
// client.h.init(recType, client.reqId, len(content))
if err := binary.Write(&r.Buffer, binary.BigEndian, r.Header); err != nil {
return nil, err
}
if _, err := r.Buffer.Write(r.Content); err != nil {
return nil, err
}
if _, err := r.Buffer.Write(pad[:r.Header.PaddingLength]); err != nil {
return nil, err
}
return r.Buffer.Bytes(), nil
}
func (rec *Record) read(r io.Reader) (buf []byte, err error) {
if err = binary.Read(r, binary.BigEndian, &rec.Header); err != nil {
return
}
if rec.Header.Version != 1 {
err = errors.New("fcgi: invalid header version")
return
}
if rec.Header.Type == FCGI_END_REQUEST {
err = io.EOF
return
}
n := int(rec.Header.ContentLength) + int(rec.Header.PaddingLength)
if len(rec.ReadBuffer) < n {
rec.ReadBuffer = make([]byte, n)
}
if n, err = io.ReadFull(r, rec.ReadBuffer[:n]); err != nil {
return
}
buf = rec.ReadBuffer[:int(rec.Header.ContentLength)]
return
}
// 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
type FCGIRequest struct { type FCGIRequest struct {
Id uint16 Id uint16
Type FCGIRequestType
Context map[string]string Context map[string]string
Body []byte
Records []Record Records []Record
} }
@ -212,36 +103,72 @@ func (r *FCGIRequest) TypePostForm(data url.Values) {
// // return client.Post(p, bodyType, buf, buf.Len()) // // return client.Post(p, bodyType, buf, buf.Len())
// } // }
// Format the context for sending // Spec: https://www.mit.edu/~yandros/doc/specs/fcgi-spec.html#S3
// func (r *FCGIRequest) writePairs() error { // Name value pairs such as: SCRIPT_PATH = /some/path
// w := newWriter(client, recType) // Should be encoded as such:
// b := make([]byte, 8) // Name size
// nn := 0 // Value size
// for k, v := range r.Context { // Name
// m := 8 + len(k) + len(v) // Value
// if m > maxWrite { // We'll encode the context correctly and return
// // param data size exceed 65535 bytes" // a slice of records to send.
// vl := maxWrite - 8 - len(k) func (req *FCGIRequest) EncodeContext() []Record {
// v = v[:vl] records := []Record{}
// } for k, v := range req.Context {
// n := encodeSize(b, uint32(len(k))) // We'll use this to put together
// n += encodeSize(b[n:], uint32(len(v))) // the body of the record
// m = n + len(k) + len(v) var buf bytes.Buffer
// if (nn + m) > maxWrite {
// w.Flush() // Let's see how many bytes we have in total.
// nn = 0 // Since we have to leave 8 bytes for encoding
// } // the sizes, we'll add it to the calculation.
// nn += m // If the value is larger than what we can
// if _, err := w.Write(b[:n]); err != nil { // handle, we'll truncate it.
// return err // log.Printf("Encoding %s(%d) = %s(%d)\n", k, len(k), v, len(v))
// } if (8 + len(k) + len(v)) > maxWrite {
// if _, err := w.WriteString(k); err != nil { valMaxLength := maxWrite - 8 - len(k)
// return err v = v[:valMaxLength]
// } }
// if _, err := w.WriteString(v); err != nil {
// return err // The high bit of name size and value size is used for signaling
// } // how many bytes are used to store the length/size.
// } // If the size is > 127, we can just use one byte,
// w.Close() // and the high bit will be 0, otherwise, we use
// return nil // four bytes and the high bit will be 1
// } // So if length is encoded in 4 bytes it would look
// something like:
// 10000000000000000000010000100000
// For lengths < 127, we just use
// one byte with a high bit of 0
// 01001001
if len(k) > 127 {
size := uint32(len(k))
size |= 1 << 31 // Set the high bit to 1
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, size)
buf.Write(b)
} else {
buf.Write([]byte{byte(len(k))})
}
if len(v) > 127 {
size := uint32(len(v))
size |= 1 << 31
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, size)
buf.Write(b)
} else {
buf.Write([]byte{byte(len(v))})
}
// Now we just write our values to the buffer
buf.WriteString(k)
buf.WriteString(v)
records = append(records, *NewRecord(FCGI_PARAMS, buf.Bytes()))
buf.Reset()
}
log.Printf("We are sending %d FCGI_PARAMS records", len(records))
return records
}

View File

@ -1,28 +0,0 @@
package fastcgi
type streamReader struct {
c *FCGIClient
buf []byte
}
func (w *streamReader) Read(p []byte) (n int, err error) {
if len(p) > 0 {
if len(w.buf) == 0 {
rec := &Record{}
w.buf, err = rec.read(w.c.rwc)
if err != nil {
return
}
}
n = len(p)
if n > len(w.buf) {
n = len(w.buf)
}
copy(p, w.buf[:n])
w.buf = w.buf[n:]
}
return
}

View File

@ -1,29 +0,0 @@
package fastcgi
// streamWriter abstracts out the separation of a stream into discrete records.
// It only writes maxWrite bytes at a time.
type streamWriter struct {
c *FCGIClient
recType FCGIRequestType
}
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, 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, nil)
}

View File

@ -16,6 +16,8 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
req.TypeGet() req.TypeGet()
fcgiClient, err := fastcgi.Dial("unix", "/var/run/php/php8.3-fpm.sock") fcgiClient, err := fastcgi.Dial("unix", "/var/run/php/php8.3-fpm.sock")
defer fcgiClient.Close()
if err != nil { if err != nil {
log.Println("err:", err) log.Println("err:", err)
} }