-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
300 lines (242 loc) · 8.32 KB
/
Copy pathhttp.go
File metadata and controls
300 lines (242 loc) · 8.32 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package directadmin
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
)
// debugBodyLimit caps the debug body bytes to 32KB.
const debugBodyLimit = 32768
const sessionTimeout = 1 * time.Hour
type httpDebug struct {
Body string
BodyTruncated bool
Code int
Cookies []string
Endpoint string
Method string
Start time.Time
}
func (c *UserContext) getRequestURLNew(endpoint string) string {
return fmt.Sprintf("%s/api/%s", c.api.url, endpoint)
}
func (c *UserContext) getRequestURLOld(endpoint string) string {
return fmt.Sprintf("%s/CMD_%s", c.api.url, endpoint)
}
// makeRequest is the underlying function for HTTP requests. It handles debugging statements, and simple error handling.
// It returns the response status code alongside the body; a non-2xx code is also surfaced as an error.
func (c *UserContext) makeRequest(req *http.Request) (int, []byte, error) {
var debugCookies []string
cookiesToSet := c.cookieJar.Cookies(req.URL)
sessionCookieSet := false
for _, cookie := range cookiesToSet {
req.AddCookie(cookie)
if cookie.Name == "csrftoken" {
req.Header.Set("X-CSRFToken", cookie.Value)
} else if cookie.Name == "session" {
sessionCookieSet = true
}
if c.api.debug {
debugCookies = append(debugCookies, cookie.String())
}
}
debug := httpDebug{
Cookies: debugCookies,
Endpoint: getPathWithQuery(req),
Method: req.Method,
Start: time.Now(),
}
defer c.api.printDebugHTTP(&debug)
if !sessionCookieSet {
req.SetBasicAuth(c.credentials.username, c.credentials.passkey)
}
resp, err := c.api.httpClient.Do(req)
if err != nil {
return 0, nil, err
}
defer resp.Body.Close()
if c.api.debug {
debug.Code = resp.StatusCode
}
// Required for plugin usage in particular (session and csrf token cookies).
for _, cookie := range resp.Cookies() {
if cookie.Name == "session" {
c.sessionExpires = time.Now().Add(sessionTimeout)
}
if cookie.Path == "" {
cookie.Path = "/"
}
c.cookieJar.SetCookies(req.URL, []*http.Cookie{cookie})
}
var responseBytes []byte
if resp.Body != nil {
responseBytes, err = io.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, nil, fmt.Errorf("error reading response body: %w", err)
}
if c.api.debug {
if len(responseBytes) > debugBodyLimit {
debug.BodyTruncated = true
debug.Body = string(responseBytes[:debugBodyLimit])
} else {
debug.Body = string(responseBytes)
}
}
}
if resp.StatusCode/100 != 2 {
return resp.StatusCode, responseBytes, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return resp.StatusCode, responseBytes, nil
}
// makeRequestNew supports DirectAdmin's new API.
func (c *UserContext) makeRequestNew(method string, endpoint string, body any, object any) ([]byte, error) {
var bodyBytes []byte
if body != nil {
var err error
bodyBytes, err = json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("error serializing body: %w", err)
}
}
req, err := c.newRequest(context.Background(), method, c.getRequestURLNew(endpoint), bytes.NewBuffer(bodyBytes))
if err != nil {
return nil, err
}
query := req.URL.Query()
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = query.Encode()
_, resp, err := c.makeRequest(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if resp != nil && object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
}
return resp, nil
}
// makeRequestOld supports DirectAdmin's old API.
func (c *UserContext) makeRequestOld(method string, endpoint string, body url.Values, object any) ([]byte, error) {
req, err := c.newRequest(context.Background(), method, c.getRequestURLOld(endpoint), strings.NewReader(body.Encode()))
if err != nil {
return nil, err
}
query := req.URL.Query()
query.Add("json", "yes")
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")
req.URL.RawQuery = query.Encode()
var genericResponse apiGenericResponse
_, resp, err := c.makeRequest(req)
if err != nil {
jsonErr := json.Unmarshal(resp, &genericResponse)
if jsonErr != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
return nil, errors.New(genericResponse.Error + ": " + genericResponse.Result)
}
if resp != nil {
if object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
} else if err = json.Unmarshal(resp, &genericResponse); err == nil && genericResponse.Error != "" {
return nil, errors.New(genericResponse.Error + ": " + genericResponse.Result)
}
}
return resp, nil
}
// newRequest builds an *http.Request with the headers common to every DirectAdmin
// request (Referer, User-Agent) already set. Callers add any method-specific headers.
func (c *UserContext) newRequest(ctx context.Context, method string, url string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, strings.ToUpper(method), url, body)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Set("Referer", c.api.url)
req.Header.Set("User-Agent", "DirectAdmin-Go-SDK")
return req, nil
}
// uploadFile functions for either the old or new DA API.
func (c *UserContext) uploadFile(method string, endpoint string, data []byte, object any, contentType string) ([]byte, error) {
req, err := c.newRequest(context.Background(), method, c.api.url+endpoint, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
query := req.URL.Query()
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", contentType)
req.URL.RawQuery = query.Encode()
_, resp, err := c.makeRequest(req)
if err != nil {
return nil, fmt.Errorf("error making request: %w", err)
}
if len(resp) > 0 && object != nil {
if err = json.Unmarshal(resp, &object); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %w", err)
}
}
return resp, nil
}
func (a *API) printDebugHTTP(debug *httpDebug) {
if a.debug {
var bodyTruncated string
if debug.BodyTruncated {
bodyTruncated = " (truncated)"
}
fmt.Printf("\nENDPOINT: %v %v\nSTATUS CODE: %v\nTIME STARTED: %v\nTIME ENDED: %v\nTIME TAKEN: %v\nCOOKIES: %s\nRESP BODY%s: %v\n", debug.Method, debug.Endpoint, debug.Code, debug.Start, time.Now(), time.Since(debug.Start), strings.Join(debug.Cookies, ";"), bodyTruncated, debug.Body)
}
}
func getPathWithQuery(req *http.Request) string {
if req == nil {
return ""
}
if req.URL.RawQuery != "" {
return req.URL.Path + "?" + req.URL.RawQuery
}
return req.URL.Path
}
// CustomHTTP is a thin escape hatch for making authenticated requests against
// server paths the SDK doesn't wrap directly, such as third-party plugin
// endpoints (e.g. /CMD_PLUGINS/...). Obtain one via UserContext.HTTP.
type CustomHTTP struct {
ctx *UserContext
}
// HTTP returns a CustomHTTP bound to the user's session.
func (c *UserContext) HTTP() *CustomHTTP {
return &CustomHTTP{ctx: c}
}
// do performs an authenticated request against the given server path (including
// any query string, e.g. "/CMD_PLUGINS/redis/ajax.raw?a=start"). It returns the
// response status code and raw body; a non-2xx code is also surfaced as an error.
func (h *CustomHTTP) do(ctx context.Context, method, path string, body io.Reader) (int, []byte, error) {
req, err := h.ctx.newRequest(ctx, method, h.ctx.api.url+path, body)
if err != nil {
return 0, nil, err
}
return h.ctx.makeRequest(req)
}
// GET performs an authenticated GET.
func (h *CustomHTTP) GET(ctx context.Context, path string) (int, []byte, error) {
return h.do(ctx, http.MethodGet, path, nil)
}
// POST performs an authenticated POST. No Content-Type is set by default.
func (h *CustomHTTP) POST(ctx context.Context, path string, body io.Reader) (int, []byte, error) {
return h.do(ctx, http.MethodPost, path, body)
}
// PUT performs an authenticated PUT.
func (h *CustomHTTP) PUT(ctx context.Context, path string, body io.Reader) (int, []byte, error) {
return h.do(ctx, http.MethodPut, path, body)
}
// DELETE performs an authenticated DELETE.
func (h *CustomHTTP) DELETE(ctx context.Context, path string) (int, []byte, error) {
return h.do(ctx, http.MethodDelete, path, nil)
}