Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
fastcgi: Set default root path; add interface guards
Matthew Holt committed
Sep 5, 2019 at 13:42 UTC
d2e46c2be0c72cf49d87a9c70400ff65046e5123
2 files changed
+29
-7
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+23
-7
@@ -76,6 +76,14 @@ func (Transport) CaddyModule() caddy.ModuleInfo {
76
}
77
}
78
79
+// Provision sets up t.
80
+func (t *Transport) Provision(_ caddy.Context) error {
81
+ if t.Root == "" {
82
+ t.Root = "{http.var.root}"
83
+ }
84
+ return nil
85
+}
86
+
87
// RoundTrip implements http.RoundTripper.
88
func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
89
env, err := t.buildEnv(r)
@@ -136,6 +144,8 @@ func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
144
145
// buildEnv returns a set of CGI environment variables for the request.
146
func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
147
+ repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
148
+
149
var env map[string]string
150
151
// Separate remote IP and port; more lenient than net.SplitHostPort
@@ -151,6 +161,7 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
161
ip = strings.Replace(ip, "[", "", 1)
162
ip = strings.Replace(ip, "]", "", 1)
163
164
+ root := repl.ReplaceAll(t.Root, ".")
165
fpath := r.URL.Path
166
167
// Split path in preparation for env variables.
@@ -167,7 +178,7 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
178
scriptName = strings.TrimSuffix(scriptName, pathInfo)
179
180
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME
170
- scriptFilename := filepath.Join(t.Root, scriptName)
181
+ scriptFilename := filepath.Join(root, scriptName)
182
183
// Add vhost path prefix to scriptName. Otherwise, some PHP software will
184
// have difficulty discovering its URL.
@@ -208,13 +219,13 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
219
"REMOTE_USER": "", // TODO: once there are authentication handlers, populate this
220
"REQUEST_METHOD": r.Method,
221
"REQUEST_SCHEME": requestScheme,
211
- "SERVER_NAME": t.ServerName,
212
- "SERVER_PORT": t.ServerPort,
222
+ "SERVER_NAME": t.serverName,
223
+ "SERVER_PORT": t.serverPort,
224
"SERVER_PROTOCOL": r.Proto,
214
- "SERVER_SOFTWARE": t.SoftwareName + "/" + t.SoftwareVersion,
225
+ "SERVER_SOFTWARE": t.softwareName + "/" + t.softwareVersion,
226
227
// Other variables
217
- "DOCUMENT_ROOT": t.Root,
228
+ "DOCUMENT_ROOT": root,
229
"DOCUMENT_URI": docURI,
230
"HTTP_HOST": r.Host, // added here, since not always part of headers
231
"REQUEST_URI": reqURL.RequestURI(),
@@ -226,7 +237,7 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
237
// PATH_TRANSLATED should only exist if PATH_INFO is defined.
238
// Info: https://www.ietf.org/rfc/rfc3875 Page 14
239
if env["PATH_INFO"] != "" {
229
- env["PATH_TRANSLATED"] = filepath.Join(t.Root, pathInfo) // Info: http://www.oreilly.com/openbook/cgi/ch02_04.html
240
+ env["PATH_TRANSLATED"] = filepath.Join(root, pathInfo) // Info: http://www.oreilly.com/openbook/cgi/ch02_04.html
241
}
242
243
// Some web apps rely on knowing HTTPS or not
@@ -248,7 +259,6 @@ func (t Transport) buildEnv(r *http.Request) (map[string]string, error) {
259
}
260
261
// Add env variables from config (with support for placeholders in values)
251
- repl := r.Context().Value(caddy.ReplacerCtxKey).(caddy.Replacer)
262
for _, envVar := range t.EnvVars {
263
env[envVar[0]] = repl.ReplaceAll(envVar[1], "")
264
}
@@ -283,3 +293,9 @@ var tlsProtocolStrings = map[uint16]string{
293
}
294
295
var headerNameReplacer = strings.NewReplacer(" ", "_", "-", "_")
296
+
297
+// Interface guards
298
+var (
299
+ _ caddy.Provisioner = (*Transport)(nil)
300
+ _ http.RoundTripper = (*Transport)(nil)
301
+)
modules/caddyhttp/reverseproxy/httptransport.go
+6
@@ -200,3 +200,9 @@ type KeepAlive struct {
200
MaxIdleConnsPerHost int `json:"max_idle_conns_per_host,omitempty"`
201
IdleConnTimeout caddy.Duration `json:"idle_timeout,omitempty"` // how long should connections be kept alive when idle
202
}
203
+
204
+// Interface guards
205
+var (
206
+ _ caddy.Provisioner = (*HTTPTransport)(nil)
207
+ _ http.RoundTripper = (*HTTPTransport)(nil)
208
+)