-
Notifications
You must be signed in to change notification settings - Fork 26
/
transport.go
152 lines (128 loc) · 3.18 KB
/
transport.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
// Copyright (c) 2014 Canonical Ltd.
// Licensed under the GPLv3, see the COPYING file for details.
package textsecure
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
log "github.com/Sirupsen/logrus"
)
var transport transporter
func setupTransporter() {
setupCA()
transport = newHTTPTransporter(config.Server, config.Tel, registrationInfo.password)
}
type response struct {
Status int
Body io.ReadCloser
}
func (r *response) isError() bool {
return r.Status < 200 || r.Status >= 300
}
func (r *response) Error() string {
return fmt.Sprintf("status code %d\n", r.Status)
}
type transporter interface {
get(url string) (*response, error)
del(url string) (*response, error)
putJSON(url string, body []byte) (*response, error)
putBinary(url string, body []byte) (*response, error)
}
type httpTransporter struct {
baseURL string
user string
pass string
client *http.Client
}
func getProxy(req *http.Request) (*url.URL, error) {
if config.ProxyServer != "" {
u, err := url.Parse(config.ProxyServer)
if err == nil {
return u, nil
}
}
return http.ProxyFromEnvironment(req)
}
func newHTTPTransporter(baseURL, user, pass string) *httpTransporter {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: rootCA},
Proxy: getProxy,
},
}
return &httpTransporter{baseURL, user, pass, client}
}
func (ht *httpTransporter) get(url string) (*response, error) {
req, err := http.NewRequest("GET", ht.baseURL+url, nil)
if err != nil {
return nil, err
}
if config.UserAgent != "" {
req.Header.Set("User-Agent", config.UserAgent)
}
req.SetBasicAuth(ht.user, ht.pass)
resp, err := ht.client.Do(req)
if err != nil {
return nil, err
}
r := &response{}
if resp != nil {
r.Status = resp.StatusCode
r.Body = resp.Body
}
log.Debugf("GET %s %d\n", url, r.Status)
return r, err
}
func (ht *httpTransporter) del(url string) (*response, error) {
req, err := http.NewRequest("DELETE", ht.baseURL+url, nil)
if err != nil {
return nil, err
}
if config.UserAgent != "" {
req.Header.Set("User-Agent", config.UserAgent)
}
req.SetBasicAuth(ht.user, ht.pass)
resp, err := ht.client.Do(req)
if err != nil {
return nil, err
}
r := &response{}
if resp != nil {
r.Status = resp.StatusCode
r.Body = resp.Body
}
log.Debugf("DELETE %s %d\n", url, r.Status)
return r, err
}
func (ht *httpTransporter) put(url string, body []byte, ct string) (*response, error) {
br := bytes.NewReader(body)
req, err := http.NewRequest("PUT", ht.baseURL+url, br)
if err != nil {
return nil, err
}
if config.UserAgent != "" {
req.Header.Set("User-Agent", config.UserAgent)
}
req.Header.Add("Content-type", ct)
req.SetBasicAuth(ht.user, ht.pass)
resp, err := ht.client.Do(req)
if err != nil {
return nil, err
}
r := &response{}
if resp != nil {
r.Status = resp.StatusCode
r.Body = resp.Body
}
log.Debugf("PUT %s %d\n", url, r.Status)
return r, err
}
func (ht *httpTransporter) putJSON(url string, body []byte) (*response, error) {
return ht.put(url, body, "application/json")
}
func (ht *httpTransporter) putBinary(url string, body []byte) (*response, error) {
return ht.put(url, body, "application/octet-stream")
}