/
http.go
90 lines (71 loc) · 1.51 KB
/
http.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"strconv"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
)
type HTTPValue interface {
String() string
}
type HTTPArgs map[string]HTTPValue
type S struct {
Val string
}
func HexArg(b []byte) S {
return S{Val: hex.EncodeToString(b)}
}
func B64Arg(b []byte) S {
return S{Val: base64.StdEncoding.EncodeToString(b)}
}
type I struct {
Val int
}
type U struct {
Val uint64
}
type UHex struct {
Val uint64
}
type B struct {
Val bool
}
type HTTPTime struct {
Val keybase1.Time
}
func (a *HTTPArgs) Add(s string, v HTTPValue) {
(*a)[s] = v
}
func NewHTTPArgs() HTTPArgs {
return make(HTTPArgs)
}
func (s S) String() string { return s.Val }
func (i I) String() string { return strconv.Itoa(i.Val) }
func (u U) String() string { return strconv.FormatUint(u.Val, 10) }
func (h UHex) String() string { return fmt.Sprintf("%016x", h.Val) }
func (b B) String() string {
if b.Val {
return "1"
}
return "0"
}
func (t HTTPTime) String() string { return strconv.FormatInt(int64(t.Val), 10) }
func (a HTTPArgs) ToValues() url.Values {
ret := url.Values{}
for k, v := range a {
ret.Set(k, v.String())
}
return ret
}
func (a HTTPArgs) EncodeToString() string {
return a.ToValues().Encode()
}
func HTTPArgsFromKeyValuePair(key string, val HTTPValue) HTTPArgs {
ret := HTTPArgs{}
ret[key] = val
return ret
}