podman
174 строки · 5.1 Кб
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
2// Use of this source code is governed by a MIT style
3// license that can be found in the LICENSE file.
4
5package gin
6
7import (
8"bytes"
9"errors"
10"fmt"
11"io"
12"log"
13"net"
14"net/http"
15"net/http/httputil"
16"os"
17"runtime"
18"strings"
19"time"
20)
21
22var (
23dunno = []byte("???")
24centerDot = []byte("·")
25dot = []byte(".")
26slash = []byte("/")
27)
28
29// RecoveryFunc defines the function passable to CustomRecovery.
30type RecoveryFunc func(c *Context, err any)
31
32// Recovery returns a middleware that recovers from any panics and writes a 500 if there was one.
33func Recovery() HandlerFunc {
34return RecoveryWithWriter(DefaultErrorWriter)
35}
36
37// CustomRecovery returns a middleware that recovers from any panics and calls the provided handle func to handle it.
38func CustomRecovery(handle RecoveryFunc) HandlerFunc {
39return RecoveryWithWriter(DefaultErrorWriter, handle)
40}
41
42// RecoveryWithWriter returns a middleware for a given writer that recovers from any panics and writes a 500 if there was one.
43func RecoveryWithWriter(out io.Writer, recovery ...RecoveryFunc) HandlerFunc {
44if len(recovery) > 0 {
45return CustomRecoveryWithWriter(out, recovery[0])
46}
47return CustomRecoveryWithWriter(out, defaultHandleRecovery)
48}
49
50// CustomRecoveryWithWriter returns a middleware for a given writer that recovers from any panics and calls the provided handle func to handle it.
51func CustomRecoveryWithWriter(out io.Writer, handle RecoveryFunc) HandlerFunc {
52var logger *log.Logger
53if out != nil {
54logger = log.New(out, "\n\n\x1b[31m", log.LstdFlags)
55}
56return func(c *Context) {
57defer func() {
58if err := recover(); err != nil {
59// Check for a broken connection, as it is not really a
60// condition that warrants a panic stack trace.
61var brokenPipe bool
62if ne, ok := err.(*net.OpError); ok {
63var se *os.SyscallError
64if errors.As(ne, &se) {
65seStr := strings.ToLower(se.Error())
66if strings.Contains(seStr, "broken pipe") ||
67strings.Contains(seStr, "connection reset by peer") {
68brokenPipe = true
69}
70}
71}
72if logger != nil {
73stack := stack(3)
74httpRequest, _ := httputil.DumpRequest(c.Request, false)
75headers := strings.Split(string(httpRequest), "\r\n")
76for idx, header := range headers {
77current := strings.Split(header, ":")
78if current[0] == "Authorization" {
79headers[idx] = current[0] + ": *"
80}
81}
82headersToStr := strings.Join(headers, "\r\n")
83if brokenPipe {
84logger.Printf("%s\n%s%s", err, headersToStr, reset)
85} else if IsDebugging() {
86logger.Printf("[Recovery] %s panic recovered:\n%s\n%s\n%s%s",
87timeFormat(time.Now()), headersToStr, err, stack, reset)
88} else {
89logger.Printf("[Recovery] %s panic recovered:\n%s\n%s%s",
90timeFormat(time.Now()), err, stack, reset)
91}
92}
93if brokenPipe {
94// If the connection is dead, we can't write a status to it.
95c.Error(err.(error)) //nolint: errcheck
96c.Abort()
97} else {
98handle(c, err)
99}
100}
101}()
102c.Next()
103}
104}
105
106func defaultHandleRecovery(c *Context, _ any) {
107c.AbortWithStatus(http.StatusInternalServerError)
108}
109
110// stack returns a nicely formatted stack frame, skipping skip frames.
111func stack(skip int) []byte {
112buf := new(bytes.Buffer) // the returned data
113// As we loop, we open files and read them. These variables record the currently
114// loaded file.
115var lines [][]byte
116var lastFile string
117for i := skip; ; i++ { // Skip the expected number of frames
118pc, file, line, ok := runtime.Caller(i)
119if !ok {
120break
121}
122// Print this much at least. If we can't find the source, it won't show.
123fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
124if file != lastFile {
125data, err := os.ReadFile(file)
126if err != nil {
127continue
128}
129lines = bytes.Split(data, []byte{'\n'})
130lastFile = file
131}
132fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
133}
134return buf.Bytes()
135}
136
137// source returns a space-trimmed slice of the n'th line.
138func source(lines [][]byte, n int) []byte {
139n-- // in stack trace, lines are 1-indexed but our array is 0-indexed
140if n < 0 || n >= len(lines) {
141return dunno
142}
143return bytes.TrimSpace(lines[n])
144}
145
146// function returns, if possible, the name of the function containing the PC.
147func function(pc uintptr) []byte {
148fn := runtime.FuncForPC(pc)
149if fn == nil {
150return dunno
151}
152name := []byte(fn.Name())
153// The name includes the path name to the package, which is unnecessary
154// since the file name is already included. Plus, it has center dots.
155// That is, we see
156// runtime/debug.*T·ptrmethod
157// and want
158// *T.ptrmethod
159// Also the package path might contain dot (e.g. code.google.com/...),
160// so first eliminate the path prefix
161if lastSlash := bytes.LastIndex(name, slash); lastSlash >= 0 {
162name = name[lastSlash+1:]
163}
164if period := bytes.Index(name, dot); period >= 0 {
165name = name[period+1:]
166}
167name = bytes.ReplaceAll(name, centerDot, dot)
168return name
169}
170
171// timeFormat returns a customized time string for logger.
172func timeFormat(t time.Time) string {
173return t.Format("2006/01/02 - 15:04:05")
174}
175