@@ -2,6 +2,8 @@ package frankenphp
22
33import (
44 "context"
5+ "errors"
6+ "fmt"
57 "log/slog"
68 "net/http"
79 "os"
@@ -117,23 +119,23 @@ func (fc *frankenPHPContext) closeContext() {
117119}
118120
119121// validate checks if the request should be outright rejected
120- func (fc * frankenPHPContext ) validate () bool {
122+ func (fc * frankenPHPContext ) validate () error {
121123 if strings .Contains (fc .request .URL .Path , "\x00 " ) {
122- fc .rejectBadRequest ( "Invalid request path" )
124+ fc .reject ( ErrInvalidRequestPath )
123125
124- return false
126+ return ErrInvalidRequestPath
125127 }
126128
127129 contentLengthStr := fc .request .Header .Get ("Content-Length" )
128130 if contentLengthStr != "" {
129131 if contentLength , err := strconv .Atoi (contentLengthStr ); err != nil || contentLength < 0 {
130- fc . rejectBadRequest ( "invalid Content-Length header: " + contentLengthStr )
132+ e := fmt . Errorf ( "%w: %s" , ErrInvalidContentLengthHeader , contentLengthStr )
131133
132- return false
134+ fc . reject ( e )
133135 }
134136 }
135137
136- return true
138+ return nil
137139}
138140
139141func (fc * frankenPHPContext ) clientHasClosed () bool {
@@ -149,27 +151,27 @@ func (fc *frankenPHPContext) clientHasClosed() bool {
149151 }
150152}
151153
152- // reject sends a response with the given status code and message
153- func (fc * frankenPHPContext ) reject (statusCode int , message string ) error {
154+ // reject sends a response with the given status code and error
155+ func (fc * frankenPHPContext ) reject (err error ) {
154156 if fc .isDone {
155- return nil
157+ return
158+ }
159+
160+ re := & ErrRejected {}
161+ if ! errors .As (err , re ) {
162+ // Should never happen
163+ panic ("only instance of ErrRejected can be passed to reject" )
156164 }
157165
158166 rw := fc .responseWriter
159167 if rw != nil {
160- rw .WriteHeader (statusCode )
161- _ , _ = rw .Write ([]byte (message ))
168+ rw .WriteHeader (re . status )
169+ _ , _ = rw .Write ([]byte (err . Error () ))
162170
163171 if f , ok := rw .(http.Flusher ); ok {
164172 f .Flush ()
165173 }
166174 }
167175
168176 fc .closeContext ()
169-
170- return ErrMaxTimeExceeded
171- }
172-
173- func (fc * frankenPHPContext ) rejectBadRequest (message string ) {
174- fc .reject (http .StatusBadRequest , message )
175177}
0 commit comments