@marcel91200 / caddy Mirror · read-only / commits / 7960b425
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

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().

Matthew Holt committed May 11, 2020 at 12:14 UTC 7960b4259d976810691f1085332d4761abab9928
2 files changed +22 -14
modules/caddyhttp/routes.go
+2 -2
@@ -113,7 +113,7 @@ func (r Route) Empty() bool {
113 // create a middleware chain.
114 type RouteList []Route
115
116 -// Provision sets up both the matchers and handlers in the route.
116 +// Provision sets up both the matchers and handlers in the routes.
117 func (routes RouteList) Provision(ctx caddy.Context) error {
118 err := routes.ProvisionMatchers(ctx)
119 if err != nil {
@@ -284,7 +284,7 @@ type RawMatcherSets []caddy.ModuleMap
284 type MatcherSets []MatcherSet
285
286 // AnyMatch returns true if req matches any of the
287 -// matcher sets in mss or if there are no matchers,
287 +// matcher sets in ms or if there are no matchers,
288 // in which case the request always matches.
289 func (ms MatcherSets) AnyMatch(req *http.Request) bool {
290 for _, m := range ms {
modules/caddyhttp/server.go
+20 -12
@@ -145,19 +145,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
145 }
146 }
147
148 - // set up the context for the request
148 repl := caddy.NewReplacer()
150 - ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
151 - ctx = context.WithValue(ctx, ServerCtxKey, s)
152 - ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
153 - ctx = context.WithValue(ctx, routeGroupCtxKey, make(map[string]struct{}))
154 - var url2 url.URL // avoid letting this escape to the heap
155 - ctx = context.WithValue(ctx, OriginalRequestCtxKey, originalRequest(r, &url2))
156 - r = r.WithContext(ctx)
157 -
158 - // once the pointer to the request won't change
159 - // anymore, finish setting up the replacer
160 - addHTTPVarsToReplacer(repl, r, w)
149 + r = PrepareRequest(r, repl, w, s)
150
151 // encode the request for logging purposes before
152 // it enters any handler chain; this is necessary
@@ -470,6 +459,25 @@ func (slc ServerLogConfig) getLoggerName(host string) string {
459 return slc.DefaultLoggerName
460 }
461
462 +// PrepareRequest fills the request r for use in a Caddy HTTP handler chain. w and s can
463 +// be nil, but the handlers will lose response placeholders and access to the server.
464 +func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter, s *Server) *http.Request {
465 + // set up the context for the request
466 + ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
467 + ctx = context.WithValue(ctx, ServerCtxKey, s)
468 + ctx = context.WithValue(ctx, VarsCtxKey, make(map[string]interface{}))
469 + ctx = context.WithValue(ctx, routeGroupCtxKey, make(map[string]struct{}))
470 + var url2 url.URL // avoid letting this escape to the heap
471 + ctx = context.WithValue(ctx, OriginalRequestCtxKey, originalRequest(r, &url2))
472 + r = r.WithContext(ctx)
473 +
474 + // once the pointer to the request won't change
475 + // anymore, finish setting up the replacer
476 + addHTTPVarsToReplacer(repl, r, w)
477 +
478 + return r
479 +}
480 +
481 // errLogValues inspects err and returns the status code
482 // to use, the error log message, and any extra fields.
483 // If err is a HandlerError, the returned values will