-
Notifications
You must be signed in to change notification settings - Fork 3
/
helpers.go
43 lines (40 loc) · 890 Bytes
/
helpers.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
package uhttp
import (
"bytes"
"io"
"net/http"
"runtime"
"strings"
)
func ExtractAndRestoreRequestBody(r *http.Request) []byte {
if r.Body != nil {
bodyBytes, err := io.ReadAll(r.Body)
if err != nil {
return []byte{}
}
defer r.Body.Close()
if r.Body != nil {
r.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
}
return bodyBytes
}
return []byte{}
}
// figures out the first caller of the function outside of github.com/dunv/http AND net/http
// straight out of net/http/server.go
func relevantCaller() runtime.Frame {
pc := make([]uintptr, 16)
n := runtime.Callers(1, pc)
frames := runtime.CallersFrames(pc[:n])
var frame runtime.Frame
for {
frame, more := frames.Next()
if !strings.HasPrefix(frame.Function, "github.com/dunv/uhttp") && !strings.HasPrefix(frame.Function, "net/http") {
return frame
}
if !more {
break
}
}
return frame
}