-
Notifications
You must be signed in to change notification settings - Fork 0
/
httppub.go
199 lines (194 loc) · 4.47 KB
/
httppub.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"encoding/json"
"fmt"
"github.com/godaner/brokerc/broker"
"github.com/godaner/brokerc/broker/httpv1"
"github.com/godaner/brokerc/spinner"
"github.com/urfave/cli"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const (
B = 1
KB = 1024 * B
MB = 1024 * KB
GB = 1024 * MB
TB = 1024 * GB
)
var HTTPPublishCommand = cli.Command{
Name: "httppub",
Usage: "publish http message",
UsageText: "Usage: brokerc httppub [options...] <uri>, uri arg format: http[s]://[username][:password]@host.domain[:port][suburi]",
Flags: []cli.Flag{
cli.StringFlag{
Name: "X",
Usage: "method.",
Required: true,
Value: "GET",
},
cli.StringFlag{
Name: "H",
Usage: "header.",
Required: true,
},
cli.StringFlag{
Name: "m",
Usage: "message.",
Required: true,
},
cli.StringFlag{
Name: "o",
Usage: "write to file instead of stdout.",
Required: false,
},
cli.BoolFlag{
Name: "d",
Usage: "debug.",
Required: false,
},
cli.StringFlag{
Name: "cafile",
Usage: "path to a file containing trusted CA certificates to enable encrypted communication.",
Required: false,
},
cli.StringFlag{
Name: "cert",
Usage: "client certificate for authentication, if required by server.",
Required: false,
},
cli.StringFlag{
Name: "key",
Usage: "client private key for authentication, if required by server.",
Required: false,
},
cli.BoolFlag{
Name: "insecure",
Usage: "do not check that the server certificate hostname matches the remote hostname.",
Required: false,
},
},
Action: func(context *cli.Context) error {
st := time.Now()
uri := context.Args().Get(0)
X, H, d, m, o, cafile, cert, key, insecure :=
context.String("X"),
context.String("H"),
context.Bool("d"),
context.String("m"),
context.String("o"),
context.String("cafile"),
context.String("cert"),
context.String("key"),
context.Bool("insecure")
logger.SetDebug(d)
b := httpv1.HTTPBrokerV1{
PublishCallBack: func(topic string, resp *http.Response) error {
hs, _ := json.Marshal(resp.Header)
logger.Debugf("PUBLISH RECV=> uri:%v, H:%v !", uri, string(hs))
defer resp.Body.Close()
if o != "" {
// after spinner stop
defer func() {
logger.Infof("PUBLISH RECV=> Download file: %v success !", o)
}()
lens := resp.Header.Get("Content-Length")
len, _ := strconv.ParseUint(lens, 10, 64)
// Spinner
stopUpdateSpinner := make(chan struct{})
defer close(stopUpdateSpinner)
s := spinner.DownloadSpinner{}
s.Start()
defer s.Stop()
download, v, fileName := uint64(0), uint64(0), o
s.UpdateStatus(&spinner.Status{
FileName: &fileName,
Total: &len,
})
go func() {
for ; ; {
select {
case <-time.After(time.Second):
spentTime := time.Now().Sub(st)
s.UpdateStatus(&spinner.Status{
Download: &download,
V: &v,
SpentTime: &spentTime,
})
v = 0
case <-stopUpdateSpinner:
return
}
}
}()
// download
file, err := os.Create(o)
if err != nil {
return err
}
defer file.Close()
buf := make([]byte, KB)
for {
n, err := resp.Body.Read(buf)
if n > 0 {
download += uint64(n)
v += uint64(n)
_, err := file.Write(buf[:n])
if err != nil {
return err
}
}
if err != nil {
break
}
}
} else {
bs, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
logger.Infof("PUBLISH RECV=> uri:%v, X:%v, resp:%v !", uri, X, string(bs))
}
return nil
},
CACertFile: cafile,
CertFile: cert,
KeyFile: key,
Insecure: insecure,
Logger: logger,
}
err := b.Connect()
if err != nil {
return err
}
defer b.Disconnect()
logger.Infof("PUBLISH=> uri:%v, X:%v, m:%v !", uri, X, m)
logger.Debugf("PUBLISH=> uri:%v, H:%v !", uri, H)
err = b.Publish(fmt.Sprintf("%v#%v", X, uri), &broker.Message{
Header: parseH(H),
Body: []byte(m),
})
if err != nil {
return err
}
return nil
},
}
func parseH(H string) (hr map[string][]string) {
hr = map[string][]string{}
kvStrs := strings.Split(H, ";")
for _, kvStr := range kvStrs {
kvs := strings.Split(kvStr, ":")
if len(kvs) != 2 {
continue
}
k, v := kvs[0], kvs[1]
vs := hr[k]
hr[k] = append(vs, v)
}
return hr
}