MCPcopy Index your code
hub / github.com/php/frankenphp / ServeHTTP

Method ServeHTTP

caddy/module.go:200–249  ·  view source on GitHub ↗

ServeHTTP implements caddyhttp.MiddlewareHandler.

(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler)

Source from the content-addressed store, hash-verified

198
199// ServeHTTP implements caddyhttp.MiddlewareHandler.
200func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
201 ctx := r.Context()
202 repl := ctx.Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
203
204 documentRoot := f.resolvedDocumentRoot
205
206 opts := make([]frankenphp.RequestOption, 0, len(f.requestOptions)+4)
207 opts = append(opts, f.requestOptions...)
208
209 if documentRoot == "" {
210 documentRoot = repl.ReplaceKnown(f.Root, "")
211 if documentRoot == "" && frankenphp.EmbeddedAppPath != "" {
212 documentRoot = frankenphp.EmbeddedAppPath
213 }
214
215 // If we do not have a resolved document root, then we cannot resolve the symlink of our cwd because it may
216 // resolve to a different directory than the one we are currently in.
217 // This is especially important if there are workers running.
218 opts = append(opts, frankenphp.WithRequestDocumentRoot(documentRoot, false))
219 }
220
221 if f.preparedEnvNeedsReplacement {
222 env := make(frankenphp.PreparedEnv, len(f.Env))
223 for k, v := range f.preparedEnv {
224 env[k] = repl.ReplaceKnown(v, "")
225 }
226
227 opts = append(opts, frankenphp.WithRequestPreparedEnv(env))
228 }
229
230 workerName := ""
231 for _, w := range f.Workers {
232 if w.matchesPath(r, documentRoot) {
233 workerName = w.Name
234 break
235 }
236 }
237
238 fr, err := frankenphp.NewRequestWithContext(
239 r,
240 append(
241 opts,
242 frankenphp.WithOriginalRequest(new(ctx.Value(caddyhttp.OriginalRequestCtxKey).(http.Request))),
243 frankenphp.WithWorkerName(workerName),
244 )...,
245 )
246
247 if err != nil {
248 return caddyhttp.Error(http.StatusInternalServerError, err)
249 }
250
251 if err = frankenphp.ServeHTTP(w, fr); err != nil && !errors.As(err, &frankenphp.ErrRejected{}) {
252 return caddyhttp.Error(http.StatusInternalServerError, err)

Callers

nothing calls this directly

Calls 7

WithRequestDocumentRootFunction · 0.92
WithRequestPreparedEnvFunction · 0.92
NewRequestWithContextFunction · 0.92
WithOriginalRequestFunction · 0.92
WithWorkerNameFunction · 0.92
matchesPathMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected