ServeHTTP implements caddyhttp.MiddlewareHandler.
(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler)
| 198 | |
| 199 | // ServeHTTP implements caddyhttp.MiddlewareHandler. |
| 200 | func (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) |
nothing calls this directly
no test coverage detected