1
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-16 21:56:40 -05:00

fastcgi: Set SERVER_SOFTWARE, _NAME, and _PORT properly (fixes #2952)

This commit is contained in:
Matthew Holt 2019-12-28 16:35:29 -07:00
parent 82bebfab8a
commit 5c8b502964
No known key found for this signature in database
GPG key ID: 2A349DD577D586A5

View file

@ -18,6 +18,7 @@ import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"path"
"path/filepath"
@ -38,12 +39,6 @@ func init() {
// Transport facilitates FastCGI communication.
type Transport struct {
// TODO: Populate these
softwareName string
softwareVersion string
serverName string
serverPort string
// Use this directory as the fastcgi root directory. Defaults to the root
// directory of the parent virtual host.
Root string `json:"root,omitempty"`
@ -68,6 +63,8 @@ type Transport struct {
// The duration used to set a deadline when sending to the FastCGI server.
WriteTimeout caddy.Duration `json:"write_timeout,omitempty"`
serverSoftware string
}
// CaddyModule returns the Caddy module information.
@ -83,6 +80,10 @@ func (t *Transport) Provision(_ caddy.Context) error {
if t.Root == "" {
t.Root = "{http.vars.root}"
}
t.serverSoftware = "Caddy"
if mod := caddy.GoModule(); mod.Version != "" {
t.serverSoftware += "/" + mod.Version
}
return nil
}
@ -206,6 +207,12 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
requestScheme = "https"
}
reqHost, reqPort, err := net.SplitHostPort(r.Host)
if err != nil {
// whatever, just assume there was no port
reqHost = r.Host
}
// Some variables are unused but cleared explicitly to prevent
// the parent environment from interfering.
env = map[string]string{
@ -223,10 +230,10 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
"REMOTE_USER": "", // TODO: once there are authentication handlers, populate this
"REQUEST_METHOD": r.Method,
"REQUEST_SCHEME": requestScheme,
"SERVER_NAME": t.serverName,
"SERVER_PORT": t.serverPort,
"SERVER_NAME": reqHost,
"SERVER_PORT": reqPort,
"SERVER_PROTOCOL": r.Proto,
"SERVER_SOFTWARE": t.softwareName + "/" + t.softwareVersion,
"SERVER_SOFTWARE": t.serverSoftware,
// Other variables
"DOCUMENT_ROOT": root,