forked from winterssy/ghttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
141 lines (126 loc) · 2.7 KB
/
util.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package ghttp
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"reflect"
"strconv"
"unsafe"
"github.com/winterssy/gjson"
)
type (
// H is an alias of gjson.Object.
// Visit https://github.com/winterssy/gjson for more details.
H = gjson.Object
)
func b2s(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func toString(v interface{}) string {
switch v := v.(type) {
case string:
return v
case []byte:
return b2s(v)
case bool:
return strconv.FormatBool(v)
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32)
case float64:
return strconv.FormatFloat(v, 'f', -1, 64)
case int:
return strconv.FormatInt(int64(v), 10)
case int8:
return strconv.FormatInt(int64(v), 10)
case int16:
return strconv.FormatInt(int64(v), 10)
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case uint:
return strconv.FormatUint(uint64(v), 10)
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint16:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
case error:
return v.Error()
case interface {
String() string
}:
return v.String()
}
panic(fmt.Errorf("ghttp: can't cast %#v of type %[1]T to string", v))
}
func toStrings(v interface{}) (vs []string) {
defer func() {
if err := recover(); err != nil {
log.Print(err)
vs = nil // ignore this field
}
}()
switch v := v.(type) {
case []string:
vs = make([]string, len(v))
copy(vs, v)
case string, []byte,
bool,
float32, float64,
int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
error,
interface {
String() string
}:
vs = []string{toString(v)}
default:
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Slice, reflect.Array:
n := rv.Len()
vs = make([]string, 0, n)
for i := 0; i < n; i++ {
vs = append(vs, toString(rv.Index(i).Interface()))
}
}
}
return
}
func toReadCloser(r io.Reader) io.ReadCloser {
rc, ok := r.(io.ReadCloser)
if !ok && r != nil {
rc = ioutil.NopCloser(r)
}
return rc
}
// Report whether an HTTP request or response body is empty.
func bodyEmpty(body io.ReadCloser) bool {
return body == nil || body == http.NoBody
}
func drainBody(body io.ReadCloser, w io.Writer) (err error) {
defer body.Close()
_, err = io.Copy(w, body)
return
}
func findCookie(name string, cookies []*http.Cookie) (*http.Cookie, error) {
for _, cookie := range cookies {
if cookie.Name == name {
return cookie, nil
}
}
return nil, http.ErrNoCookie
}
// Return value if nonempty, def otherwise.
func valueOrDefault(value, def string) string {
if value != "" {
return value
}
return def
}