Skip to content

Commit 724c0b1

Browse files
committed
feat: set a custom Server header
# Conflicts: # caddy/module.go # frankenphp.go
1 parent 63168e0 commit 724c0b1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

caddy/module.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"github.com/dunglas/frankenphp/internal/fastabs"
2323
)
2424

25+
var serverHeader = []string{"FrankenPHP Caddy"}
26+
2527
// FrankenPHPModule represents the "php_server" and "php" directives in the Caddyfile
2628
// they are responsible for forwarding requests to FrankenPHP via "ServeHTTP"
2729
//
@@ -197,6 +199,8 @@ func (f *FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ c
197199
return caddyhttp.Error(http.StatusInternalServerError, err)
198200
}
199201

202+
// TODO: set caddyhttp.ServerHeader when https://github.com/caddyserver/caddy/pull/7338 will be released
203+
w.Header()["Server"] = serverHeader
200204
if err = frankenphp.ServeHTTP(w, fr); err != nil && !errors.As(err, &frankenphp.ErrRejected{}) {
201205
return caddyhttp.Error(http.StatusInternalServerError, err)
202206
}

frankenphp.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ import (
4141

4242
type contextKeyStruct struct{}
4343

44-
var contextKey = contextKeyStruct{}
45-
4644
var (
4745
ErrInvalidRequest = errors.New("not a FrankenPHP request")
4846
ErrAlreadyStarted = errors.New("FrankenPHP is already started")
@@ -56,6 +54,9 @@ var (
5654
ErrInvalidContentLengthHeader = ErrRejected{"invalid Content-Length header", http.StatusBadRequest}
5755
ErrMaxWaitTimeExceeded = ErrRejected{"maximum request handling time exceeded", http.StatusServiceUnavailable}
5856

57+
contextKey = contextKeyStruct{}
58+
serverHeader = []string{"FrankenPHP"}
59+
5960
isRunning bool
6061
onServerShutdown []func()
6162

@@ -333,6 +334,11 @@ func Shutdown() {
333334

334335
// ServeHTTP executes a PHP script according to the given context.
335336
func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error {
337+
h := responseWriter.Header()
338+
if h["Server"] == nil {
339+
h["Server"] = serverHeader
340+
}
341+
336342
if !isRunning {
337343
return ErrNotRunning
338344
}

0 commit comments

Comments
 (0)