transport.go 6.72 KB
Newer Older
1
package irma
2
3
4

import (
	"bytes"
5
	"context"
6
	"crypto/sha256"
7
	"encoding/base64"
8
9
10
	"encoding/json"
	"io"
	"io/ioutil"
11
	"log"
12
	"net"
13
	"net/http"
14
	"path/filepath"
15
16
	"strings"
	"time"
17

18
	"github.com/go-errors/errors"
19
	"github.com/hashicorp/go-retryablehttp"
20
	"github.com/sirupsen/logrus"
21

22
	"github.com/privacybydesign/irmago/internal/disable_sigpipe"
23
	"github.com/privacybydesign/irmago/internal/fs"
24
25
)

Sietse Ringers's avatar
Sietse Ringers committed
26
// HTTPTransport sends and receives JSON messages to a HTTP server.
27
type HTTPTransport struct {
Sietse Ringers's avatar
Sietse Ringers committed
28
	Server  string
29
	client  *retryablehttp.Client
Sietse Ringers's avatar
Sietse Ringers committed
30
	headers map[string]string
31
32
}

33
34
// Logger is used for logging. If not set, init() will initialize it to logrus.StandardLogger().
var Logger *logrus.Logger
Sietse Ringers's avatar
Sietse Ringers committed
35

36
37
38
var transportlogger *log.Logger

func init() {
39
40
	if Logger == nil {
		Logger = logrus.StandardLogger()
41
42
43
	}
}

Sietse Ringers's avatar
Sietse Ringers committed
44
// NewHTTPTransport returns a new HTTPTransport.
45
func NewHTTPTransport(serverURL string) *HTTPTransport {
46
47
	if Logger.IsLevelEnabled(logrus.TraceLevel) {
		transportlogger = log.New(Logger.WriterLevel(logrus.TraceLevel), "transport: ", 0)
48
49
	} else {
		transportlogger = log.New(ioutil.Discard, "", 0)
50
51
	}

52
	url := serverURL
53
	if serverURL != "" && !strings.HasSuffix(url, "/") { // TODO fix this
54
55
		url += "/"
	}
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

	// Create a transport that dials with a SIGPIPE handler (which is only active on iOS)
	var innerTransport http.Transport

	innerTransport.Dial = func(network, addr string) (c net.Conn, err error) {
		c, err = net.Dial(network, addr)
		if err != nil {
			return c, err
		}
		if err = disable_sigpipe.DisableSigPipe(c); err != nil {
			return c, err
		}
		return c, nil
	}

71
72
73
74
75
76
	client := &retryablehttp.Client{
		Logger:       transportlogger,
		RetryWaitMin: 100 * time.Millisecond,
		RetryWaitMax: 200 * time.Millisecond,
		RetryMax:     2,
		Backoff:      retryablehttp.DefaultBackoff,
77
		CheckRetry: func(ctx context.Context, resp *http.Response, err error) (bool, error) {
78
79
80
81
82
83
84
			// Don't retry on 5xx (which retryablehttp does by default)
			return err != nil || resp.StatusCode == 0, err
		},
		HTTPClient: &http.Client{
			Timeout:   time.Second * 3,
			Transport: &innerTransport,
		},
85
86
	}

Sietse Ringers's avatar
Sietse Ringers committed
87
88
89
	return &HTTPTransport{
		Server:  url,
		headers: map[string]string{},
90
		client:  client,
Sietse Ringers's avatar
Sietse Ringers committed
91
	}
92
93
}

94
// SetHeader sets a header to be sent in requests.
Sietse Ringers's avatar
Sietse Ringers committed
95
96
97
98
func (transport *HTTPTransport) SetHeader(name, val string) {
	transport.headers[name] = val
}

99
func (transport *HTTPTransport) request(
100
	url string, method string, reader io.Reader, contenttype string,
101
) (response *http.Response, err error) {
102
103
	var req retryablehttp.Request
	req.Request, err = http.NewRequest(method, transport.Server+url, reader)
104
105
106
107
108
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}

	req.Header.Set("User-Agent", "irmago")
109
110
	if reader != nil && contenttype != "" {
		req.Header.Set("Content-Type", contenttype)
111
112
113
114
115
	}
	for name, val := range transport.headers {
		req.Header.Set(name, val)
	}

116
	res, err := transport.client.Do(&req)
117
118
119
120
121
122
123
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}
	return res, nil
}

func (transport *HTTPTransport) jsonRequest(url string, method string, result interface{}, object interface{}) error {
124
	if method != http.MethodPost && method != http.MethodGet && method != http.MethodDelete {
125
126
127
128
129
130
131
		panic("Unsupported HTTP method " + method)
	}
	if method == http.MethodGet && object != nil {
		panic("Cannot GET and also post an object")
	}

	var reader io.Reader
132
	var contenttype string
133
	if object != nil {
134
135
136
137
138
139
140
141
142
143
		switch o := object.(type) {
		case []byte:
			Logger.Trace("transport: body (base64): ", base64.StdEncoding.EncodeToString(o))
			contenttype = "application/octet-stream"
			reader = bytes.NewBuffer(o)
		case string:
			Logger.Trace("transport: body: ", o)
			contenttype = "text/plain; charset=UTF-8"
			reader = bytes.NewBuffer([]byte(o))
		default:
144
145
			marshaled, err := json.Marshal(object)
			if err != nil {
Tomas's avatar
Tomas committed
146
				return &SessionError{ErrorType: ErrorSerialization, Err: err}
147
			}
148
			Logger.Trace("transport: body: ", string(marshaled))
149
			contenttype = "application/json; charset=UTF-8"
150
			reader = bytes.NewBuffer(marshaled)
151
152
153
		}
	}

154
	res, err := transport.request(url, method, reader, contenttype)
155
	if err != nil {
156
		return err
157
	}
158
159
160
161
	if method == http.MethodDelete {
		return nil
	}

162
163
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
Tomas's avatar
Tomas committed
164
		return &SessionError{ErrorType: ErrorServerResponse, Err: err, RemoteStatus: res.StatusCode}
165
166
	}
	if res.StatusCode != 200 {
Tomas's avatar
Tomas committed
167
		apierr := &RemoteError{}
Sietse Ringers's avatar
Sietse Ringers committed
168
169
		err = json.Unmarshal(body, apierr)
		if err != nil || apierr.ErrorName == "" { // Not an ApiErrorMessage
Tomas's avatar
Tomas committed
170
			return &SessionError{ErrorType: ErrorServerResponse, RemoteStatus: res.StatusCode}
171
		}
172
		Logger.Tracef("transport: error: %+v", apierr)
Tomas's avatar
Tomas committed
173
		return &SessionError{ErrorType: ErrorApi, RemoteStatus: res.StatusCode, RemoteError: apierr}
174
175
	}

176
	Logger.Tracef("transport: response: %s", string(body))
177
178
179
	if result == nil { // caller doesn't care about server response
		return nil
	}
180
181
182
	if _, resultstr := result.(*string); resultstr {
		*result.(*string) = string(body)
	} else {
183
		err = UnmarshalValidate(body, result)
184
		if err != nil {
Tomas's avatar
Tomas committed
185
			return &SessionError{ErrorType: ErrorServerResponse, Err: err, RemoteStatus: res.StatusCode}
186
		}
187
188
189
190
191
	}

	return nil
}

192
func (transport *HTTPTransport) GetBytes(url string) ([]byte, error) {
193
	res, err := transport.request(url, http.MethodGet, nil, "")
194
195
196
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}
197
198

	if res.StatusCode != 200 {
Tomas's avatar
Tomas committed
199
		return nil, &SessionError{ErrorType: ErrorServerResponse, RemoteStatus: res.StatusCode}
200
	}
201
202
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
Tomas's avatar
Tomas committed
203
		return nil, &SessionError{ErrorType: ErrorServerResponse, Err: err, RemoteStatus: res.StatusCode}
204
205
206
207
	}
	return b, nil
}

208
func (transport *HTTPTransport) GetSignedFile(url string, dest string, hash ConfigurationFileHash) error {
209
210
211
212
	b, err := transport.GetBytes(url)
	if err != nil {
		return err
	}
213
214
215
216
	sha := sha256.Sum256(b)
	if hash != nil && !bytes.Equal(hash, sha[:]) {
		return errors.Errorf("Signature over new file %s is not valid", dest)
	}
217
	if err = fs.EnsureDirectoryExists(filepath.Dir(dest)); err != nil {
218
219
		return err
	}
220
	return fs.SaveFile(dest, b)
221
222
}

223
224
225
226
func (transport *HTTPTransport) GetFile(url string, dest string) error {
	return transport.GetSignedFile(url, dest, nil)
}

Sietse Ringers's avatar
Sietse Ringers committed
227
// Post sends the object to the server and parses its response into result.
Sietse Ringers's avatar
Sietse Ringers committed
228
func (transport *HTTPTransport) Post(url string, result interface{}, object interface{}) error {
229
	return transport.jsonRequest(url, http.MethodPost, result, object)
230
231
}

Sietse Ringers's avatar
Sietse Ringers committed
232
// Get performs a GET request and parses the server's response into result.
Sietse Ringers's avatar
Sietse Ringers committed
233
func (transport *HTTPTransport) Get(url string, result interface{}) error {
234
	return transport.jsonRequest(url, http.MethodGet, result, nil)
235
236
}

Sietse Ringers's avatar
Sietse Ringers committed
237
// Delete performs a DELETE.
238
func (transport *HTTPTransport) Delete() {
239
	_ = transport.jsonRequest("", http.MethodDelete, nil, nil)
240
}