-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
124 lines (107 loc) · 2.77 KB
/
worker.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
package sceneworker
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
var (
ErrorTimeout = errors.New("Timeout")
ErrorRefused = errors.New("Refused")
)
// defining a interface for worker
type Worker interface {
Run(req interface{}) (interface{}, error)
// define output as a map of strings
}
// Define http worker
type HttpWorker struct {
HttpClient *http.Client
Url string
HttpMethod string
ContentType string
Body map[string]string
Cookies []*http.Cookie
}
type OutputHttpWorker struct {
StatusCode int `json:"StatusCode"`
LenghtBody int64 `json:"LenghtBody"`
ElapsedTime int64 `json:"ElapsedTime"`
Cookies []*http.Cookie
}
func (hw *HttpWorker) Run() (OutputHttpWorker, error) {
// empty data by default
data := []byte{}
output := OutputHttpWorker{
StatusCode: -100,
LenghtBody: -100,
ElapsedTime: -100,
}
// Parse url
urlParsed, err := url.Parse(hw.Url)
if err != nil {
log.Fatal("Worker ERROR ", err)
return output, err
}
// Validate if Method is POST to setup the datatype of the answer
if hw.HttpMethod == "POST" {
if hw.ContentType == "application/json" {
data, _ = json.Marshal(hw.Body)
}
if hw.ContentType == "application/x-www-form-urlencoded" {
htmlForm := url.Values{}
for key, value := range hw.Body {
htmlForm.Set(key, value)
}
data = []byte(htmlForm.Encode())
}
}
request, err := http.NewRequest(hw.HttpMethod, urlParsed.String(), bytes.NewBuffer(data))
request.Header.Set("Content-Type", hw.ContentType)
// Validate if error is Timeout or the site is not up
if err != nil {
log.Fatal("Worker ERROR, creating request:", err)
}
// add cookies if exists
// validate if the Cookies atribute is empty
if len(hw.Cookies) != 0 {
for _, cookie := range hw.Cookies {
request.AddCookie(cookie)
}
}
start := time.Now()
response, err := hw.HttpClient.Do(request)
elapsed := time.Since(start)
// Validate if error is Timeout or the site is not up
if err != nil {
if strings.Contains(err.Error(), "Client.Timeout") {
//log.Println("Error creating request", err)
return output, ErrorTimeout
} else {
log.Fatal("Worker ERROR, bad request ", err)
return output, err
}
}
// store cookies
output.Cookies = response.Cookies()
// convert to miliseconds
contentLengthStr := response.Header.Get("Content-Length")
// validate content length
if contentLengthStr != "" {
contentLength, err := strconv.Atoi(contentLengthStr)
if err != nil {
log.Fatal("Worker ERROR, Error parsing Content-Length:", err)
}
output.LenghtBody = int64(contentLength)
} else {
output.LenghtBody = int64(0)
}
output.ElapsedTime = elapsed.Nanoseconds() / int64(time.Millisecond)
output.StatusCode = response.StatusCode
return output, nil
}