2019-09-03 07:01:02 +03: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.
|
|
|
|
|
|
|
|
package fastcgi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-09-05 22:36:42 +03:00
|
|
|
"net/url"
|
2019-09-03 07:01:02 +03:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-09-05 22:36:42 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-09-05 22:14:39 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
|
2019-09-03 07:01:02 +03:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
caddy.RegisterModule(Transport{})
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:14:39 +03:00
|
|
|
// Transport facilitates FastCGI communication.
|
2019-09-03 07:01:02 +03:00
|
|
|
type Transport struct {
|
2019-09-05 22:36:42 +03:00
|
|
|
// TODO: Populate these
|
|
|
|
softwareName string
|
|
|
|
softwareVersion string
|
|
|
|
serverName string
|
|
|
|
serverPort string
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// Use this directory as the fastcgi root directory. Defaults to the root
|
|
|
|
// directory of the parent virtual host.
|
2019-09-05 22:14:39 +03:00
|
|
|
Root string `json:"root,omitempty"`
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// The path in the URL will be split into two, with the first piece ending
|
|
|
|
// with the value of SplitPath. The first piece will be assumed as the
|
|
|
|
// actual resource (CGI script) name, and the second piece will be set to
|
|
|
|
// PATH_INFO for the CGI script to use.
|
2019-09-05 22:14:39 +03:00
|
|
|
SplitPath string `json:"split_path,omitempty"`
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// Environment Variables
|
2019-09-05 22:14:39 +03:00
|
|
|
EnvVars [][2]string `json:"env,omitempty"`
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// The duration used to set a deadline when connecting to an upstream.
|
2019-09-05 22:14:39 +03:00
|
|
|
DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// The duration used to set a deadline when reading from the FastCGI server.
|
2019-09-05 22:14:39 +03:00
|
|
|
ReadTimeout caddy.Duration `json:"read_timeout,omitempty"`
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// The duration used to set a deadline when sending to the FastCGI server.
|
2019-09-05 22:14:39 +03:00
|
|
|
WriteTimeout caddy.Duration `json:"write_timeout,omitempty"`
|
2019-09-03 07:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (Transport) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
|
|
|
Name: "http.handlers.reverse_proxy.transport.fastcgi",
|
|
|
|
New: func() caddy.Module { return new(Transport) },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:42:20 +03:00
|
|
|
// Provision sets up t.
|
|
|
|
func (t *Transport) Provision(_ caddy.Context) error {
|
|
|
|
if t.Root == "" {
|
|
|
|
t.Root = "{http.var.root}"
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:14:39 +03:00
|
|
|
// RoundTrip implements http.RoundTripper.
|
2019-09-03 07:01:02 +03:00
|
|
|
func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
|
|
env, err := t.buildEnv(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("building environment: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:14:39 +03:00
|
|
|
// TODO: doesn't dialer have a Timeout field?
|
2019-09-03 07:01:02 +03:00
|
|
|
ctx := context.Background()
|
|
|
|
if t.DialTimeout > 0 {
|
|
|
|
var cancel context.CancelFunc
|
2019-09-05 22:14:39 +03:00
|
|
|
ctx, cancel = context.WithTimeout(ctx, time.Duration(t.DialTimeout))
|
2019-09-03 07:01:02 +03:00
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:14:39 +03:00
|
|
|
// extract dial information from request (this
|
|
|
|
// should embedded by the reverse proxy)
|
|
|
|
network, address := "tcp", r.URL.Host
|
|
|
|
if dialInfoVal := ctx.Value(reverseproxy.DialInfoCtxKey); dialInfoVal != nil {
|
|
|
|
dialInfo := dialInfoVal.(reverseproxy.DialInfo)
|
|
|
|
network = dialInfo.Network
|
|
|
|
address = dialInfo.Address
|
|
|
|
}
|
|
|
|
|
2019-09-03 07:01:02 +03:00
|
|
|
fcgiBackend, err := DialContext(ctx, network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dialing backend: %v", err)
|
|
|
|
}
|
2019-09-05 22:14:39 +03:00
|
|
|
// fcgiBackend gets closed when response body is closed (see clientCloser)
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// read/write timeouts
|
2019-09-05 22:14:39 +03:00
|
|
|
if err := fcgiBackend.SetReadTimeout(time.Duration(t.ReadTimeout)); err != nil {
|
2019-09-03 07:01:02 +03:00
|
|
|
return nil, fmt.Errorf("setting read timeout: %v", err)
|
|
|
|
}
|
2019-09-05 22:14:39 +03:00
|
|
|
if err := fcgiBackend.SetWriteTimeout(time.Duration(t.WriteTimeout)); err != nil {
|
2019-09-03 07:01:02 +03:00
|
|
|
return nil, fmt.Errorf("setting write timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-05 22:14:39 +03:00
|
|
|
contentLength := r.ContentLength
|
|
|
|
if contentLength == 0 {
|
2019-09-03 07:01:02 +03:00
|
|
|
contentLength, _ = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64)
|
|
|
|
}
|
2019-09-05 22:14:39 +03:00
|
|
|
|
|
|
|
var resp *http.Response
|
2019-09-03 07:01:02 +03:00
|
|
|
switch r.Method {
|
2019-09-05 22:14:39 +03:00
|
|
|
case http.MethodHead:
|
2019-09-03 07:01:02 +03:00
|
|
|
resp, err = fcgiBackend.Head(env)
|
2019-09-05 22:14:39 +03:00
|
|
|
case http.MethodGet:
|
2019-09-03 07:01:02 +03:00
|
|
|
resp, err = fcgiBackend.Get(env, r.Body, contentLength)
|
2019-09-05 22:14:39 +03:00
|
|
|
case http.MethodOptions:
|
2019-09-03 07:01:02 +03:00
|
|
|
resp, err = fcgiBackend.Options(env)
|
|
|
|
default:
|
|
|
|
resp, err = fcgiBackend.Post(env, r.Method, r.Header.Get("Content-Type"), r.Body, contentLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildEnv returns a set of CGI environment variables for the request.
|
|
|
|
func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
|
2019-09-05 22:42:20 +03:00
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
|
|
|
|
|
2019-09-03 07:01:02 +03:00
|
|
|
var env map[string]string
|
|
|
|
|
|
|
|
// Separate remote IP and port; more lenient than net.SplitHostPort
|
|
|
|
var ip, port string
|
|
|
|
if idx := strings.LastIndex(r.RemoteAddr, ":"); idx > -1 {
|
|
|
|
ip = r.RemoteAddr[:idx]
|
|
|
|
port = r.RemoteAddr[idx+1:]
|
|
|
|
} else {
|
|
|
|
ip = r.RemoteAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove [] from IPv6 addresses
|
|
|
|
ip = strings.Replace(ip, "[", "", 1)
|
|
|
|
ip = strings.Replace(ip, "]", "", 1)
|
|
|
|
|
2019-09-05 22:42:20 +03:00
|
|
|
root := repl.ReplaceAll(t.Root, ".")
|
2019-09-03 07:01:02 +03:00
|
|
|
fpath := r.URL.Path
|
|
|
|
|
|
|
|
// Split path in preparation for env variables.
|
|
|
|
// Previous canSplit checks ensure this can never be -1.
|
|
|
|
// TODO: I haven't brought over canSplit; make sure this doesn't break
|
|
|
|
splitPos := t.splitPos(fpath)
|
|
|
|
|
|
|
|
// Request has the extension; path was split successfully
|
|
|
|
docURI := fpath[:splitPos+len(t.SplitPath)]
|
|
|
|
pathInfo := fpath[splitPos+len(t.SplitPath):]
|
|
|
|
scriptName := fpath
|
|
|
|
|
|
|
|
// Strip PATH_INFO from SCRIPT_NAME
|
|
|
|
scriptName = strings.TrimSuffix(scriptName, pathInfo)
|
|
|
|
|
|
|
|
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME
|
2019-09-05 22:42:20 +03:00
|
|
|
scriptFilename := filepath.Join(root, scriptName)
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// Add vhost path prefix to scriptName. Otherwise, some PHP software will
|
|
|
|
// have difficulty discovering its URL.
|
|
|
|
pathPrefix, _ := r.Context().Value(caddy.CtxKey("path_prefix")).(string)
|
|
|
|
scriptName = path.Join(pathPrefix, scriptName)
|
|
|
|
|
2019-09-05 22:36:42 +03:00
|
|
|
// Get the request URL from context. The context stores the original URL in case
|
|
|
|
// it was changed by a middleware such as rewrite. By default, we pass the
|
|
|
|
// original URI in as the value of REQUEST_URI (the user can overwrite this
|
|
|
|
// if desired). Most PHP apps seem to want the original URI. Besides, this is
|
|
|
|
// how nginx defaults: http://stackoverflow.com/a/12485156/1048862
|
|
|
|
reqURL, ok := r.Context().Value(caddyhttp.OriginalURLCtxKey).(url.URL)
|
|
|
|
if !ok {
|
|
|
|
// some requests, like active health checks, don't add this to
|
|
|
|
// the request context, so we can just use the current URL
|
|
|
|
reqURL = *r.URL
|
|
|
|
}
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
requestScheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
requestScheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some variables are unused but cleared explicitly to prevent
|
|
|
|
// the parent environment from interfering.
|
|
|
|
env = map[string]string{
|
|
|
|
// Variables defined in CGI 1.1 spec
|
|
|
|
"AUTH_TYPE": "", // Not used
|
|
|
|
"CONTENT_LENGTH": r.Header.Get("Content-Length"),
|
|
|
|
"CONTENT_TYPE": r.Header.Get("Content-Type"),
|
|
|
|
"GATEWAY_INTERFACE": "CGI/1.1",
|
|
|
|
"PATH_INFO": pathInfo,
|
|
|
|
"QUERY_STRING": r.URL.RawQuery,
|
|
|
|
"REMOTE_ADDR": ip,
|
|
|
|
"REMOTE_HOST": ip, // For speed, remote host lookups disabled
|
|
|
|
"REMOTE_PORT": port,
|
|
|
|
"REMOTE_IDENT": "", // Not used
|
2019-09-05 22:36:42 +03:00
|
|
|
"REMOTE_USER": "", // TODO: once there are authentication handlers, populate this
|
|
|
|
"REQUEST_METHOD": r.Method,
|
|
|
|
"REQUEST_SCHEME": requestScheme,
|
2019-09-05 22:42:20 +03:00
|
|
|
"SERVER_NAME": t.serverName,
|
|
|
|
"SERVER_PORT": t.serverPort,
|
2019-09-05 22:36:42 +03:00
|
|
|
"SERVER_PROTOCOL": r.Proto,
|
2019-09-05 22:42:20 +03:00
|
|
|
"SERVER_SOFTWARE": t.softwareName + "/" + t.softwareVersion,
|
2019-09-03 07:01:02 +03:00
|
|
|
|
|
|
|
// Other variables
|
2019-09-05 22:42:20 +03:00
|
|
|
"DOCUMENT_ROOT": root,
|
2019-09-05 22:36:42 +03:00
|
|
|
"DOCUMENT_URI": docURI,
|
|
|
|
"HTTP_HOST": r.Host, // added here, since not always part of headers
|
|
|
|
"REQUEST_URI": reqURL.RequestURI(),
|
2019-09-03 07:01:02 +03:00
|
|
|
"SCRIPT_FILENAME": scriptFilename,
|
|
|
|
"SCRIPT_NAME": scriptName,
|
|
|
|
}
|
|
|
|
|
|
|
|
// compliance with the CGI specification requires that
|
|
|
|
// PATH_TRANSLATED should only exist if PATH_INFO is defined.
|
|
|
|
// Info: https://www.ietf.org/rfc/rfc3875 Page 14
|
|
|
|
if env["PATH_INFO"] != "" {
|
2019-09-05 22:42:20 +03:00
|
|
|
env["PATH_TRANSLATED"] = filepath.Join(root, pathInfo) // Info: http://www.oreilly.com/openbook/cgi/ch02_04.html
|
2019-09-03 07:01:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some web apps rely on knowing HTTPS or not
|
|
|
|
if r.TLS != nil {
|
|
|
|
env["HTTPS"] = "on"
|
|
|
|
// and pass the protocol details in a manner compatible with apache's mod_ssl
|
|
|
|
// (which is why these have a SSL_ prefix and not TLS_).
|
|
|
|
v, ok := tlsProtocolStrings[r.TLS.Version]
|
|
|
|
if ok {
|
|
|
|
env["SSL_PROTOCOL"] = v
|
|
|
|
}
|
|
|
|
// and pass the cipher suite in a manner compatible with apache's mod_ssl
|
|
|
|
for k, v := range caddytls.SupportedCipherSuites {
|
|
|
|
if v == r.TLS.CipherSuite {
|
|
|
|
env["SSL_CIPHER"] = k
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add env variables from config (with support for placeholders in values)
|
|
|
|
for _, envVar := range t.EnvVars {
|
|
|
|
env[envVar[0]] = repl.ReplaceAll(envVar[1], "")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add all HTTP headers to env variables
|
|
|
|
for field, val := range r.Header {
|
|
|
|
header := strings.ToUpper(field)
|
|
|
|
header = headerNameReplacer.Replace(header)
|
|
|
|
env["HTTP_"+header] = strings.Join(val, ", ")
|
|
|
|
}
|
|
|
|
return env, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitPos returns the index where path should
|
|
|
|
// be split based on t.SplitPath.
|
|
|
|
func (t Transport) splitPos(path string) int {
|
|
|
|
// TODO:
|
|
|
|
// if httpserver.CaseSensitivePath {
|
|
|
|
// return strings.Index(path, r.SplitPath)
|
|
|
|
// }
|
|
|
|
return strings.Index(strings.ToLower(path), strings.ToLower(t.SplitPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// Map of supported protocols to Apache ssl_mod format
|
|
|
|
// Note that these are slightly different from SupportedProtocols in caddytls/config.go
|
|
|
|
var tlsProtocolStrings = map[uint16]string{
|
|
|
|
tls.VersionTLS10: "TLSv1",
|
|
|
|
tls.VersionTLS11: "TLSv1.1",
|
|
|
|
tls.VersionTLS12: "TLSv1.2",
|
|
|
|
tls.VersionTLS13: "TLSv1.3",
|
|
|
|
}
|
|
|
|
|
|
|
|
var headerNameReplacer = strings.NewReplacer(" ", "_", "-", "_")
|
2019-09-05 22:42:20 +03:00
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ caddy.Provisioner = (*Transport)(nil)
|
|
|
|
_ http.RoundTripper = (*Transport)(nil)
|
|
|
|
)
|