summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/server.go
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-05-11 12:14:47 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-05-11 12:14:47 -0600
commit7960b4259d976810691f1085332d4761abab9928 (patch)
treef352971746e571fd178dede975106c751a5de7b5 /modules/caddyhttp/server.go
parent2c91688f39a80afc48e232e989f13aafbc782cf5 (diff)
caddyhttp: Minor refactoring for preparing requests
While building a layer4 app for Caddy, I discovered that we need the ability to fill a request's context just like the HTTP server does, hence this exported function PrepareRequest().
Diffstat (limited to 'modules/caddyhttp/server.go')
-rw-r--r--modules/caddyhttp/server.go32
1 files changed, 20 insertions, 12 deletions
diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go
index a4fda28..55da253 100644
--- a/modules/caddyhttp/server.go
+++ b/modules/caddyhttp/server.go
@@ -145,19 +145,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
- // set up the context for the request
repl := caddy.NewReplacer()
- ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
- ctx = context.WithValue(ctx, ServerCtxKey, s)
- ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
- ctx = context.WithValue(ctx, routeGroupCtxKey, make(map[string]struct{}))
- var url2 url.URL // avoid letting this escape to the heap
- ctx = context.WithValue(ctx, OriginalRequestCtxKey, originalRequest(r, &url2))
- r = r.WithContext(ctx)
-
- // once the pointer to the request won't change
- // anymore, finish setting up the replacer
- addHTTPVarsToReplacer(repl, r, w)
+ r = PrepareRequest(r, repl, w, s)
// encode the request for logging purposes before
// it enters any handler chain; this is necessary
@@ -470,6 +459,25 @@ func (slc ServerLogConfig) getLoggerName(host string) string {
return slc.DefaultLoggerName
}
+// PrepareRequest fills the request r for use in a Caddy HTTP handler chain. w and s can
+// be nil, but the handlers will lose response placeholders and access to the server.
+func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter, s *Server) *http.Request {
+ // set up the context for the request
+ ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
+ ctx = context.WithValue(ctx, ServerCtxKey, s)
+ ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
+ ctx = context.WithValue(ctx, routeGroupCtxKey, make(map[string]struct{}))
+ var url2 url.URL // avoid letting this escape to the heap
+ ctx = context.WithValue(ctx, OriginalRequestCtxKey, originalRequest(r, &url2))
+ r = r.WithContext(ctx)
+
+ // once the pointer to the request won't change
+ // anymore, finish setting up the replacer
+ addHTTPVarsToReplacer(repl, r, w)
+
+ return r
+}
+
// errLogValues inspects err and returns the status code
// to use, the error log message, and any extra fields.
// If err is a HandlerError, the returned values will