-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.go
More file actions
223 lines (189 loc) · 5.28 KB
/
Copy pathplugin.go
File metadata and controls
223 lines (189 loc) · 5.28 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
package send
import (
"context"
"errors"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
rrcontext "github.com/roadrunner-server/context"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
jprop "go.opentelemetry.io/contrib/propagators/jaeger"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/trace"
)
const (
PluginName string = "sendfile"
ContentTypeKey string = "Content-Type"
ContentTypeVal string = "application/octet-stream"
xSendHeader string = "X-Sendfile"
bufSize int = 10 * 1024 * 1024 // 10MB chunks
)
type Logger interface {
NamedLogger(name string) *slog.Logger
}
type Plugin struct {
log *slog.Logger
writersPool sync.Pool
prop propagation.TextMapPropagator
}
func (p *Plugin) Init(log Logger) error {
p.log = log.NamedLogger(PluginName)
p.writersPool = sync.Pool{
New: func() any {
wr := new(writer)
wr.code = http.StatusOK
wr.data = make([]byte, 0, 10)
wr.hdrToSend = make(map[string][]string, 2)
return wr
},
}
p.prop = propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, jprop.Jaeger{})
return nil
}
// Middleware is an HTTP plugin middleware to serve headers
func (p *Plugin) Middleware(next http.Handler) http.Handler {
// Define the http.HandlerFunc
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rrWriter := p.getWriter()
defer func() {
p.putWriter(rrWriter)
_ = r.Body.Close()
}()
var otelVal string
var tp trace.TracerProvider
if val, ok := r.Context().Value(rrcontext.OtelTracerNameKey).(string); ok {
otelVal = val
tp = trace.SpanFromContext(r.Context()).TracerProvider()
var ctx context.Context
ctx, span := tp.Tracer(val, trace.WithSchemaURL(semconv.SchemaURL),
trace.WithInstrumentationVersion(otelhttp.Version)).
Start(r.Context(), PluginName, trace.WithSpanKind(trace.SpanKindInternal))
// inject
p.prop.Inject(ctx, propagation.HeaderCarrier(r.Header))
r = r.WithContext(ctx)
span.End()
}
next.ServeHTTP(rrWriter, r)
// start a second span for the post-processing (X-Sendfile handling)
var postSpan trace.Span
if otelVal != "" {
_, postSpan = tp.Tracer(otelVal, trace.WithSchemaURL(semconv.SchemaURL),
trace.WithInstrumentationVersion(otelhttp.Version)).
Start(r.Context(), PluginName+":post", trace.WithSpanKind(trace.SpanKindInternal))
}
defer func() {
if postSpan != nil {
postSpan.End()
}
}()
// capture the X-Sendfile path once
path := rrWriter.Header().Get(xSendHeader)
// if there is no X-Sendfile header from the PHP worker, just return
if path == "" {
// re-add all headers from the worker
addHeaders(w.Header(), rrWriter.hdrToSend)
// write original
w.WriteHeader(rrWriter.code)
if len(rrWriter.data) > 0 {
// write a body if exists
_, err := w.Write(rrWriter.data)
if err != nil {
p.log.Error("failed to write data to the response", "error", err)
}
}
return
}
// delete the original X-Sendfile header
rrWriter.Header().Del(xSendHeader)
// re-add original headers
addHeaders(w.Header(), rrWriter.hdrToSend)
// do not allow paths like ../../resource, security
// only specified folder and resources in it
// see: https://lgtm.com/rules/1510366186013/
cleanPath := filepath.Clean(path)
if strings.Contains(cleanPath, "..") {
w.WriteHeader(http.StatusForbidden)
return
}
// check if the file exists; use the cleaned path for all subsequent I/O
fs, err := os.Stat(cleanPath)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
// empty files have nothing to send
size := fs.Size()
if size == 0 {
w.Header().Set(ContentTypeKey, ContentTypeVal)
w.WriteHeader(http.StatusOK)
return
}
f, err := os.Open(cleanPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer f.Close()
// set content-type before writing any data
w.Header().Set(ContentTypeKey, ContentTypeVal)
buf := make([]byte, min(size, int64(bufSize)))
off := 0
for {
buf = buf[:cap(buf)]
n, err := f.ReadAt(buf, int64(off))
if err != nil {
if errors.Is(err, io.EOF) {
if n > 0 {
_, werr := w.Write(buf[:n])
if werr != nil {
p.log.Error("write response", "error", werr)
return
}
if fl, ok := w.(http.Flusher); ok {
fl.Flush()
}
}
break
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, werr := w.Write(buf[:n])
if werr != nil {
// we can't write response into the response writer
p.log.Error("write response", "error", werr)
return
}
// send data to the user
if fl, ok := w.(http.Flusher); ok {
fl.Flush()
}
off += n
}
})
}
func (p *Plugin) Name() string {
return PluginName
}
func (p *Plugin) getWriter() *writer {
return p.writersPool.Get().(*writer)
}
func (p *Plugin) putWriter(w *writer) {
w.code = http.StatusOK
w.data = w.data[:0]
clear(w.hdrToSend)
p.writersPool.Put(w)
}
// addHeaders copies every value of the worker-supplied headers into dst.
func addHeaders(dst http.Header, src map[string][]string) {
for k, vals := range src {
for _, v := range vals {
dst.Add(k, v)
}
}
}