2019-09-03 12:01:02 +08: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 (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
2019-12-29 07:35:29 +08:00
|
|
|
"net"
|
2019-09-03 12:01:02 +08:00
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-09-06 03:36:42 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
|
2019-09-06 03:14:39 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
|
2019-09-03 12:01:02 +08:00
|
|
|
"github.com/caddyserver/caddy/v2/modules/caddytls"
|
2020-03-24 22:34:15 +08:00
|
|
|
"go.uber.org/zap"
|
2021-12-03 04:23:19 +08:00
|
|
|
"go.uber.org/zap/zapcore"
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
|
|
)
|
|
|
|
|
2022-09-02 12:02:48 +08:00
|
|
|
var noopLogger = zap.NewNop()
|
|
|
|
|
2019-09-03 12:01:02 +08:00
|
|
|
func init() {
|
|
|
|
caddy.RegisterModule(Transport{})
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:14:39 +08:00
|
|
|
// Transport facilitates FastCGI communication.
|
2019-09-03 12:01:02 +08:00
|
|
|
type Transport struct {
|
|
|
|
// Use this directory as the fastcgi root directory. Defaults to the root
|
|
|
|
// directory of the parent virtual host.
|
2019-09-06 03:14:39 +08:00
|
|
|
Root string `json:"root,omitempty"`
|
2019-09-03 12:01:02 +08: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.
|
2020-03-22 21:48:21 +08:00
|
|
|
//
|
2019-10-29 04:39:37 +08:00
|
|
|
// Future enhancements should be careful to avoid CVE-2019-11043,
|
|
|
|
// which can be mitigated with use of a try_files-like behavior
|
2020-03-22 21:48:21 +08:00
|
|
|
// that 404s if the fastcgi path info is not found.
|
|
|
|
SplitPath []string `json:"split_path,omitempty"`
|
2019-09-03 12:01:02 +08:00
|
|
|
|
2020-07-21 02:16:13 +08:00
|
|
|
// Path declared as root directory will be resolved to its absolute value
|
|
|
|
// after the evaluation of any symbolic links.
|
|
|
|
// Due to the nature of PHP opcache, root directory path is cached: when
|
|
|
|
// using a symlinked directory as root this could generate errors when
|
|
|
|
// symlink is changed without php-fpm being restarted; enabling this
|
|
|
|
// directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path.
|
|
|
|
ResolveRootSymlink bool `json:"resolve_root_symlink,omitempty"`
|
|
|
|
|
2019-12-24 03:45:35 +08:00
|
|
|
// Extra environment variables.
|
2019-09-11 04:12:51 +08:00
|
|
|
EnvVars map[string]string `json:"env,omitempty"`
|
2019-09-03 12:01:02 +08:00
|
|
|
|
2021-11-24 14:32:25 +08:00
|
|
|
// The duration used to set a deadline when connecting to an upstream. Default: `3s`.
|
2019-09-06 03:14:39 +08:00
|
|
|
DialTimeout caddy.Duration `json:"dial_timeout,omitempty"`
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
// The duration used to set a deadline when reading from the FastCGI server.
|
2019-09-06 03:14:39 +08:00
|
|
|
ReadTimeout caddy.Duration `json:"read_timeout,omitempty"`
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
// The duration used to set a deadline when sending to the FastCGI server.
|
2019-09-06 03:14:39 +08:00
|
|
|
WriteTimeout caddy.Duration `json:"write_timeout,omitempty"`
|
2019-12-29 07:35:29 +08:00
|
|
|
|
2022-09-02 12:02:48 +08:00
|
|
|
// Capture and log any messages sent by the upstream on stderr. Logs at WARN
|
|
|
|
// level by default. If the response has a 4xx or 5xx status ERROR level will
|
|
|
|
// be used instead.
|
|
|
|
CaptureStderr bool `json:"capture_stderr,omitempty"`
|
|
|
|
|
2019-12-29 07:35:29 +08:00
|
|
|
serverSoftware string
|
2020-03-24 22:34:15 +08:00
|
|
|
logger *zap.Logger
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (Transport) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-11 04:36:46 +08:00
|
|
|
ID: "http.reverse_proxy.transport.fastcgi",
|
|
|
|
New: func() caddy.Module { return new(Transport) },
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:42:20 +08:00
|
|
|
// Provision sets up t.
|
2020-03-24 22:34:15 +08:00
|
|
|
func (t *Transport) Provision(ctx caddy.Context) error {
|
2022-09-17 06:55:30 +08:00
|
|
|
t.logger = ctx.Logger()
|
2021-11-24 14:32:25 +08:00
|
|
|
|
2019-09-06 03:42:20 +08:00
|
|
|
if t.Root == "" {
|
2019-09-07 02:36:45 +08:00
|
|
|
t.Root = "{http.vars.root}"
|
2019-09-06 03:42:20 +08:00
|
|
|
}
|
2021-11-24 14:32:25 +08:00
|
|
|
|
2022-08-05 01:16:59 +08:00
|
|
|
version, _ := caddy.Version()
|
|
|
|
t.serverSoftware = "Caddy/" + version
|
2021-11-24 14:32:25 +08:00
|
|
|
|
|
|
|
// Set a relatively short default dial timeout.
|
|
|
|
// This is helpful to make load-balancer retries more speedy.
|
|
|
|
if t.DialTimeout == 0 {
|
|
|
|
t.DialTimeout = caddy.Duration(3 * time.Second)
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:42:20 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:14:39 +08:00
|
|
|
// RoundTrip implements http.RoundTripper.
|
2019-09-03 12:01:02 +08:00
|
|
|
func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
|
2022-09-02 12:02:48 +08:00
|
|
|
server := r.Context().Value(caddyhttp.ServerCtxKey).(*caddyhttp.Server)
|
|
|
|
|
2022-03-08 01:06:33 +08:00
|
|
|
// Disallow null bytes in the request path, because
|
|
|
|
// PHP upstreams may do bad things, like execute a
|
|
|
|
// non-PHP file as PHP code. See #4574
|
|
|
|
if strings.Contains(r.URL.Path, "\x00") {
|
|
|
|
return nil, caddyhttp.Error(http.StatusBadRequest, fmt.Errorf("invalid request path"))
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:01:02 +08:00
|
|
|
env, err := t.buildEnv(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("building environment: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-07 02:02:11 +08:00
|
|
|
ctx := r.Context()
|
2019-09-03 12:01:02 +08:00
|
|
|
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-10 01:00:13 +08:00
|
|
|
// extract dial information from request (should have been embedded by the reverse proxy)
|
2019-09-06 03:14:39 +08:00
|
|
|
network, address := "tcp", r.URL.Host
|
http: Change routes to sequential matcher evaluation (#2967)
Previously, all matchers in a route would be evaluated before any
handlers were executed, and a composite route of the matching routes
would be created. This made rewrites especially tricky, since the only
way to defer later matchers' evaluation was to wrap them in a subroute,
or to invoke a "rehandle" which often caused bugs.
Instead, this new sequential design evaluates each route's matchers then
its handlers in lock-step; matcher-handlers-matcher-handlers...
If the first matching route consists of a rewrite, then the second route
will be evaluated against the rewritten request, rather than the original
one, and so on.
This should do away with any need for rehandling.
I've also taken this opportunity to avoid adding new values to the
request context in the handler chain, as this creates a copy of the
Request struct, which may possibly lead to bugs like it has in the past
(see PR #1542, PR #1481, and maybe issue #2463). We now add all the
expected context values in the top-level handler at the server, then
any new values can be added to the variable table via the VarsCtxKey
context key, or just the GetVar/SetVar functions. In particular, we are
using this facility to convey dial information in the reverse proxy.
Had to be careful in one place as the middleware compilation logic has
changed, and moved a bit. We no longer compile a middleware chain per-
request; instead, we can compile it at provision-time, and defer only the
evaluation of matchers to request-time, which should slightly improve
performance. Doing this, however, we take advantage of multiple function
closures, and we also changed the use of HandlerFunc (function pointer)
to Handler (interface)... this led to a situation where, if we aren't
careful, allows one request routed a certain way to permanently change
the "next" handler for all/most other requests! We avoid this by making
a copy of the interface value (which is a lightweight pointer copy) and
using exclusively that within our wrapped handlers. This way, the
original stack frame is preserved in a "read-only" fashion. The comments
in the code describe this phenomenon.
This may very well be a breaking change for some configurations, however
I do not expect it to impact many people. I will make it clear in the
release notes that this change has occurred.
2020-01-10 01:00:13 +08:00
|
|
|
if dialInfo, ok := reverseproxy.GetDialInfo(ctx); ok {
|
2019-09-06 03:14:39 +08:00
|
|
|
network = dialInfo.Network
|
|
|
|
address = dialInfo.Address
|
|
|
|
}
|
|
|
|
|
2022-09-02 12:02:48 +08:00
|
|
|
logCreds := server.Logs != nil && server.Logs.ShouldLogCredentials
|
|
|
|
loggableReq := caddyhttp.LoggableHTTPRequest{
|
|
|
|
Request: r,
|
|
|
|
ShouldLogCredentials: logCreds,
|
|
|
|
}
|
|
|
|
loggableEnv := loggableEnv{vars: env, logCredentials: logCreds}
|
2022-09-03 06:57:55 +08:00
|
|
|
|
|
|
|
logger := t.logger.With(
|
2022-09-02 12:02:48 +08:00
|
|
|
zap.Object("request", loggableReq),
|
|
|
|
zap.Object("env", loggableEnv),
|
2020-03-24 22:34:15 +08:00
|
|
|
)
|
2022-09-03 06:57:55 +08:00
|
|
|
logger.Debug("roundtrip",
|
|
|
|
zap.String("dial", address),
|
|
|
|
zap.Object("env", loggableEnv),
|
|
|
|
zap.Object("request", loggableReq))
|
2020-03-24 22:34:15 +08:00
|
|
|
|
2022-09-03 06:57:55 +08:00
|
|
|
// connect to the backend
|
|
|
|
dialer := net.Dialer{Timeout: time.Duration(t.DialTimeout)}
|
|
|
|
conn, err := dialer.DialContext(ctx, network, address)
|
2019-09-03 12:01:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("dialing backend: %v", err)
|
|
|
|
}
|
2022-09-03 06:57:55 +08:00
|
|
|
defer func() {
|
|
|
|
// conn will be closed with the response body unless there's an error
|
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// create the client that will facilitate the protocol
|
|
|
|
client := client{
|
|
|
|
rwc: conn,
|
|
|
|
reqID: 1,
|
|
|
|
logger: logger,
|
2023-05-04 08:40:49 +08:00
|
|
|
stderr: t.CaptureStderr,
|
2022-09-02 12:02:48 +08:00
|
|
|
}
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
// read/write timeouts
|
2022-09-03 06:57:55 +08:00
|
|
|
if err = client.SetReadTimeout(time.Duration(t.ReadTimeout)); err != nil {
|
2019-09-03 12:01:02 +08:00
|
|
|
return nil, fmt.Errorf("setting read timeout: %v", err)
|
|
|
|
}
|
2022-09-03 06:57:55 +08:00
|
|
|
if err = client.SetWriteTimeout(time.Duration(t.WriteTimeout)); err != nil {
|
2019-09-03 12:01:02 +08:00
|
|
|
return nil, fmt.Errorf("setting write timeout: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:14:39 +08:00
|
|
|
contentLength := r.ContentLength
|
|
|
|
if contentLength == 0 {
|
2019-09-03 12:01:02 +08:00
|
|
|
contentLength, _ = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64)
|
|
|
|
}
|
2019-09-06 03:14:39 +08:00
|
|
|
|
|
|
|
var resp *http.Response
|
2019-09-03 12:01:02 +08:00
|
|
|
switch r.Method {
|
2019-09-06 03:14:39 +08:00
|
|
|
case http.MethodHead:
|
2022-09-03 06:57:55 +08:00
|
|
|
resp, err = client.Head(env)
|
2019-09-06 03:14:39 +08:00
|
|
|
case http.MethodGet:
|
2022-09-03 06:57:55 +08:00
|
|
|
resp, err = client.Get(env, r.Body, contentLength)
|
2019-09-06 03:14:39 +08:00
|
|
|
case http.MethodOptions:
|
2022-09-03 06:57:55 +08:00
|
|
|
resp, err = client.Options(env)
|
2019-09-03 12:01:02 +08:00
|
|
|
default:
|
2022-09-03 06:57:55 +08:00
|
|
|
resp, err = client.Post(env, r.Method, r.Header.Get("Content-Type"), r.Body, contentLength)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2022-09-03 06:57:55 +08:00
|
|
|
return resp, nil
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// buildEnv returns a set of CGI environment variables for the request.
|
2021-12-03 04:23:19 +08:00
|
|
|
func (t Transport) buildEnv(r *http.Request) (envVars, error) {
|
2019-12-30 04:12:52 +08:00
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
2019-09-06 03:42:20 +08:00
|
|
|
|
2021-12-03 04:23:19 +08:00
|
|
|
var env envVars
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2020-03-24 11:12:54 +08:00
|
|
|
// make sure file root is absolute
|
|
|
|
root, err := filepath.Abs(repl.ReplaceAll(t.Root, "."))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-21 02:16:13 +08:00
|
|
|
if t.ResolveRootSymlink {
|
|
|
|
root, err = filepath.EvalSymlinks(root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:01:02 +08:00
|
|
|
fpath := r.URL.Path
|
2020-12-05 08:12:13 +08:00
|
|
|
scriptName := fpath
|
2019-09-03 12:01:02 +08:00
|
|
|
|
2020-12-05 08:12:13 +08:00
|
|
|
docURI := fpath
|
2020-04-04 00:25:25 +08:00
|
|
|
// split "actual path" from "path info" if configured
|
2020-12-05 08:12:13 +08:00
|
|
|
var pathInfo string
|
2020-04-04 00:25:25 +08:00
|
|
|
if splitPos := t.splitPos(fpath); splitPos > -1 {
|
|
|
|
docURI = fpath[:splitPos]
|
|
|
|
pathInfo = fpath[splitPos:]
|
2020-12-05 08:12:13 +08:00
|
|
|
|
|
|
|
// Strip PATH_INFO from SCRIPT_NAME
|
|
|
|
scriptName = strings.TrimSuffix(scriptName, pathInfo)
|
2020-04-04 00:25:25 +08:00
|
|
|
}
|
2019-09-03 12:01:02 +08:00
|
|
|
|
2020-12-05 08:12:13 +08:00
|
|
|
// Try to grab the path remainder from a file matcher
|
|
|
|
// if we didn't get a split result here.
|
|
|
|
// See https://github.com/caddyserver/caddy/issues/3718
|
|
|
|
if pathInfo == "" {
|
2023-04-27 10:46:41 +08:00
|
|
|
pathInfo, _ = repl.GetString("http.matchers.file.remainder")
|
2020-12-05 08:12:13 +08:00
|
|
|
}
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME
|
2021-06-17 23:59:08 +08:00
|
|
|
scriptFilename := caddyhttp.SanitizedPathJoin(root, scriptName)
|
2019-09-03 12:01:02 +08:00
|
|
|
|
2020-07-18 04:48:50 +08:00
|
|
|
// Ensure the SCRIPT_NAME has a leading slash for compliance with RFC3875
|
|
|
|
// Info: https://tools.ietf.org/html/rfc3875#section-4.1.13
|
|
|
|
if scriptName != "" && !strings.HasPrefix(scriptName, "/") {
|
|
|
|
scriptName = "/" + scriptName
|
|
|
|
}
|
|
|
|
|
2019-09-06 03:36:42 +08: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
|
2021-06-17 04:29:42 +08:00
|
|
|
origReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
requestScheme := "http"
|
|
|
|
if r.TLS != nil {
|
|
|
|
requestScheme = "https"
|
|
|
|
}
|
|
|
|
|
2019-12-29 07:35:29 +08:00
|
|
|
reqHost, reqPort, err := net.SplitHostPort(r.Host)
|
|
|
|
if err != nil {
|
|
|
|
// whatever, just assume there was no port
|
|
|
|
reqHost = r.Host
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:46:41 +08:00
|
|
|
authUser, _ := repl.GetString("http.auth.user.id")
|
2020-07-18 03:33:40 +08:00
|
|
|
|
2019-09-03 12:01:02 +08:00
|
|
|
// Some variables are unused but cleared explicitly to prevent
|
|
|
|
// the parent environment from interfering.
|
2021-12-03 04:23:19 +08:00
|
|
|
env = envVars{
|
2019-09-03 12:01:02 +08:00
|
|
|
// 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
|
2020-07-18 03:33:40 +08:00
|
|
|
"REMOTE_USER": authUser,
|
2019-09-06 03:36:42 +08:00
|
|
|
"REQUEST_METHOD": r.Method,
|
|
|
|
"REQUEST_SCHEME": requestScheme,
|
2019-12-29 07:35:29 +08:00
|
|
|
"SERVER_NAME": reqHost,
|
2019-09-06 03:36:42 +08:00
|
|
|
"SERVER_PROTOCOL": r.Proto,
|
2019-12-29 07:35:29 +08:00
|
|
|
"SERVER_SOFTWARE": t.serverSoftware,
|
2019-09-03 12:01:02 +08:00
|
|
|
|
|
|
|
// Other variables
|
2019-09-06 03:42:20 +08:00
|
|
|
"DOCUMENT_ROOT": root,
|
2019-09-06 03:36:42 +08:00
|
|
|
"DOCUMENT_URI": docURI,
|
|
|
|
"HTTP_HOST": r.Host, // added here, since not always part of headers
|
2021-06-17 04:29:42 +08:00
|
|
|
"REQUEST_URI": origReq.URL.RequestURI(),
|
2019-09-03 12:01:02 +08: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"] != "" {
|
2021-06-17 23:59:08 +08:00
|
|
|
env["PATH_TRANSLATED"] = caddyhttp.SanitizedPathJoin(root, pathInfo) // Info: http://www.oreilly.com/openbook/cgi/ch02_04.html
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2020-07-18 04:48:50 +08:00
|
|
|
// compliance with the CGI specification requires that
|
2022-03-03 02:24:16 +08:00
|
|
|
// the SERVER_PORT variable MUST be set to the TCP/IP port number on which this request is received from the client
|
|
|
|
// even if the port is the default port for the scheme and could otherwise be omitted from a URI.
|
|
|
|
// https://tools.ietf.org/html/rfc3875#section-4.1.15
|
2020-07-18 04:48:50 +08:00
|
|
|
if reqPort != "" {
|
|
|
|
env["SERVER_PORT"] = reqPort
|
2022-03-03 02:24:16 +08:00
|
|
|
} else if requestScheme == "http" {
|
|
|
|
env["SERVER_PORT"] = "80"
|
|
|
|
} else if requestScheme == "https" {
|
|
|
|
env["SERVER_PORT"] = "443"
|
2020-07-18 04:48:50 +08:00
|
|
|
}
|
|
|
|
|
2019-09-03 12:01:02 +08: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
|
2020-04-02 04:09:29 +08:00
|
|
|
for _, cs := range caddytls.SupportedCipherSuites() {
|
|
|
|
if cs.ID == r.TLS.CipherSuite {
|
|
|
|
env["SSL_CIPHER"] = cs.Name
|
2019-09-03 12:01:02 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add env variables from config (with support for placeholders in values)
|
2019-09-11 04:12:51 +08:00
|
|
|
for key, value := range t.EnvVars {
|
|
|
|
env[key] = repl.ReplaceAll(value, "")
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2020-03-22 21:48:21 +08:00
|
|
|
// TODO: from v1...
|
2019-09-03 12:01:02 +08:00
|
|
|
// if httpserver.CaseSensitivePath {
|
|
|
|
// return strings.Index(path, r.SplitPath)
|
|
|
|
// }
|
2020-06-13 00:07:59 +08:00
|
|
|
if len(t.SplitPath) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-03-22 21:48:21 +08:00
|
|
|
lowerPath := strings.ToLower(path)
|
|
|
|
for _, split := range t.SplitPath {
|
|
|
|
if idx := strings.Index(lowerPath, strings.ToLower(split)); idx > -1 {
|
2020-03-24 08:29:16 +08:00
|
|
|
return idx + len(split)
|
2020-03-22 21:48:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
2019-09-03 12:01:02 +08:00
|
|
|
}
|
|
|
|
|
2021-12-03 04:23:19 +08:00
|
|
|
type envVars map[string]string
|
|
|
|
|
2022-09-02 12:02:48 +08:00
|
|
|
// loggableEnv is a simple type to allow for speeding up zap log encoding.
|
|
|
|
type loggableEnv struct {
|
|
|
|
vars envVars
|
|
|
|
logCredentials bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (env loggableEnv) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
|
|
|
for k, v := range env.vars {
|
|
|
|
if !env.logCredentials {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "http_cookie", "http_set_cookie", "http_authorization", "http_proxy_authorization":
|
|
|
|
v = ""
|
|
|
|
}
|
|
|
|
}
|
2021-12-03 04:23:19 +08:00
|
|
|
enc.AddString(k, v)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:01:02 +08:00
|
|
|
// 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-06 03:42:20 +08:00
|
|
|
|
|
|
|
// Interface guards
|
|
|
|
var (
|
2022-09-02 12:02:48 +08:00
|
|
|
_ zapcore.ObjectMarshaler = (*loggableEnv)(nil)
|
2021-12-03 04:23:19 +08:00
|
|
|
|
2019-09-06 03:42:20 +08:00
|
|
|
_ caddy.Provisioner = (*Transport)(nil)
|
|
|
|
_ http.RoundTripper = (*Transport)(nil)
|
|
|
|
)
|