From b5745c941b5c2b79ec292bfdbe64fd6b20f72c2a Mon Sep 17 00:00:00 2001 From: Javier Feliz Date: Thu, 26 Dec 2024 21:24:24 -0500 Subject: [PATCH] Progress --- fastcgi/client.go | 8 +----- fastcgi/request.go | 61 ++++++++++++++++++++++++---------------------- 2 files changed, 33 insertions(+), 36 deletions(-) diff --git a/fastcgi/client.go b/fastcgi/client.go index 779e666..1ad1250 100644 --- a/fastcgi/client.go +++ b/fastcgi/client.go @@ -31,15 +31,9 @@ func (client *FCGIClient) writeRecord(recType FCGIRequestType, content []byte) ( client.mutex.Lock() defer client.mutex.Unlock() client.buf.Reset() - // Initialize the record - header := Header{} - header.init(recType, 1, len(content)) - rec := Record{ - Header: header, - Content: content, - } // Write the record to the connection + rec := NewRecord(recType, content) b, err := rec.toBytes() _, err = client.rwc.Write(b) return err diff --git a/fastcgi/request.go b/fastcgi/request.go index 213ebf5..ba39471 100644 --- a/fastcgi/request.go +++ b/fastcgi/request.go @@ -22,11 +22,6 @@ type Header struct { } func (h *Header) init(reqType FCGIRequestType, reqId uint16, l int) { - h.Version = 1 - h.Type = reqType - h.Id = reqId - h.ContentLength = uint16(l) - h.PaddingLength = uint8(-l & 7) } // A record is essentially a "packet" in FastCGI. @@ -47,6 +42,38 @@ type Record struct { 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) { @@ -109,30 +136,6 @@ func RequestFromHttp(r *http.Request) *FCGIRequest { return &c } -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 - h := Header{} - h.init(FCGI_BEGIN_REQUEST, 1, len(b)) - return &Record{ - Header: h, - Content: b[:], - } -} - func (req *FCGIRequest) Script(path string) { req.Context["SCRIPT_FILENAME"] = path }