-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
147 lines (124 loc) · 3.36 KB
/
client.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
package ali_log
import (
"bytes"
"crypto/md5"
"errors"
"fmt"
"github.com/mreiferson/go-httpclient"
"net/http"
"strings"
"sync"
"time"
)
const (
DefaultTimeout int64 = 35
)
const (
version = "0.6.0"
signaturemethod = "hmac-sha1"
)
type LogClient interface {
Send(method string, headers map[string]string, logdata interface{}, resource string) (resp *http.Response, err error)
}
type aliLogClient struct {
Timeout int64
url string
accessKey string
credential Credential
client *http.Client
clientLocker sync.Mutex
}
func NewAliLogClient(url, accessKey, accessKeySecret string) LogClient {
if 0 == len(url) {
panic("ali-log: log url is empty")
}
credential := NewAliLogCredential(accessKeySecret)
cli := new(aliLogClient)
cli.credential = credential
cli.accessKey = accessKey
cli.url = url
if 5 != len(strings.Split(url, ".")) {
panic("ali-log: log url is invalid")
}
cli.initClient()
return cli
}
func (a *aliLogClient) initClient() {
a.clientLocker.Lock()
defer a.clientLocker.Unlock()
timeoutInt := DefaultTimeout
if a.Timeout > 0 {
timeoutInt = a.Timeout
}
timeout := time.Second * time.Duration(timeoutInt)
transport := &httpclient.Transport{
ConnectTimeout: time.Second * 3,
RequestTimeout: timeout,
ResponseHeaderTimeout: timeout + time.Second,
}
a.client = &http.Client{Transport: transport}
}
func (a *aliLogClient) authorization(method string, headers map[string]string, resource string) (authHeader string, err error) {
if signature, e := a.credential.Signature(method, headers, resource); e != nil {
return "", e
} else {
authHeader = fmt.Sprintf("LOG %s:%s", a.accessKey, signature)
}
return
}
func (a *aliLogClient) Send(method string, headers map[string]string, logdata interface{}, resource string) (resp *http.Response, err error) {
var logContent []byte
if nil == logdata {
logContent = []byte{}
} else {
switch m := logdata.(type) {
case []byte:
{
logContent = m
}
default:
err = errors.New("[ali_log][Send] logdata type Invalid")
return
}
}
logMD5 := md5.Sum(logContent)
strMd5 := strings.ToUpper(fmt.Sprintf("%x", logMD5))
if 0 == len(method) {
method = "POST"
}
if nil == headers {
headers = make(map[string]string)
}
headers[LOG_VERSION] = version
headers[CONTENT_TYPE] = "application/x-protobuf"
headers[CONTENT_MD5] = strMd5
headers[LOG_SIGNATUREMETHOD] = signaturemethod
headers[CONTENT_LENTH] = fmt.Sprintf("%v", len(logContent))
headers[LOG_BODYRAWSIZE] = "0"
headers[HOST] = a.url
headers[DATE] = time.Now().UTC().Format(http.TimeFormat)
if authHeader, e := a.authorization(method, headers, fmt.Sprintf("/%s", resource)); e != nil {
err = errors.New("[ali_log][Send][authorization] " + e.Error())
return
} else {
headers[AUTHORIZATION] = authHeader
}
url := a.url + "/" + resource
if !strings.HasPrefix(a.url, "http://") || strings.HasPrefix(a.url, "https://") {
url = "http://" + a.url + "/" + resource
}
postBodyReader := bytes.NewBuffer(logContent)
var req *http.Request
if req, err = http.NewRequest(method, url, postBodyReader); err != nil {
err = errors.New("[ali_log][Send][NewRequest] " + err.Error())
return
}
for header, value := range headers {
req.Header.Add(header, value)
}
if resp, err = a.client.Do(req); err != nil {
err = errors.New("[ali_log][Send][Do] " + err.Error())
return
}
return
}