-
Notifications
You must be signed in to change notification settings - Fork 6
/
http.go
99 lines (88 loc) · 2.35 KB
/
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
package client
import (
"bytes"
"errors"
"fmt"
"net"
"net/http"
"strings"
"time"
log "github.com/Sirupsen/logrus"
xplatform "github.com/dvonthenen/goxplatform"
)
const (
defaultReadTimeout = 10 * time.Second
defaultWriteTimeout = 10 * time.Second
)
//Client representation of an HTTP client
type Client struct {
streamID string
url string
masterAddr string
masterPath string
client *http.Client
}
//New generates a new HTTP client
func New(addr string, path string) *Client {
return &Client{
url: "http://" + addr + path,
masterAddr: addr,
masterPath: path,
client: &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 10 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
},
},
}
}
func parsePartialURI(str string) (string, string) {
str = xplatform.GetInstance().Str.Trim(str, " /")
index := strings.Index(str, "/")
master := str[:index]
path := str[index:]
return master, path
}
//Send will send a HTTP payload to an HTTP server
func (c *Client) Send(payload []byte) (*http.Response, error) {
httpReq, err := http.NewRequest("POST", c.url, bytes.NewReader(payload))
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "application/x-protobuf")
httpReq.Header.Set("Accept", "application/json") //TODO switch to protobuf at some point
httpReq.Header.Set("User-Agent", "scaleio/0.1")
if c.streamID != "" {
httpReq.Header.Set("Mesos-Stream-Id", c.streamID)
}
httpResp, err := c.client.Do(httpReq)
if err != nil {
log.Errorln("HTTP ERROR:", err)
return nil, err
}
if httpResp.StatusCode == http.StatusTemporaryRedirect ||
httpResp.StatusCode == http.StatusPermanentRedirect {
log.Warnln("Old Master:", c.masterAddr)
master, path := parsePartialURI(httpResp.Header.Get("Location"))
c.masterAddr = master
c.masterPath = path
log.Warnln("New Master:", c.masterAddr)
c.url = "http://" + c.masterAddr + c.masterPath
log.Warnln("New URL:", c.url)
msg := fmt.Sprint("StatusRedirect - New master is: ", c.masterAddr)
log.Errorln(msg)
return nil, errors.New(msg)
}
streamID := httpResp.Header.Get("Mesos-Stream-Id")
if streamID != "" {
if c.streamID == "" {
log.Infoln("[MESOS-STREAM-ID] Setting to", streamID)
} else {
log.Infoln("[MESOS-STREAM-ID]", c.streamID, "->", streamID)
}
c.streamID = streamID
}
return httpResp, nil
}