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

import (
	"bytes"
	"encoding/json"
6
	"fmt"
7
8
9
	"io"
	"io/ioutil"
	"net/http"
10
	"path/filepath"
11
12
	"strings"
	"time"
13

14
	"github.com/privacybydesign/irmago/internal/fs"
15
16
)

Sietse Ringers's avatar
Sietse Ringers committed
17
// HTTPTransport sends and receives JSON messages to a HTTP server.
18
type HTTPTransport struct {
Sietse Ringers's avatar
Sietse Ringers committed
19
20
21
	Server  string
	client  *http.Client
	headers map[string]string
22
23
}

24
const verbose = false
Sietse Ringers's avatar
Sietse Ringers committed
25

Sietse Ringers's avatar
Sietse Ringers committed
26
// NewHTTPTransport returns a new HTTPTransport.
27
28
func NewHTTPTransport(serverURL string) *HTTPTransport {
	url := serverURL
29
	if serverURL != "" && !strings.HasSuffix(url, "/") { // TODO fix this
30
31
32
		url += "/"
	}
	return &HTTPTransport{
Sietse Ringers's avatar
Sietse Ringers committed
33
34
		Server:  url,
		headers: map[string]string{},
35
		client: &http.Client{
Sietse Ringers's avatar
Sietse Ringers committed
36
			Timeout: time.Second * 15,
37
38
39
40
		},
	}
}

41
// SetHeader sets a header to be sent in requests.
Sietse Ringers's avatar
Sietse Ringers committed
42
43
44
45
func (transport *HTTPTransport) SetHeader(name, val string) {
	transport.headers[name] = val
}

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
func (transport *HTTPTransport) request(
	url string, method string, reader io.Reader, isstr bool,
) (response *http.Response, err error) {
	req, err := http.NewRequest(method, transport.Server+url, reader)
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}

	req.Header.Set("User-Agent", "irmago")
	if reader != nil {
		if isstr {
			req.Header.Set("Content-Type", "text/plain; charset=UTF-8")
		} else {
			req.Header.Set("Content-Type", "application/json; charset=UTF-8")
		}
	}
	for name, val := range transport.headers {
		req.Header.Set(name, val)
	}

	res, err := transport.client.Do(req)
	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 {
74
	if method != http.MethodPost && method != http.MethodGet && method != http.MethodDelete {
75
76
77
78
79
80
		panic("Unsupported HTTP method " + method)
	}
	if method == http.MethodGet && object != nil {
		panic("Cannot GET and also post an object")
	}

81
	var isstr bool
82
83
	var reader io.Reader
	if object != nil {
84
85
86
87
88
89
		var objstr string
		if objstr, isstr = object.(string); isstr {
			reader = bytes.NewBuffer([]byte(objstr))
		} else {
			marshaled, err := json.Marshal(object)
			if err != nil {
Tomas's avatar
Tomas committed
90
				return &SessionError{ErrorType: ErrorSerialization, Err: err}
91
			}
Sietse Ringers's avatar
Sietse Ringers committed
92
			if verbose {
93
				fmt.Printf("%s %s: %s\n", method, url, string(marshaled))
Sietse Ringers's avatar
Sietse Ringers committed
94
			}
95
			reader = bytes.NewBuffer(marshaled)
96
		}
97
98
99
100
	} else {
		if verbose {
			fmt.Printf("%s %s\n", method, url)
		}
101
102
	}

103
	res, err := transport.request(url, method, reader, isstr)
104
	if err != nil {
105
		return err
106
	}
107
108
109
110
	if method == http.MethodDelete {
		return nil
	}

111
112
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
113
		return &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode, StatusMessage: res.Status}
114
115
116
	}
	if res.StatusCode != 200 {
		apierr := &ApiError{}
Sietse Ringers's avatar
Sietse Ringers committed
117
118
		err = json.Unmarshal(body, apierr)
		if err != nil || apierr.ErrorName == "" { // Not an ApiErrorMessage
119
			return &SessionError{ErrorType: ErrorServerResponse, Status: res.StatusCode, StatusMessage: res.Status}
120
		}
Sietse Ringers's avatar
Sietse Ringers committed
121
122
123
		if verbose {
			fmt.Printf("ERROR: %+v\n", apierr)
		}
124
		return &SessionError{ErrorType: ErrorApi, Status: res.StatusCode, ApiError: apierr, StatusMessage: res.Status}
125
126
	}

Sietse Ringers's avatar
Sietse Ringers committed
127
128
129
	if verbose {
		fmt.Printf("RESPONSE: %s\n", string(body))
	}
130
131
132
133
134
	if _, resultstr := result.(*string); resultstr {
		*result.(*string) = string(body)
	} else {
		err = json.Unmarshal(body, result)
		if err != nil {
135
			return &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode, StatusMessage: res.Status}
136
		}
137
138
139
140
141
	}

	return nil
}

142
143
144
145
146
func (transport *HTTPTransport) GetBytes(url string) ([]byte, error) {
	res, err := transport.request(url, http.MethodGet, nil, false)
	if err != nil {
		return nil, &SessionError{ErrorType: ErrorTransport, Err: err}
	}
147
148
149
150

	if res.StatusCode != 200 {
		return nil, &SessionError{ErrorType: ErrorServerResponse, Status: res.StatusCode}
	}
151
152
	b, err := ioutil.ReadAll(res.Body)
	if err != nil {
153
		return nil, &SessionError{ErrorType: ErrorServerResponse, Err: err, Status: res.StatusCode, StatusMessage: res.Status}
154
155
156
157
158
159
160
161
162
	}
	return b, nil
}

func (transport *HTTPTransport) GetFile(url string, dest string) error {
	b, err := transport.GetBytes(url)
	if err != nil {
		return err
	}
163
	if err = fs.EnsureDirectoryExists(filepath.Dir(dest)); err != nil {
164
165
		return err
	}
166
	return fs.SaveFile(dest, b)
167
168
}

Sietse Ringers's avatar
Sietse Ringers committed
169
// Post sends the object to the server and parses its response into result.
Sietse Ringers's avatar
Sietse Ringers committed
170
func (transport *HTTPTransport) Post(url string, result interface{}, object interface{}) error {
171
	return transport.jsonRequest(url, http.MethodPost, result, object)
172
173
}

Sietse Ringers's avatar
Sietse Ringers committed
174
// Get performs a GET request and parses the server's response into result.
Sietse Ringers's avatar
Sietse Ringers committed
175
func (transport *HTTPTransport) Get(url string, result interface{}) error {
176
	return transport.jsonRequest(url, http.MethodGet, result, nil)
177
178
}

Sietse Ringers's avatar
Sietse Ringers committed
179
// Delete performs a DELETE.
180
func (transport *HTTPTransport) Delete() {
181
	_ = transport.jsonRequest("", http.MethodDelete, nil, nil)
182
}