-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
64 lines (59 loc) · 1.77 KB
/
Copy pathutils.go
File metadata and controls
64 lines (59 loc) · 1.77 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
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) {
// A negative limit would slice to a negative bound and panic. The only
// caller passes a constant, so this is unreachable today -- but the
// function is exported to the rest of the package and should be total.
if maxSize < 0 {
maxSize = 0
}
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
}
// isPrintable reports whether bs should be shown as text rather than dumped as
// hex.
//
// Whitespace counts as text. unicode.IsPrint answers false for '\n', '\t' and
// '\r', so testing it alone sent every multi-line body -- pretty-printed JSON,
// HTML, a log excerpt, anything with a line break in the first 256 bytes -- to
// the hex dumper, which is the least readable way to show text a human was
// about to read.
//
// Known gap: bytes that are not valid UTF-8 decode to U+FFFD, which is itself
// printable, so a body of high bytes still reads as text. Cropping happens
// before this check and can split a multi-byte rune, so the obvious utf8.Valid
// guard would misfile legitimate text; tracked separately.
func isPrintable(bs []byte) bool {
nonPrintableIdx := bytes.IndexFunc(bs, func(r rune) bool {
return !unicode.IsPrint(r) && !unicode.IsSpace(r)
})
return nonPrintableIdx < 0
}