1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

fix(server): create new request for every post trial

Updates #1169
This commit is contained in:
wdvxdr 2021-11-17 21:45:51 +08:00
parent c478870870
commit 7b02f8b670
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6

View File

@ -236,17 +236,26 @@ func (c *HTTPClient) onBotPushEvent(e *coolq.Event) {
} }
client := http.Client{Timeout: time.Second * time.Duration(c.timeout)} client := http.Client{Timeout: time.Second * time.Duration(c.timeout)}
req, _ := http.NewRequest("POST", c.addr, bytes.NewReader(e.JSONBytes()))
req.Header.Set("X-Self-ID", strconv.FormatInt(c.bot.Client.Uin, 10)) // see https://stackoverflow.com/questions/31337891/net-http-http-contentlength-222-with-body-length-0
req.Header.Set("User-Agent", "CQHttp/4.15.0") // we should create a new request for every single post trial
req.Header.Set("Content-Type", "application/json") createRequest := func() (*http.Request, error) {
if c.secret != "" { req, err := http.NewRequest("POST", c.addr, bytes.NewReader(e.JSONBytes()))
mac := hmac.New(sha1.New, []byte(c.secret)) if err != nil {
_, _ = mac.Write(e.JSONBytes()) return nil, err
req.Header.Set("X-Signature", "sha1="+hex.EncodeToString(mac.Sum(nil))) }
} req.Header.Set("X-Self-ID", strconv.FormatInt(c.bot.Client.Uin, 10))
if c.apiPort != 0 { req.Header.Set("User-Agent", "CQHttp/4.15.0")
req.Header.Set("X-API-Port", strconv.FormatInt(int64(c.apiPort), 10)) req.Header.Set("Content-Type", "application/json")
if c.secret != "" {
mac := hmac.New(sha1.New, []byte(c.secret))
_, _ = mac.Write(e.JSONBytes())
req.Header.Set("X-Signature", "sha1="+hex.EncodeToString(mac.Sum(nil)))
}
if c.apiPort != 0 {
req.Header.Set("X-API-Port", strconv.FormatInt(int64(c.apiPort), 10))
}
return req, nil
} }
var res *http.Response var res *http.Response
@ -254,6 +263,12 @@ func (c *HTTPClient) onBotPushEvent(e *coolq.Event) {
const maxAttemptTimes = 5 const maxAttemptTimes = 5
for i := 0; i <= maxAttemptTimes; i++ { for i := 0; i <= maxAttemptTimes; i++ {
req, err := createRequest()
if err != nil {
log.Warnf("上报 Event 数据到 %v 时创建请求失败: %v", c.addr, err)
return
}
res, err = client.Do(req) res, err = client.Do(req)
if err == nil { if err == nil {
//goland:noinspection GoDeferInLoop //goland:noinspection GoDeferInLoop