/
http_server.go
544 lines (509 loc) · 16.9 KB
/
http_server.go
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// Copyright 2017 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fhttp // import "fortio.org/fortio/fhttp"
// pprof import to get /debug/pprof endpoints on a mux through SetupPPROF.
import (
"bytes"
"flag"
"fmt"
"io"
"net"
"net/http"
"net/http/pprof"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync/atomic"
"time"
"fortio.org/fortio/dflag"
"fortio.org/fortio/fnet"
"fortio.org/fortio/jrpc"
"fortio.org/fortio/log"
"fortio.org/fortio/version"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
// -- Echo Server --
var (
// Start time of the server (used in debug handler for uptime).
startTime time.Time
// EchoRequests is the number of request received. Only updated in Debug mode.
EchoRequests int64
// TODO find a way to only include this on binaries and not library mode (#433).
defaultEchoServerParams = dflag.DynString(flag.CommandLine, "echo-server-default-params", "",
"Default parameters/querystring to use if there isn't one provided explicitly. E.g \"status=404&delay=3s\"")
fetch2CopiesAllHeader = dflag.DynBool(flag.CommandLine, "proxy-all-headers", true,
"Determines if only tracing or all headers (and cookies) are copied from request on the fetch2 ui/server endpoint")
serverIdleTimeout = dflag.DynDuration(flag.CommandLine, "server-idle-timeout", 30*time.Second, "Default IdleTimeout for servers")
)
// EchoHandler is an http server handler echoing back the input.
func EchoHandler(w http.ResponseWriter, r *http.Request) {
if log.LogVerbose() {
LogRequest(r, "Echo") // will also print headers
}
defaultParams := defaultEchoServerParams.Get()
hasQuestionMark := strings.Contains(r.RequestURI, "?")
if !hasQuestionMark && len(defaultParams) > 0 {
newQS := r.RequestURI + "?" + defaultParams
log.LogVf("Adding default base query string %q to %v trying %q", defaultParams, r.URL, newQS)
nr := *r
var err error
nr.URL, err = url.ParseRequestURI(newQS)
if err != nil {
log.Errf("Unexpected error parsing echo-server-default-params: %v", err)
} else {
nr.Form = nil
r = &nr
}
}
data, err := io.ReadAll(r.Body) // must be done before calling FormValue
if err != nil {
log.Errf("Error reading %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Debugf("Read %d", len(data))
handleCommonArgs(w, r)
statusStr := r.FormValue("status")
var status int
if statusStr != "" {
status = generateStatus(statusStr)
} else {
status = http.StatusOK
}
gzip := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") && generateGzip(r.FormValue("gzip"))
if gzip {
gwz := NewGzipHTTPResponseWriter(w)
defer gwz.Close()
w = gwz
}
size := generateSize(r.FormValue("size"))
if size >= 0 {
log.LogVf("Writing %d size with %d status", size, status)
writePayload(w, status, size)
return
}
// echo back the Content-Type and Content-Length in the response
for _, k := range []string{"Content-Type", "Content-Length"} {
if v := r.Header.Get(k); v != "" {
jrpc.SetHeaderIfMissing(w.Header(), k, v)
}
}
w.WriteHeader(status)
if _, err = w.Write(data); err != nil {
log.Errf("Error writing response %v to %v", err, r.RemoteAddr)
}
}
// handleCommonArgs common flags for debug and echo handlers.
// Must be called after body is read.
func handleCommonArgs(w http.ResponseWriter, r *http.Request) {
dur := generateDelay(r.FormValue("delay"))
if dur > 0 {
log.LogVf("Sleeping for %v", dur)
time.Sleep(dur)
}
if log.LogDebug() {
// Note this easily lead to contention, debug mode only (or low qps).
rqNum := atomic.AddInt64(&EchoRequests, 1)
log.Debugf("Request # %v", rqNum)
}
if generateClose(r.FormValue("close")) {
log.Debugf("Adding Connection:close / will close socket")
w.Header().Set("Connection", "close")
}
// process header(s) args, must be before size to compose properly
for _, hdr := range r.Form["header"] {
log.LogVf("Adding requested header %s", hdr)
if len(hdr) == 0 {
continue
}
s := strings.SplitN(hdr, ":", 2)
if len(s) != 2 {
log.Errf("invalid extra header '%s', expecting Key: Value", hdr)
continue
}
w.Header().Add(s[0], s[1])
}
}
func writePayload(w http.ResponseWriter, status int, size int) {
jrpc.SetHeaderIfMissing(w.Header(), "Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(size))
w.WriteHeader(status)
n, err := w.Write(fnet.Payload[:size])
if err != nil || n != size {
log.Errf("Error writing payload of size %d: %d %v", size, n, err)
}
}
func closingServer(listener net.Listener) error {
var err error
for {
var c net.Conn
c, err = listener.Accept()
if err != nil {
log.Errf("Accept error in dummy server %v", err)
break
}
log.LogVf("Got connection from %v, closing", c.RemoteAddr())
err = c.Close()
if err != nil {
log.Errf("Close error in dummy server %v", err)
break
}
}
return err
}
// HTTPServer creates an http server named name on address/port port.
// Port can include binding address and/or be port 0.
func HTTPServer(name string, port string) (*http.ServeMux, net.Addr) {
m := http.NewServeMux()
return m, HTTPServerWithHandler(name, port, m)
}
// HTTPServerWithHandler creates and h2c compatible server named name on address/port port.
// Port can include binding address and/or be port 0.
// Takes in a handler.
func HTTPServerWithHandler(name string, port string, hdlr http.Handler) net.Addr {
h2s := &http2.Server{}
s := &http.Server{
ReadHeaderTimeout: serverIdleTimeout.Get(),
IdleTimeout: serverIdleTimeout.Get(),
Handler: h2c.NewHandler(hdlr, h2s),
}
listener, addr := fnet.Listen(name, port)
if listener == nil {
return nil // error already logged
}
go func() {
err := s.Serve(listener)
if err != nil {
log.Fatalf("Unable to serve %s on %s: %v", name, addr.String(), err)
}
}()
return addr
}
// DynamicHTTPServer listens on an available port, sets up an http or a closing
// server simulating an https server (when closing is true) server on it and
// returns the listening port and mux to which one can attach handlers to.
// Note: in a future version of istio, the closing will be actually be secure
// on/off and create an https server instead of a closing server.
// As this is a dynamic tcp socket server, the address is TCP.
func DynamicHTTPServer(closing bool) (*http.ServeMux, *net.TCPAddr) {
if !closing {
mux, addr := HTTPServer("dynamic", "0")
return mux, addr.(*net.TCPAddr)
}
// Note: we actually use the fact it's not supported as an error server for tests - need to change that
log.Errf("Secure setup not yet supported. Will just close incoming connections for now")
listener, addr := fnet.Listen("closing server", "0")
// err = http.ServeTLS(listener, nil, "", "") // go 1.9
go func() {
err := closingServer(listener)
if err != nil {
log.Fatalf("Unable to serve closing server on %s: %v", addr.String(), err)
}
}()
return nil, addr.(*net.TCPAddr)
}
/*
// DebugHandlerTemplate returns debug/useful info on the http requet.
// slower heavier but nicer source code version of DebugHandler
func DebugHandlerTemplate(w http.ResponseWriter, r *http.Request) {
log.LogVf("%v %v %v %v", r.Method, r.URL, r.Proto, r.RemoteAddr)
hostname, _ := os.Hostname()
data, err := io.ReadAll(r.Body)
if err != nil {
log.Errf("Error reading %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Note: this looks nicer but is about 2x slower / less qps / more cpu and 25% bigger executable than doing the writes oneself:
const templ = `Φορτίο version {{.Version}} echo debug server on {{.Hostname}} - request from {{.R.RemoteAddr}}
{{.R.Method}} {{.R.URL}} {{.R.Proto}}
headers:
{{ range $name, $vals := .R.Header }}{{range $val := $vals}}{{$name}}: {{ $val }}
{{end}}{{end}}
body:
{{.Body}}
{{if .DumpEnv}}
environment:
{{ range $idx, $e := .Env }}
{{$e}}{{end}}
{{end}}`
t := template.Must(template.New("debugOutput").Parse(templ))
err = t.Execute(w, &struct {
R *http.Request
Hostname string
Version string
Body string
DumpEnv bool
Env []string
}{r, hostname, Version, DebugSummary(data, 512), r.FormValue("env") == "dump", os.Environ()})
if err != nil {
Critf("Template execution failed: %v", err)
}
w.Header().Set("Content-Type", "text/plain; charset=UTF-8")
}
*/
// DebugHandler returns debug/useful info to http client.
func DebugHandler(w http.ResponseWriter, r *http.Request) {
LogRequest(r, "Debug")
var buf bytes.Buffer
buf.WriteString("Φορτίο version ")
buf.WriteString(version.Long())
buf.WriteString(" echo debug server up for ")
buf.WriteString(fmt.Sprint(RoundDuration(time.Since(startTime))))
buf.WriteString(" on ")
hostname, _ := os.Hostname()
buf.WriteString(hostname)
buf.WriteString(" - request from ")
buf.WriteString(r.RemoteAddr)
buf.WriteString("\n\n")
buf.WriteString(r.Method)
buf.WriteByte(' ')
buf.WriteString(r.URL.String())
buf.WriteByte(' ')
buf.WriteString(r.Proto)
buf.WriteString("\n\nheaders:\n\n")
// Host is removed from headers map and put here (!)
buf.WriteString("Host: ")
buf.WriteString(r.Host)
keys := make([]string, 0, len(r.Header))
for k := range r.Header {
keys = append(keys, k)
}
sort.Strings(keys)
for _, name := range keys {
buf.WriteByte('\n')
buf.WriteString(name)
buf.WriteString(": ")
first := true
headers := r.Header[name]
for _, h := range headers {
if !first {
buf.WriteByte(',')
}
buf.WriteString(h)
first = false
}
}
data, err := io.ReadAll(r.Body)
if err != nil {
/*
expected := r.ContentLength
if expected < 0 {
expected = 0 // GET have -1 content length
}
dataBuffer := make([]byte, expected)
numRead, err := r.Body.Read(dataBuffer)
log.LogVf("read %d/%d: %v", numRead, expected, err)
data := dataBuffer[0:numRead]
if err != nil && !errors.Is(err, io.EOF) {
*/
log.Errf("Error reading %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
buf.WriteString("\n\nbody:\n\n")
buf.WriteString(DebugSummary(data, 512))
buf.WriteByte('\n')
if r.FormValue("env") == "dump" {
buf.WriteString("\nenvironment:\n\n")
for _, v := range os.Environ() {
buf.WriteString(v)
buf.WriteByte('\n')
}
}
handleCommonArgs(w, r)
jrpc.SetHeaderIfMissing(w.Header(), "Content-Type", "text/plain; charset=UTF-8")
if _, err = w.Write(buf.Bytes()); err != nil {
log.Errf("Error writing response %v to %v", err, r.RemoteAddr)
}
/*
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
*/
}
// CacheOn sets the header for indefinite caching.
func CacheOn(w http.ResponseWriter) {
w.Header().Set("Cache-Control", "max-age=365000000, immutable")
}
// EchoDebugPath returns the additional echo handler path behind debugPath (ie /debug -> /debug/echo/).
func EchoDebugPath(debugPath string) string {
return strings.TrimSuffix(debugPath, "/") + "/echo/"
}
// Serve starts a debug / echo http server on the given port.
// Returns the mux and addr where the listening socket is bound.
// The .Port can be retrieved from it when requesting the 0 port as
// input for dynamic http server.
func Serve(port, debugPath string) (*http.ServeMux, net.Addr) {
startTime = time.Now()
mux, addr := HTTPServer("http-echo", port)
if addr == nil {
return nil, nil // error already logged
}
if debugPath != "" {
mux.Handle(debugPath, Gzip(http.HandlerFunc(DebugHandler)))
mux.HandleFunc(EchoDebugPath(debugPath), EchoHandler) // Fix #524
}
mux.HandleFunc("/", EchoHandler)
return mux, addr
}
// ServeTCP is Serve() but restricted to TCP (return address is assumed
// to be TCP - will panic for unix domain).
func ServeTCP(port, debugPath string) (*http.ServeMux, *net.TCPAddr) {
mux, addr := Serve(port, debugPath)
if addr == nil {
return nil, nil // error already logged
}
return mux, addr.(*net.TCPAddr)
}
// -- formerly in ui handler
// SetupPPROF add pprof to the mux (mirror the init() of http pprof).
func SetupPPROF(mux *http.ServeMux) {
mux.HandleFunc("/debug/pprof/", LogAndCall("pprof:index", pprof.Index))
mux.HandleFunc("/debug/pprof/cmdline", LogAndCall("pprof:cmdline", pprof.Cmdline))
mux.HandleFunc("/debug/pprof/profile", LogAndCall("pprof:profile", pprof.Profile))
mux.HandleFunc("/debug/pprof/symbol", LogAndCall("pprof:symbol", pprof.Symbol))
mux.HandleFunc("/debug/pprof/trace", LogAndCall("pprof:trace", pprof.Trace))
}
// -- Fetch er (simple http proxy) --
var proxyClient = CreateProxyClient()
// FetcherHandler2 is the handler for the fetcher/proxy that supports h2 input and makes a
// new request with all headers copied (allows to test sticky routing)
// Note this should only be made available to trusted clients.
func FetcherHandler2(w http.ResponseWriter, r *http.Request) {
LogRequest(r, "Fetch proxy2")
query := r.URL.Query()
vals, ok := query["url"]
if !ok {
http.Error(w, "missing url query argument", http.StatusBadRequest)
return
}
url := strings.TrimSpace(vals[0])
if url == "" {
http.Error(w, "missing url value", http.StatusBadRequest)
return
}
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "http://" + url
}
//nolint:contextcheck // bug(?) we transfer the context from the http request https://github.com/kkHAIKE/contextcheck/issues/3
req := MakeSimpleRequest(url, r, fetch2CopiesAllHeader.Get())
if req == nil {
http.Error(w, "parsing url failed, invalid url", http.StatusBadRequest)
return
}
OnBehalfOfRequest(req, r)
resp, err := proxyClient.Do(req)
if err != nil {
msg := fmt.Sprintf("Error for %q: %v", url, err)
log.Errf(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
log.LogVf("Success for %+v", req)
for k, v := range resp.Header {
for _, vv := range v {
w.Header().Add(k, vv)
}
}
w.WriteHeader(resp.StatusCode)
bw, err := fnet.Copy(w, resp.Body)
if err != nil {
log.Warnf("Error copying response for %s: %v", url, err)
}
log.LogVf("fh2 copied %d from %s - code %d", bw, url, resp.StatusCode)
_ = resp.Body.Close()
}
// FetcherHandler is the handler for the fetcher/proxy.
func FetcherHandler(w http.ResponseWriter, r *http.Request) {
LogRequest(r, "Fetch (prefix stripped)")
hj, ok := w.(http.Hijacker)
if !ok {
log.Errf("hijacking not supported: %v", r.Proto)
http.Error(w, "Use fetch2 when using http/2.0", http.StatusHTTPVersionNotSupported)
return
}
conn, _, err := hj.Hijack()
if err != nil {
log.Errf("hijacking error %v", err)
return
}
// Don't forget to close the connection:
defer conn.Close()
// Stripped prefix gets replaced by ./ - sometimes...
url := strings.TrimPrefix(r.URL.String(), "./")
opts := NewHTTPOptions("http://" + url)
opts.HTTPReqTimeOut = 5 * time.Minute
OnBehalfOf(opts, r)
client, _ := NewClient(opts)
if client == nil {
return // error logged already
}
_, data, _ := client.Fetch()
_, err = conn.Write(data)
if err != nil {
log.Errf("Error writing fetched data to %v: %v", r.RemoteAddr, err)
}
client.Close()
}
// -- Redirection to https feature --
// RedirectToHTTPSHandler handler sends a redirect to same URL with https.
func RedirectToHTTPSHandler(w http.ResponseWriter, r *http.Request) {
dest := "https://" + r.Host + r.URL.String()
LogRequest(r, "Redirecting to "+dest)
http.Redirect(w, r, dest, http.StatusSeeOther)
}
// RedirectToHTTPS Sets up a redirector to https on the given port.
// (Do not create a loop, make sure this is addressed from an ingress).
func RedirectToHTTPS(port string) net.Addr {
m, a := HTTPServer("https redirector", port)
if m == nil {
return nil // error already logged
}
m.HandleFunc("/", RedirectToHTTPSHandler)
return a
}
// LogRequest logs the incoming request, including headers when loglevel is verbose.
func LogRequest(r *http.Request, msg string) {
if log.Log(log.Info) {
log.Printf("%s: %v %v %v %v (%s) %s %q", msg, r.Method, r.URL, r.Proto, r.RemoteAddr,
r.Header.Get("X-Forwarded-Proto"), r.Header.Get("X-Forwarded-For"), r.Header.Get("User-Agent"))
}
if log.LogVerbose() {
// Host is removed from headers map and put separately
log.Printf("Header Host: %v", r.Host)
for name, headers := range r.Header {
for _, h := range headers {
log.Printf("Header %v: %v\n", name, h)
}
}
}
}
// LogAndCall wraps an HTTP handler to log the request first.
func LogAndCall(msg string, hf http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
LogRequest(r, msg)
hf(w, r)
})
}
// LogAndCallNoArg is LogAndCall for functions not needing the response/request args.
func LogAndCallNoArg(msg string, f func()) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
LogRequest(r, msg)
f()
})
}