forked from justmao945/mallory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
85 lines (77 loc) · 1.74 KB
/
util.go
File metadata and controls
85 lines (77 loc) · 1.74 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mallory
import (
"fmt"
"net/http"
"os"
"runtime"
"strconv"
"syscall"
"time"
"unsafe"
)
// Duration to e.g. 432ms or 12s, human readable translation
func BeautifyDuration(d time.Duration) string {
u, ms, s := uint64(d), uint64(time.Millisecond), uint64(time.Second)
if d < 0 {
u = -u
}
switch {
case u < ms:
return "0"
case u < s:
return strconv.FormatUint(u/ms, 10) + "ms"
default:
return strconv.FormatUint(u/s, 10) + "s"
}
}
func BeautifySize(s int64) string {
switch {
case s < 1024:
return strconv.FormatInt(s, 10) + "B"
case s < 1024*1024:
return strconv.FormatInt(s/1024, 10) + "KB"
default:
return strconv.FormatInt(s/1024/1024, 10) + "MB"
}
}
// copy and overwrite headers from r to w
func CopyHeader(w http.ResponseWriter, r *http.Response) {
// copy headers
dst, src := w.Header(), r.Header
for k, _ := range dst {
dst.Del(k)
}
for k, vs := range src {
for _, v := range vs {
dst.Add(k, v)
}
}
}
// POSIX ioctl syscall
// From https://github.com/mreiferson/go-simplelog/blob/master/simplelog.go
func Ioctl(fd, request, argp uintptr) syscall.Errno {
_, _, errorp := syscall.Syscall(syscall.SYS_IOCTL, fd, request, argp)
return errorp
}
// Test is a termnial or not
// From https://github.com/mreiferson/go-simplelog/blob/master/simplelog.go
func Isatty(f *os.File) bool {
switch runtime.GOOS {
case "darwin":
case "linux":
default:
return false
}
var t [2]byte
errno := Ioctl(f.Fd(), syscall.TIOCGPGRP, uintptr(unsafe.Pointer(&t)))
return errno == 0
}
// Test a file is exist or not
func IsExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// Return http status text looks like "200 OK"
func StatusText(c int) string {
return fmt.Sprintf("%d %s", c, http.StatusText(c))
}