-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutors.go
57 lines (46 loc) · 939 Bytes
/
executors.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
package atmin
import (
"bufio"
"bytes"
"crypto/tls"
"log"
"net"
"net/http"
)
type HTTPExecutor struct {
Addr string
TLS bool
}
func (m Minimizer) ExecuteHTTP(addr string, useTLS bool) Minimizer {
m.ex = &HTTPExecutor{Addr: addr, TLS: useTLS}
m.out = m.ex.Execute(m.in)
return m
}
func (ex *HTTPExecutor) Execute(in []byte) []byte {
var conn net.Conn
var err error
r, err := http.ReadRequest(bufio.NewReader(bytes.NewBuffer(in)))
if err != nil {
return []byte(err.Error())
}
if ex.TLS {
conn, err = tls.Dial("tcp", ex.Addr, &tls.Config{InsecureSkipVerify: true})
if err != nil {
log.Fatal(err)
}
} else {
conn, err = net.Dial("tcp", ex.Addr)
if err != nil {
log.Fatal(err)
}
}
defer conn.Close()
conn.Write(in)
resp, err := http.ReadResponse(bufio.NewReader(conn), r)
if err != nil {
return []byte(err.Error())
}
var buf bytes.Buffer
buf.ReadFrom(resp.Body)
return buf.Bytes()
}