feat: set a custom Server header

# Conflicts:
#	caddy/module.go
#	frankenphp.go
This commit is contained in:
Kévin Dunglas
2025-11-04 00:41:59 +01:00
parent 63168e087e
commit 724c0b11ca
2 changed files with 12 additions and 2 deletions

View File

@@ -22,6 +22,8 @@ import (
"github.com/dunglas/frankenphp/internal/fastabs"
)
var serverHeader = []string{"FrankenPHP Caddy"}
// FrankenPHPModule represents the "php_server" and "php" directives in the Caddyfile
// they are responsible for forwarding requests to FrankenPHP via "ServeHTTP"
//
@@ -197,6 +199,8 @@ func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ c
return caddyhttp.Error(http.StatusInternalServerError, err)
}
// TODO: set caddyhttp.ServerHeader when https://github.com/caddyserver/caddy/pull/7338 will be released
w.Header()["Server"] = serverHeader
if err = frankenphp.ServeHTTP(w, fr); err != nil && !errors.As(err, &frankenphp.ErrRejected{}) {
return caddyhttp.Error(http.StatusInternalServerError, err)
}

View File

@@ -41,8 +41,6 @@ import (
type contextKeyStruct struct{}
var contextKey = contextKeyStruct{}
var (
ErrInvalidRequest = errors.New("not a FrankenPHP request")
ErrAlreadyStarted = errors.New("FrankenPHP is already started")
@@ -56,6 +54,9 @@ var (
ErrInvalidContentLengthHeader = ErrRejected{"invalid Content-Length header", http.StatusBadRequest}
ErrMaxWaitTimeExceeded = ErrRejected{"maximum request handling time exceeded", http.StatusServiceUnavailable}
contextKey = contextKeyStruct{}
serverHeader = []string{"FrankenPHP"}
isRunning bool
onServerShutdown []func()
@@ -333,6 +334,11 @@ func Shutdown() {
// ServeHTTP executes a PHP script according to the given context.
func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error {
h := responseWriter.Header()
if h["Server"] == nil {
h["Server"] = serverHeader
}
if !isRunning {
return ErrNotRunning
}