-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_http.go
470 lines (383 loc) · 10.6 KB
/
job_http.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
// SPDX-FileCopyrightText: 2021 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package karajo
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
libhttp "git.sr.ht/~shulhan/pakakeh.go/lib/http"
"git.sr.ht/~shulhan/pakakeh.go/lib/mlog"
)
const (
defJobHTTPMethod = http.MethodGet
defJobInterval = 30 * time.Second
defJosParamEpoch = "_karajo_epoch"
defTimeLayout = "2006-01-02 15:04:05 MST"
)
// JobHTTP A JobHTTP is a periodic job that send HTTP request to external HTTP
// server (or to karajo Job itself).
//
// See the [JobBase]'s Interval and Schedule fields for more information on
// how to setup periodic time.
//
// Each JobHTTP execution send the parameter named "_karajo_epoch" with value
// set to current server Unix timestamp.
// If the request type is "query" then the parameter is inside the query URL.
// If the request type is "form" then the parameter is inside the body.
// If the request type is "json" then the parameter is inside the body as JSON
// object, for example '{"_karajo_epoch":1656750073}'.
//
// The job configuration in INI format,
//
// [job "name"]
// secret =
// header_sign =
// http_method =
// http_url =
// http_request_type =
// http_header =
// http_timeout =
// http_insecure =
type JobHTTP struct {
// jobq is a channel passed by Karajo instance to limit number of
// job running at the same time.
jobq chan struct{}
headers http.Header
// httpc define the HTTP client that will execute the http_url.
httpc *libhttp.Client
params map[string]interface{}
stopq chan struct{}
// Secret define a string to sign the request query or body with
// HMAC+SHA-256.
// The signature is sent on HTTP header "X-Karajo-Sign" as hex string.
// This field is optional.
Secret string `ini:"::secret" json:"-"`
// HeaderSign define the HTTP header where the signature will be
// written in request.
// Default to "X-Karajo-Sign" if its empty.
HeaderSign string `ini:"::header_sign" json:"header_sign,omitempty"`
// HTTPMethod HTTP method to be used in request for job execution.
// Its accept only GET, POST, PUT, or DELETE.
// This field is optional, default to GET.
HTTPMethod string `ini:"::http_method" json:"http_method"`
// The HTTP URL where the job will be executed.
// This field is required.
HTTPURL string `ini:"::http_url" json:"http_url"`
baseURI string
requestURI string
// HTTPRequestType The header Content-Type to be set on request.
//
// - (empty string): no header Content-Type set.
// - query: no header Content-Type to be set, reserved for future
// use.
// - form: header Content-Type set to
// "application/x-www-form-urlencoded".
// - json: header Content-Type set to "application/json".
//
// The type "form" and "json" only applicable if the HTTPMethod is
// POST or PUT.
// This field is optional, default to query.
HTTPRequestType string `ini:"::http_request_type" json:"http_request_type"`
requestMethod libhttp.RequestMethod
requestType libhttp.RequestType
// Optional HTTP headers for HTTPURL, in the format of "K: V".
HTTPHeaders []string `ini:"::http_header" json:"http_headers,omitempty"`
JobBase
// HTTPTimeout custom HTTP timeout for this job.
// This field is optional, if not set default to global timeout in
// Env.HTTPTimeout.
// To make job run without timeout, set the value to negative.
HTTPTimeout time.Duration `ini:"::http_timeout" json:"http_timeout"`
// HTTPInsecure can be set to true if the http_url is HTTPS with
// unknown Certificate Authority.
HTTPInsecure bool `ini:"::http_insecure" json:"http_insecure,omitempty"`
}
// Start running the job.
func (job *JobHTTP) Start(jobq chan struct{}, logq chan<- *JobLog) {
job.jobq = jobq
job.JobBase.logq = logq
// Signal to the caller that job has started.
jobq <- struct{}{}
if job.scheduler != nil {
job.startScheduler()
return
}
if job.Interval > 0 {
job.startInterval()
}
}
func (job *JobHTTP) startScheduler() {
for {
select {
case <-job.scheduler.C:
job.run()
case <-job.stopq:
job.scheduler.Stop()
return
}
}
}
func (job *JobHTTP) startInterval() {
var (
now time.Time
nextInterval time.Duration
timer *time.Timer
)
for {
job.Lock()
now = timeNow()
nextInterval = job.computeNextInterval(now)
job.NextRun = now.Add(nextInterval)
job.Unlock()
if timer == nil {
timer = time.NewTimer(nextInterval)
} else {
timer.Reset(nextInterval)
}
select {
case <-timer.C:
case <-job.stopq:
timer.Stop()
return
}
timer.Stop()
job.run()
}
}
func (job *JobHTTP) run() {
var (
jlog *JobLog
err error
)
jlog, err = job.execute()
job.finish(jlog, err)
}
// Stop the job.
func (job *JobHTTP) Stop() {
mlog.Outf(`%s: %s: stopping ...`, job.kind, job.ID)
job.JobBase.Cancel()
select {
case job.stopq <- struct{}{}:
default:
}
mlog.Flush()
}
// init initialize the job, compute the last run and the next run.
func (job *JobHTTP) init(env *Env, name string) (err error) {
var logp = `init`
job.stopq = make(chan struct{}, 1)
job.JobBase.kind = jobKindHTTP
err = job.JobBase.init(env, name)
if err != nil {
return fmt.Errorf(`%s: %w`, logp, err)
}
err = job.initHTTPMethod()
if err != nil {
return err
}
err = job.initHTTPRequestType()
if err != nil {
return err
}
err = job.initHTTPURL(env.ListenAddress)
if err != nil {
return err
}
err = job.initHTTPHeaders()
if err != nil {
return err
}
job.params = make(map[string]interface{})
var httpClientOpts = libhttp.ClientOptions{
ServerURL: job.baseURI,
Headers: job.headers,
AllowInsecure: job.HTTPInsecure,
}
job.httpc = libhttp.NewClient(httpClientOpts)
if job.HTTPTimeout == 0 {
job.HTTPTimeout = env.HTTPTimeout
} else if job.HTTPTimeout < 0 {
// Negative value means 0 on net/http.Client.
job.HTTPTimeout = 0
}
job.httpc.Client.Timeout = job.HTTPTimeout
if len(job.HeaderSign) == 0 {
job.HeaderSign = HeaderNameXKarajoSign
}
return nil
}
// initHTTPMethod check if defined HTTP method is valid.
// If its empty, set default to GET, otherwise return an error.
func (job *JobHTTP) initHTTPMethod() (err error) {
job.HTTPMethod = strings.TrimSpace(job.HTTPMethod)
if len(job.HTTPMethod) == 0 {
job.HTTPMethod = defJobHTTPMethod
job.requestMethod = libhttp.RequestMethodGet
return nil
}
var vstr = strings.ToUpper(job.HTTPMethod)
switch vstr {
case http.MethodGet:
job.requestMethod = libhttp.RequestMethodGet
case http.MethodDelete:
job.requestMethod = libhttp.RequestMethodDelete
case http.MethodPost:
job.requestMethod = libhttp.RequestMethodPost
case http.MethodPut:
job.requestMethod = libhttp.RequestMethodPut
default:
return fmt.Errorf(`invalid HTTP method %q`, vstr)
}
return nil
}
func (job *JobHTTP) initHTTPRequestType() (err error) {
var vstr = strings.ToLower(job.HTTPRequestType)
switch vstr {
case ``, `query`:
job.requestType = libhttp.RequestTypeQuery
case `form`:
job.requestType = libhttp.RequestTypeForm
case `json`:
job.requestType = libhttp.RequestTypeJSON
default:
return fmt.Errorf(`invalid HTTP request type %q`, vstr)
}
return nil
}
func (job *JobHTTP) initHTTPURL(serverAddress string) (err error) {
if job.HTTPURL[0] == '/' {
job.baseURI = fmt.Sprintf(`http://%s`, serverAddress)
job.requestURI = job.HTTPURL
return nil
}
var (
httpURL *url.URL
port string
)
httpURL, err = url.Parse(job.HTTPURL)
if err != nil {
return fmt.Errorf(`%s: invalid http_url %q: %w`, job.ID, job.HTTPURL, err)
}
port = httpURL.Port()
if len(port) == 0 {
if httpURL.Scheme == `https` {
port = `443`
} else {
port = `80`
}
}
job.baseURI = fmt.Sprintf(`%s://%s:%s`, httpURL.Scheme, httpURL.Hostname(), port)
job.requestURI = httpURL.RequestURI()
return nil
}
func (job *JobHTTP) initHTTPHeaders() (err error) {
if len(job.HTTPHeaders) > 0 {
job.headers = make(http.Header, len(job.HTTPHeaders))
}
var (
h string
kv []string
)
for _, h = range job.HTTPHeaders {
kv = strings.SplitN(h, `:`, 2)
if len(kv) != 2 {
return fmt.Errorf(`%s: invalid header %q`, job.ID, h)
}
job.headers.Set(strings.TrimSpace(kv[0]), strings.TrimSpace(kv[1]))
}
return nil
}
func (job *JobHTTP) execute() (jlog *JobLog, err error) {
var ctx context.Context
ctx, jlog = job.JobBase.newLog()
if jlog.Status == JobStatusPaused {
return jlog, nil
}
defer job.JobBase.ctxCancel()
var (
logp = `execute`
now = timeNow()
headers = http.Header{}
params interface{}
rawb []byte
)
_, _ = jlog.Write([]byte("=== BEGIN\n"))
job.params[defJosParamEpoch] = now.Unix()
switch job.requestType {
case libhttp.RequestTypeQuery, libhttp.RequestTypeForm:
params, rawb = job.paramsToURLValues()
case libhttp.RequestTypeJSON:
params, rawb, err = job.paramsToJSON()
if err != nil {
return jlog, fmt.Errorf(`%s: %w`, logp, err)
}
}
if len(job.Secret) != 0 {
var sign = Sign(rawb, []byte(job.Secret))
headers.Set(job.HeaderSign, sign)
}
var (
clientReq = libhttp.ClientRequest{
Method: job.requestMethod,
Path: job.requestURI,
Type: job.requestType,
Header: headers,
Params: params,
}
httpReq *http.Request
)
httpReq, err = job.httpc.GenerateHTTPRequest(clientReq)
if err != nil {
return jlog, fmt.Errorf(`%s: %w`, logp, err)
}
httpReq = httpReq.WithContext(ctx)
rawb, err = httputil.DumpRequestOut(httpReq, true)
if err != nil {
return jlog, fmt.Errorf(`%s: %w`, logp, err)
}
fmt.Fprintf(jlog, "--- HTTP request:\n%s\n\n", rawb)
var clientResp *libhttp.ClientResponse
clientResp, err = job.httpc.Do(httpReq)
if err != nil {
var errCtx = ctx.Err()
if errCtx != nil && errors.Is(errCtx, context.Canceled) {
return jlog, fmt.Errorf(`%s: %w`, logp, &errJobCanceled)
}
return jlog, fmt.Errorf(`%s: %w`, logp, err)
}
rawb, err = httputil.DumpResponse(clientResp.HTTPResponse, true)
if err != nil {
return jlog, fmt.Errorf(`%s: %w`, logp, err)
}
fmt.Fprintf(jlog, "--- HTTP response:\n%s\n\n", rawb)
if clientResp.HTTPResponse.StatusCode != http.StatusOK {
return jlog, fmt.Errorf(`%s: %s`, logp, clientResp.HTTPResponse.Status)
}
_, _ = jlog.Write([]byte("=== DONE\n"))
return jlog, nil
}
func (job *JobHTTP) paramsToJSON() (obj map[string]interface{}, raw []byte, err error) {
raw, err = json.Marshal(job.params)
if err != nil {
return nil, nil, err
}
return job.params, raw, nil
}
// paramsToURLValues convert the job parameters to url.Values.
func (job *JobHTTP) paramsToURLValues() (url.Values, []byte) {
var (
urlValues = url.Values{}
k string
v interface{}
)
for k, v = range job.params {
urlValues.Set(k, fmt.Sprintf(`%s`, v))
}
return urlValues, []byte(urlValues.Encode())
}