@marcel91200 / caddy Mirror · read-only / commits / 9c0bf311
Fork 0

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

Miscellaneous cleanups / comments

Matthew Holt committed Oct 10, 2019 at 15:38 UTC 9c0bf311f93218e8f899cd3c6102868be18a4d1c
8 files changed +56 -9
context.go
+1 -1
@@ -30,7 +30,7 @@ import (
30 // with care and wrapped with derivation functions from the
31 // standard context package only if you don't need the Caddy
32 // specific features. These contexts are cancelled when the
33 -// lifetime of the modules loaded from it are over.
33 +// lifetime of the modules loaded from it is over.
34 //
35 // Use NewContext() to get a valid value (but most modules will
36 // not actually need to do this).
modules/caddyhttp/caddyhttp.go
+1 -1
@@ -348,7 +348,7 @@ func (app *App) automaticHTTPS() error {
348 })
349
350 // manage their certificates
351 - log.Printf("[INFO] Enabling automatic HTTPS certificates for %v", domainsForCerts)
351 + log.Printf("[INFO] Enabling automatic TLS certificate management for %v", domainsForCerts)
352 err := tlsApp.Manage(domainsForCerts)
353 if err != nil {
354 return fmt.Errorf("%s: managing certificate for %s: %s", srvName, domains, err)
modules/caddyhttp/replacer.go
+38 -7
@@ -19,6 +19,7 @@ import (
19 "net"
20 "net/http"
21 "net/textproto"
22 + "net/url"
23 "path"
24 "strconv"
25 "strings"
@@ -59,6 +60,15 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon
60 }
61
62 switch key {
63 + case "http.request.method":
64 + return req.Method, true
65 + case "http.request.scheme":
66 + if req.TLS != nil {
67 + return "https", true
68 + }
69 + return "http", true
70 + case "http.request.proto":
71 + return req.Proto, true
72 case "http.request.host":
73 host, _, err := net.SplitHostPort(req.Host)
74 if err != nil {
@@ -81,13 +91,8 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon
91 case "http.request.remote.port":
92 _, port, _ := net.SplitHostPort(req.RemoteAddr)
93 return port, true
84 - case "http.request.method":
85 - return req.Method, true
86 - case "http.request.scheme":
87 - if req.TLS != nil {
88 - return "https", true
89 - }
90 - return "http", true
94 +
95 + // current URI, including any internal rewrites
96 case "http.request.uri":
97 return req.URL.RequestURI(), true
98 case "http.request.uri.path":
@@ -106,6 +111,32 @@ func addHTTPVarsToReplacer(repl caddy.Replacer, req *http.Request, w http.Respon
111 qs = "?" + qs
112 }
113 return qs, true
114 +
115 + // original URI, before any internal changes
116 + case "http.request.orig_uri":
117 + u, _ := req.Context().Value(OriginalURLCtxKey).(url.URL)
118 + return u.RequestURI(), true
119 + case "http.request.orig_uri.path":
120 + u, _ := req.Context().Value(OriginalURLCtxKey).(url.URL)
121 + return u.Path, true
122 + case "http.request.orig_uri.path.file":
123 + u, _ := req.Context().Value(OriginalURLCtxKey).(url.URL)
124 + _, file := path.Split(u.Path)
125 + return file, true
126 + case "http.request.orig_uri.path.dir":
127 + u, _ := req.Context().Value(OriginalURLCtxKey).(url.URL)
128 + dir, _ := path.Split(u.Path)
129 + return dir, true
130 + case "http.request.orig_uri.query":
131 + u, _ := req.Context().Value(OriginalURLCtxKey).(url.URL)
132 + return u.RawQuery, true
133 + case "http.request.orig_uri.query_string":
134 + u, _ := req.Context().Value(OriginalURLCtxKey).(url.URL)
135 + qs := u.Query().Encode()
136 + if qs != "" {
137 + qs = "?" + qs
138 + }
139 + return qs, true
140 }
141
142 // hostname labels
modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go
+1
@@ -110,6 +110,7 @@ func (t Transport) RoundTrip(r *http.Request) (*http.Response, error) {
110
111 fcgiBackend, err := DialContext(ctx, network, address)
112 if err != nil {
113 + // TODO: wrap in a special error type if the dial failed, so retries can happen if enabled
114 return nil, fmt.Errorf("dialing backend: %v", err)
115 }
116 // fcgiBackend gets closed when response body is closed (see clientCloser)
modules/caddyhttp/reverseproxy/reverseproxy.go
+2
@@ -243,6 +243,8 @@ func (h *Handler) Cleanup() error {
243 close(h.HealthChecks.Active.stopChan)
244 }
245
246 + // TODO: Close keepalive connections on reload? https://github.com/caddyserver/caddy/pull/2507/files#diff-70219fd88fe3f36834f474ce6537ed26R762
247 +
248 // remove hosts from our config from the pool
249 for _, upstream := range h.Upstreams {
250 hosts.Delete(upstream.dialInfo.String())
modules/caddyhttp/routes.go
+3
@@ -141,6 +141,9 @@ func (routes RouteList) BuildCompositeRoute(req *http.Request) Handler {
141 func wrapMiddleware(mh MiddlewareHandler) Middleware {
142 return func(next HandlerFunc) HandlerFunc {
143 return func(w http.ResponseWriter, r *http.Request) error {
144 + // TODO: We could wait to evaluate matchers here, just eval
145 + // the next matcher and choose the next route...
146 +
147 // TODO: This is where request tracing could be implemented; also
148 // see below to trace the responder as well
149 // TODO: Trace a diff of the request, would be cool too! see what changed since the last middleware (host, headers, URI...)
replacer.go
+7
@@ -19,6 +19,7 @@ import (
19 "path/filepath"
20 "runtime"
21 "strings"
22 + "time"
23 )
24
25 // Replacer can replace values in strings.
@@ -156,11 +157,17 @@ func globalDefaultReplacements(key string) (string, bool) {
157 return runtime.GOOS, true
158 case "system.arch":
159 return runtime.GOARCH, true
160 + case "time.now.common_log":
161 + return nowFunc().Format("02/Jan/2006:15:04:05 -0700"), true
162 }
163
164 return "", false
165 }
166
167 +// nowFunc is a variable so tests can change it
168 +// in order to obtain a deterministic time.
169 +var nowFunc = time.Now
170 +
171 // ReplacerCtxKey is the context key for a replacer.
172 const ReplacerCtxKey CtxKey = "replacer"
173
storage.go
+3
@@ -59,3 +59,6 @@ func dataDir() string {
59 }
60 return filepath.Join(baseDir, "caddy")
61 }
62 +
63 +// TODO: Consider using Go 1.13's os.UserConfigDir() (https://golang.org/pkg/os/#UserConfigDir)
64 +// if we are going to store the last-loaded config anywhere