-
Notifications
You must be signed in to change notification settings - Fork 488
Expand file tree
/
Copy pathcontext.go
More file actions
258 lines (217 loc) · 6.55 KB
/
Copy pathcontext.go
File metadata and controls
258 lines (217 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package frankenphp
import (
"context"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
// frankenPHPContext provides contextual information about the Request to handle.
type frankenPHPContext struct {
mercureContext
ctx context.Context
documentRoot string
splitPath []string
env PreparedEnv
logger *slog.Logger
request *http.Request
worker *worker
server *Server
// idle timeout per body read; zero disables it
requestBodyTimeout time.Duration
docURI string
pathInfo string
scriptName string
scriptFilename string
requestURI string
// Whether the request is already closed by us
isDone bool
// The client's connection state as of the moment isDone was set. Captured
// once in closeContext, before close(fc.done): that unblocks the request
// dispatcher, which lets the handler return, which is itself what cancels
// request.Context() (see the net/http docs - this happens whether or not
// the client is actually still connected). Checking clientHasClosed()
// again after isDone would therefore read true for virtually any write
// following a normal fastcgi_finish_request(), not just a real abort.
clientHadClosed bool
responseWriter http.ResponseWriter
responseController *http.ResponseController
handlerParameters any
handlerReturn any
done chan any
startedAt time.Time
}
// NewRequestWithContext creates a new FrankenPHP request context.
//
// FrankenPHP does not strip request headers whose name contains an underscore
// or a dot. Because CGI maps dashes to underscores ("Foo-Bar" becomes the
// HTTP_FOO_BAR variable) and PHP maps dots to underscores when registering
// variables ("Foo.Bar" also becomes HTTP_FOO_BAR), a client-supplied "Foo_Bar"
// or "Foo.Bar" header is indistinguishable from the legitimate "Foo-Bar" in
// $_SERVER and can spoof it. This affects any such header an application or
// upstream proxy trusts (forwarded-for, auth, etc.). Drop headers containing
// an underscore or a dot before calling this function, unless you explicitly
// need (and whitelist) them. The Caddy-based server and reverse proxies such
// as nginx (underscores_in_headers off) already do this.
func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error) {
c := context.WithValue(r.Context(), contextKey, opts)
return r.WithContext(c), nil
}
func newContextFromRequest(request *http.Request, responseWriter http.ResponseWriter, s *Server, opts ...RequestOption) (*frankenPHPContext, error) {
fc := &frankenPHPContext{
ctx: request.Context(),
done: make(chan any),
startedAt: time.Now(),
server: s,
splitPath: s.splitPath,
logger: s.logger,
request: request,
documentRoot: s.root,
responseWriter: responseWriter,
}
for _, o := range opts {
if err := o(fc); err != nil {
return nil, err
}
}
// assign a worker directly if it has a request matcher
if fc.worker == nil {
for _, w := range s.workersWithRequestMatcher {
if w.matchRequest(request) {
fc.worker = w
break
}
}
}
if fc.documentRoot == "" {
if EmbeddedAppPath != "" {
fc.documentRoot = EmbeddedAppPath
} else {
var err error
if fc.documentRoot, err = os.Getwd(); err != nil {
return nil, err
}
}
}
// if no originalRequest was passed, use the URI from the actual request
// when using Caddy's http module, the original unchanged uri will be used here
// request.URL is often already rewritten to match a PHP script path
if fc.requestURI == "" {
fc.requestURI = fc.request.URL.RequestURI()
}
splitCgiPath(fc)
return fc, nil
}
// newWorkerDummyContext creates a context for worker startup
func newWorkerDummyContext(w *worker) (*frankenPHPContext, error) {
r, err := http.NewRequestWithContext(globalCtx, http.MethodGet, filepath.Base(w.fileName), nil)
if err != nil {
return nil, err
}
server := w.server
if server == nil {
// global worker, not associated with a server
server = fallbackServer
}
fc := &frankenPHPContext{
done: make(chan any),
ctx: r.Context(),
server: server,
request: r,
startedAt: time.Now(),
// startup output of a scoped worker belongs to its server's logger
logger: server.logger,
worker: w,
}
for _, o := range w.requestOptions {
if err := o(fc); err != nil {
return nil, err
}
}
splitCgiPath(fc)
return fc, nil
}
// newContextFromMessage creates a context from a message (external workers)
func newContextFromMessage(message any, rw http.ResponseWriter, ctx context.Context, w *worker) *frankenPHPContext {
server := w.server
if server == nil {
server = fallbackServer
}
if ctx == nil {
ctx = globalCtx
}
return &frankenPHPContext{
done: make(chan any),
startedAt: time.Now(),
server: server,
worker: w,
logger: server.logger,
responseWriter: rw,
handlerParameters: message,
ctx: ctx,
}
}
// closeContext sends the response to the client
func (fc *frankenPHPContext) closeContext() {
if fc.isDone {
return
}
// Snapshot before close(fc.done): that call is what eventually lets the
// handler return and cancels request.Context(), so clientHasClosed()
// must be read before it, not after.
fc.clientHadClosed = fc.clientHasClosed()
close(fc.done)
fc.isDone = true
}
// validate checks if the request should be outright rejected
func (fc *frankenPHPContext) validate() error {
if strings.Contains(fc.request.URL.Path, "\x00") {
fc.reject(ErrInvalidRequestPath)
return ErrInvalidRequestPath
}
contentLengthStr := fc.request.Header.Get("Content-Length")
if contentLengthStr != "" {
if contentLength, err := strconv.Atoi(contentLengthStr); err != nil || contentLength < 0 {
e := fmt.Errorf("%w: %q", ErrInvalidContentLengthHeader, contentLengthStr)
fc.reject(e)
return e
}
}
return nil
}
func (fc *frankenPHPContext) clientHasClosed() bool {
if fc.request == nil {
return false // not in HTTP context
}
select {
case <-fc.ctx.Done():
return true
default:
return false
}
}
// reject sends a response with the given status code and error
func (fc *frankenPHPContext) reject(err error) {
if fc.isDone {
return
}
re, ok := errors.AsType[ErrRejected](err)
if !ok {
// Should never happen
panic("only instance of ErrRejected can be passed to reject")
}
rw := fc.responseWriter
if rw != nil {
rw.WriteHeader(re.status)
_, _ = rw.Write([]byte(err.Error()))
if f, ok := rw.(http.Flusher); ok {
f.Flush()
}
}
fc.closeContext()
}