2019-09-02 23:01:02 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// Forked Jan. 2015 from http://bitbucket.org/PinIdea/fcgi_client
|
|
|
|
// (which is forked from https://code.google.com/p/go-fastcgi-client/).
|
|
|
|
// This fork contains several fixes and improvements by Matt Holt and
|
|
|
|
// other contributors to the Caddy project.
|
|
|
|
|
|
|
|
// Copyright 2012 Junqing Tan <ivan@mysqlab.net> and The Go Authors
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// Part of source code is from Go fcgi package
|
|
|
|
|
|
|
|
package fastcgi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/textproto"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-09-01 23:02:48 -05:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2024-09-13 12:16:37 -05:00
|
|
|
"go.uber.org/zap/zapcore"
|
2019-09-02 23:01:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// FCGIListenSockFileno describes listen socket file number.
|
|
|
|
const FCGIListenSockFileno uint8 = 0
|
|
|
|
|
|
|
|
// FCGIHeaderLen describes header length.
|
|
|
|
const FCGIHeaderLen uint8 = 8
|
|
|
|
|
|
|
|
// Version1 describes the version.
|
|
|
|
const Version1 uint8 = 1
|
|
|
|
|
|
|
|
// FCGINullRequestID describes the null request ID.
|
|
|
|
const FCGINullRequestID uint8 = 0
|
|
|
|
|
|
|
|
// FCGIKeepConn describes keep connection mode.
|
|
|
|
const FCGIKeepConn uint8 = 1
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BeginRequest is the begin request flag.
|
|
|
|
BeginRequest uint8 = iota + 1
|
|
|
|
// AbortRequest is the abort request flag.
|
|
|
|
AbortRequest
|
|
|
|
// EndRequest is the end request flag.
|
|
|
|
EndRequest
|
|
|
|
// Params is the parameters flag.
|
|
|
|
Params
|
|
|
|
// Stdin is the standard input flag.
|
|
|
|
Stdin
|
|
|
|
// Stdout is the standard output flag.
|
|
|
|
Stdout
|
|
|
|
// Stderr is the standard error flag.
|
|
|
|
Stderr
|
|
|
|
// Data is the data flag.
|
|
|
|
Data
|
|
|
|
// GetValues is the get values flag.
|
|
|
|
GetValues
|
|
|
|
// GetValuesResult is the get values result flag.
|
|
|
|
GetValuesResult
|
|
|
|
// UnknownType is the unknown type flag.
|
|
|
|
UnknownType
|
|
|
|
// MaxType is the maximum type flag.
|
|
|
|
MaxType = UnknownType
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Responder is the responder flag.
|
|
|
|
Responder uint8 = iota + 1
|
|
|
|
// Authorizer is the authorizer flag.
|
|
|
|
Authorizer
|
|
|
|
// Filter is the filter flag.
|
|
|
|
Filter
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// RequestComplete is the completed request flag.
|
|
|
|
RequestComplete uint8 = iota
|
|
|
|
// CantMultiplexConns is the multiplexed connections flag.
|
|
|
|
CantMultiplexConns
|
|
|
|
// Overloaded is the overloaded flag.
|
|
|
|
Overloaded
|
|
|
|
// UnknownRole is the unknown role flag.
|
|
|
|
UnknownRole
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// MaxConns is the maximum connections flag.
|
|
|
|
MaxConns string = "MAX_CONNS"
|
|
|
|
// MaxRequests is the maximum requests flag.
|
|
|
|
MaxRequests string = "MAX_REQS"
|
|
|
|
// MultiplexConns is the multiplex connections flag.
|
|
|
|
MultiplexConns string = "MPXS_CONNS"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
maxWrite = 65500 // 65530 may work, but for compatibility
|
|
|
|
maxPad = 255
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2022-09-02 17:57:55 -05:00
|
|
|
// client implements a FastCGI client, which is a standard for
|
2019-09-02 23:01:02 -05:00
|
|
|
// interfacing external applications with Web servers.
|
2022-09-02 17:57:55 -05:00
|
|
|
type client struct {
|
|
|
|
rwc net.Conn
|
|
|
|
// keepAlive bool // TODO: implement
|
|
|
|
reqID uint16
|
|
|
|
stderr bool
|
|
|
|
logger *zap.Logger
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do made the request and returns a io.Reader that translates the data read
|
|
|
|
// from fcgi responder out of fcgi packet before returning it.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) Do(p map[string]string, req io.Reader) (r io.Reader, err error) {
|
|
|
|
writer := &streamWriter{c: c}
|
|
|
|
writer.buf = bufPool.Get().(*bytes.Buffer)
|
|
|
|
writer.buf.Reset()
|
|
|
|
defer bufPool.Put(writer.buf)
|
|
|
|
|
|
|
|
err = writer.writeBeginRequest(uint16(Responder), 0)
|
2019-09-02 23:01:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:57:55 -05:00
|
|
|
writer.recType = Params
|
|
|
|
err = writer.writePairs(p)
|
2019-09-02 23:01:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-02 17:57:55 -05:00
|
|
|
writer.recType = Stdin
|
2019-09-02 23:01:02 -05:00
|
|
|
if req != nil {
|
2022-09-02 17:57:55 -05:00
|
|
|
_, err = io.Copy(writer, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = writer.FlushStream()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
r = &streamReader{c: c}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// clientCloser is a io.ReadCloser. It wraps a io.Reader with a Closer
|
2022-09-02 17:57:55 -05:00
|
|
|
// that closes the client connection.
|
2019-09-02 23:01:02 -05:00
|
|
|
type clientCloser struct {
|
2022-09-02 17:57:55 -05:00
|
|
|
rwc net.Conn
|
|
|
|
r *streamReader
|
2019-09-02 23:01:02 -05:00
|
|
|
io.Reader
|
2022-09-01 23:02:48 -05:00
|
|
|
|
|
|
|
status int
|
|
|
|
logger *zap.Logger
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
|
2022-09-01 23:02:48 -05:00
|
|
|
func (f clientCloser) Close() error {
|
2022-09-02 17:57:55 -05:00
|
|
|
stderr := f.r.stderr.Bytes()
|
2022-09-01 23:02:48 -05:00
|
|
|
if len(stderr) == 0 {
|
2022-09-02 17:57:55 -05:00
|
|
|
return f.rwc.Close()
|
2022-09-01 23:02:48 -05:00
|
|
|
}
|
|
|
|
|
2024-09-13 12:16:37 -05:00
|
|
|
logLevel := zapcore.WarnLevel
|
2022-09-01 23:02:48 -05:00
|
|
|
if f.status >= 400 {
|
2024-09-13 12:16:37 -05:00
|
|
|
logLevel = zapcore.ErrorLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
if c := f.logger.Check(logLevel, "stderr"); c != nil {
|
|
|
|
c.Write(zap.ByteString("body", stderr))
|
2022-09-01 23:02:48 -05:00
|
|
|
}
|
2022-09-02 17:57:55 -05:00
|
|
|
|
|
|
|
return f.rwc.Close()
|
2022-09-01 23:02:48 -05:00
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
|
|
|
|
// Request returns a HTTP Response with Header and Body
|
|
|
|
// from fcgi responder
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) Request(p map[string]string, req io.Reader) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
r, err := c.Do(p, req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rb := bufio.NewReader(r)
|
|
|
|
tp := textproto.NewReader(rb)
|
|
|
|
resp = new(http.Response)
|
|
|
|
|
|
|
|
// Parse the response headers.
|
|
|
|
mimeHeader, err := tp.ReadMIMEHeader()
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Header = http.Header(mimeHeader)
|
|
|
|
|
|
|
|
if resp.Header.Get("Status") != "" {
|
2022-08-04 12:17:35 -05:00
|
|
|
statusNumber, statusInfo, statusIsCut := strings.Cut(resp.Header.Get("Status"), " ")
|
|
|
|
resp.StatusCode, err = strconv.Atoi(statusNumber)
|
2019-09-02 23:01:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-08-04 12:17:35 -05:00
|
|
|
if statusIsCut {
|
|
|
|
resp.Status = statusInfo
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resp.StatusCode = http.StatusOK
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: fixTransferEncoding ?
|
|
|
|
resp.TransferEncoding = resp.Header["Transfer-Encoding"]
|
|
|
|
resp.ContentLength, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
|
|
|
|
|
2022-09-02 17:57:55 -05:00
|
|
|
// wrap the response body in our closer
|
|
|
|
closer := clientCloser{
|
|
|
|
rwc: c.rwc,
|
|
|
|
r: r.(*streamReader),
|
|
|
|
Reader: rb,
|
|
|
|
status: resp.StatusCode,
|
|
|
|
logger: noopLogger,
|
|
|
|
}
|
2019-09-02 23:01:02 -05:00
|
|
|
if chunked(resp.TransferEncoding) {
|
2022-09-02 17:57:55 -05:00
|
|
|
closer.Reader = httputil.NewChunkedReader(rb)
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
2022-09-02 17:57:55 -05:00
|
|
|
if c.stderr {
|
|
|
|
closer.logger = c.logger
|
|
|
|
}
|
|
|
|
resp.Body = closer
|
|
|
|
|
2019-09-02 23:01:02 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get issues a GET request to the fcgi responder.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) Get(p map[string]string, body io.Reader, l int64) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
p["REQUEST_METHOD"] = "GET"
|
|
|
|
p["CONTENT_LENGTH"] = strconv.FormatInt(l, 10)
|
|
|
|
|
|
|
|
return c.Request(p, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Head issues a HEAD request to the fcgi responder.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) Head(p map[string]string) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
p["REQUEST_METHOD"] = "HEAD"
|
|
|
|
p["CONTENT_LENGTH"] = "0"
|
|
|
|
|
|
|
|
return c.Request(p, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options issues an OPTIONS request to the fcgi responder.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) Options(p map[string]string) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
p["REQUEST_METHOD"] = "OPTIONS"
|
|
|
|
p["CONTENT_LENGTH"] = "0"
|
|
|
|
|
|
|
|
return c.Request(p, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Post issues a POST request to the fcgi responder. with request body
|
|
|
|
// in the format that bodyType specified
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) Post(p map[string]string, method string, bodyType string, body io.Reader, l int64) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
if p == nil {
|
|
|
|
p = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
p["REQUEST_METHOD"] = strings.ToUpper(method)
|
|
|
|
|
|
|
|
if len(p["REQUEST_METHOD"]) == 0 || p["REQUEST_METHOD"] == "GET" {
|
|
|
|
p["REQUEST_METHOD"] = "POST"
|
|
|
|
}
|
|
|
|
|
|
|
|
p["CONTENT_LENGTH"] = strconv.FormatInt(l, 10)
|
|
|
|
if len(bodyType) > 0 {
|
|
|
|
p["CONTENT_TYPE"] = bodyType
|
|
|
|
} else {
|
|
|
|
p["CONTENT_TYPE"] = "application/x-www-form-urlencoded"
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Request(p, body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostForm issues a POST to the fcgi responder, with form
|
|
|
|
// as a string key to a list values (url.Values)
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) PostForm(p map[string]string, data url.Values) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
body := bytes.NewReader([]byte(data.Encode()))
|
|
|
|
return c.Post(p, "POST", "application/x-www-form-urlencoded", body, int64(body.Len()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostFile issues a POST to the fcgi responder in multipart(RFC 2046) standard,
|
|
|
|
// with form as a string key to a list values (url.Values),
|
|
|
|
// and/or with file as a string key to a list file path.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) PostFile(p map[string]string, data url.Values, file map[string]string) (resp *http.Response, err error) {
|
2019-09-02 23:01:02 -05:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(buf)
|
|
|
|
bodyType := writer.FormDataContentType()
|
|
|
|
|
|
|
|
for key, val := range data {
|
|
|
|
for _, v0 := range val {
|
|
|
|
err = writer.WriteField(key, v0)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range file {
|
|
|
|
fd, e := os.Open(val)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
part, e := writer.CreateFormFile(key, filepath.Base(val))
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, fd)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Post(p, "POST", bodyType, buf, int64(buf.Len()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadTimeout sets the read timeout for future calls that read from the
|
|
|
|
// fcgi responder. A zero value for t means no timeout will be set.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) SetReadTimeout(t time.Duration) error {
|
|
|
|
if t != 0 {
|
|
|
|
return c.rwc.SetReadDeadline(time.Now().Add(t))
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteTimeout sets the write timeout for future calls that send data to
|
|
|
|
// the fcgi responder. A zero value for t means no timeout will be set.
|
2022-09-02 17:57:55 -05:00
|
|
|
func (c *client) SetWriteTimeout(t time.Duration) error {
|
|
|
|
if t != 0 {
|
|
|
|
return c.rwc.SetWriteDeadline(time.Now().Add(t))
|
2019-09-02 23:01:02 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether chunked is part of the encodings stack
|
|
|
|
func chunked(te []string) bool { return len(te) > 0 && te[0] == "chunked" }
|