-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
44 lines (40 loc) · 814 Bytes
/
utils.go
File metadata and controls
44 lines (40 loc) · 814 Bytes
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
package main
import (
"bytes"
"encoding/hex"
"io"
"net/http"
"strings"
"unicode"
)
func parseHeaderLine(l string) (name, value string) {
if idx := strings.Index(l, ":"); idx < 0 {
name = l
} else {
name = l[:idx]
value = l[idx+1:]
}
name = http.CanonicalHeaderKey(strings.TrimSpace(name))
value = strings.TrimSpace(value)
return
}
func printPayload(w io.Writer, bs []byte, maxSize int) (croppedBytes int) {
if n := len(bs); n > maxSize {
bs = bs[:maxSize]
croppedBytes = n - maxSize
}
if isPrintable(bs) {
_, _ = w.Write(bs)
} else {
d := hex.Dumper(w)
defer func() { _ = d.Close() }()
_, _ = d.Write(bs)
}
return
}
func isPrintable(bs []byte) bool {
nonPrintableIdx := bytes.IndexFunc(bs, func(r rune) bool {
return !unicode.IsPrint(r)
})
return nonPrintableIdx < 0
}