-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathrequest.go
158 lines (148 loc) · 3.97 KB
/
request.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
package transmissionrpc
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sync"
)
const csrfHeader = "X-Transmission-Session-Id"
type requestPayload struct {
Method string `json:"method"`
Arguments interface{} `json:"arguments,omitempty"`
Tag int `json:"tag,omitempty"`
}
type answerPayload struct {
Arguments interface{} `json:"arguments"`
Result string `json:"result"`
Tag *int `json:"tag"`
}
func (c *Client) rpcCall(ctx context.Context, method string, arguments interface{}, result interface{}) (err error) {
return c.request(ctx, method, arguments, result, true)
}
func (c *Client) request(ctx context.Context, method string, arguments interface{}, result interface{}, retry bool) (err error) {
// Let's avoid crashing
if c.httpC == nil {
err = errors.New("this controller is not initialized, please use the New() function")
return
}
// Prepare the pipeline between payload generation and request
pOut, pIn := io.Pipe()
// Prepare the request
var req *http.Request
if req, err = http.NewRequestWithContext(ctx, "POST", c.url, pOut); err != nil {
err = fmt.Errorf("can't prepare request for '%s' method: %w", method, err)
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.userAgent)
req.Header.Set(csrfHeader, c.getSessionID())
req.SetBasicAuth(c.user, c.password)
// Prepare the marshalling goroutine
var tag int
var encErr error
var mg sync.WaitGroup
mg.Add(1)
go func() {
tag = c.rnd.Int()
encErr = json.NewEncoder(pIn).Encode(&requestPayload{
Method: method,
Arguments: arguments,
Tag: tag,
})
pIn.Close()
mg.Done()
}()
// Execute request
var resp *http.Response
if resp, err = c.httpC.Do(req); err != nil {
mg.Wait()
if encErr != nil {
err = fmt.Errorf("request error: %w | json payload marshall error: %v", err, encErr)
} else {
err = fmt.Errorf("request error: %w", err)
}
return
}
defer resp.Body.Close()
// Let's test the enc result, just in case
mg.Wait()
if encErr != nil {
err = fmt.Errorf("request payload JSON marshalling failed: %w", encErr)
return
}
// Is the CRSF token invalid ?
if resp.StatusCode == http.StatusConflict {
// Recover new token and save it
c.updateSessionID(resp.Header.Get(csrfHeader))
// Retry request if first try
if retry {
return c.request(ctx, method, arguments, result, false)
}
err = errors.New("CSRF token invalid 2 times in a row: stopping to avoid infinite loop")
return
}
// Is request successful ?
if resp.StatusCode != 200 {
err = HTTPStatusCode(resp.StatusCode)
return
}
// Debug
if c.debug {
var data []byte
if data, err = ioutil.ReadAll(resp.Body); err == nil {
fmt.Fprintln(os.Stderr, string(data))
} else {
fmt.Fprintln(os.Stderr, err.Error())
}
resp.Body.Close()
resp.Body = ioutil.NopCloser(bytes.NewBuffer(data))
}
// Decode body
answer := answerPayload{
Arguments: result,
}
if err = json.NewDecoder(resp.Body).Decode(&answer); err != nil {
err = fmt.Errorf("can't unmarshall request answer body: %w", err)
return
}
// fmt.Println("DEBUG >", answer.Result)
// Final checks
if answer.Tag == nil {
err = errors.New("http answer does not have a tag within it's payload")
return
}
if *answer.Tag != tag {
err = errors.New("http request tag and answer payload tag do not match")
return
}
if answer.Result != "success" {
err = fmt.Errorf("http request ok but payload does not indicate success: %s", answer.Result)
return
}
// All good
return
}
func (c *Client) getSessionID() string {
defer c.sessionIDAccess.RUnlock()
c.sessionIDAccess.RLock()
return c.sessionID
}
func (c *Client) updateSessionID(newID string) {
defer c.sessionIDAccess.Unlock()
c.sessionIDAccess.Lock()
c.sessionID = newID
}
type HTTPStatusCode int
func (hsc HTTPStatusCode) Error() string {
text := http.StatusText(int(hsc))
if text != "" {
text = ": " + text
}
return fmt.Sprintf("HTTP error %d%s", hsc, text)
}