-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathresponse.go
346 lines (302 loc) · 9.42 KB
/
response.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"bufio"
"encoding/json"
"fmt"
"io"
"mime"
"net/http"
"path/filepath"
"strconv"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/systemd"
)
// ResponseType is the response type
type ResponseType string
// "there are three standard return types: Standard return value,
// Background operation, Error", each returning a JSON object with the
// following "type" field:
const (
ResponseTypeSync ResponseType = "sync"
ResponseTypeAsync ResponseType = "async"
ResponseTypeError ResponseType = "error"
)
// Response knows how to serve itself, and how to find itself
type Response interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
type resp struct {
Status int `json:"status-code"`
Type ResponseType `json:"type"`
Result interface{} `json:"result,omitempty"`
*Meta
}
// TODO This is being done in a rush to get the proper external
// JSON representation in the API in time for the release.
// The right code style takes a bit more work and unifies
// these fields inside resp.
type Meta struct {
Sources []string `json:"sources,omitempty"`
Paging *Paging `json:"paging,omitempty"`
SuggestedCurrency string `json:"suggested-currency,omitempty"`
Change string `json:"change,omitempty"`
}
type Paging struct {
Page int `json:"page"`
Pages int `json:"pages"`
}
type respJSON struct {
Type ResponseType `json:"type"`
Status int `json:"status-code"`
StatusText string `json:"status"`
Result interface{} `json:"result"`
*Meta
}
func (r *resp) MarshalJSON() ([]byte, error) {
return json.Marshal(respJSON{
Type: r.Type,
Status: r.Status,
StatusText: http.StatusText(r.Status),
Result: r.Result,
Meta: r.Meta,
})
}
func (r *resp) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
status := r.Status
bs, err := r.MarshalJSON()
if err != nil {
logger.Noticef("cannot marshal %#v to JSON: %v", *r, err)
bs = nil
status = 500
}
hdr := w.Header()
if r.Status == 202 || r.Status == 201 {
if m, ok := r.Result.(map[string]interface{}); ok {
if location, ok := m["resource"]; ok {
if location, ok := location.(string); ok && location != "" {
hdr.Set("Location", location)
}
}
}
}
hdr.Set("Content-Type", "application/json")
w.WriteHeader(status)
w.Write(bs)
}
type errorKind string
const (
errorKindTwoFactorRequired = errorKind("two-factor-required")
errorKindTwoFactorFailed = errorKind("two-factor-failed")
errorKindLoginRequired = errorKind("login-required")
errorKindInvalidAuthData = errorKind("invalid-auth-data")
errorKindTermsNotAccepted = errorKind("terms-not-accepted")
errorKindNoPaymentMethods = errorKind("no-payment-methods")
errorKindPaymentDeclined = errorKind("payment-declined")
errorKindPasswordPolicy = errorKind("password-policy")
errorKindSnapAlreadyInstalled = errorKind("snap-already-installed")
errorKindSnapNotInstalled = errorKind("snap-not-installed")
errorKindSnapNotFound = errorKind("snap-not-found")
errorKindAppNotFound = errorKind("app-not-found")
errorKindSnapLocal = errorKind("snap-local")
errorKindSnapNoUpdateAvailable = errorKind("snap-no-update-available")
errorKindNotSnap = errorKind("snap-not-a-snap")
errorKindSnapNeedsDevMode = errorKind("snap-needs-devmode")
errorKindSnapNeedsClassic = errorKind("snap-needs-classic")
errorKindSnapNeedsClassicSystem = errorKind("snap-needs-classic-system")
)
type errorValue interface{}
type errorResult struct {
Message string `json:"message"` // note no omitempty
Kind errorKind `json:"kind,omitempty"`
Value errorValue `json:"value,omitempty"`
}
// SyncResponse builds a "sync" response from the given result.
func SyncResponse(result interface{}, meta *Meta) Response {
if err, ok := result.(error); ok {
return InternalError("internal error: %v", err)
}
if rsp, ok := result.(Response); ok {
return rsp
}
return &resp{
Type: ResponseTypeSync,
Status: 200,
Result: result,
Meta: meta,
}
}
// AsyncResponse builds an "async" response from the given *Task
func AsyncResponse(result map[string]interface{}, meta *Meta) Response {
return &resp{
Type: ResponseTypeAsync,
Status: 202,
Result: result,
Meta: meta,
}
}
// makeErrorResponder builds an errorResponder from the given error status.
func makeErrorResponder(status int) errorResponder {
return func(format string, v ...interface{}) Response {
res := &errorResult{
Message: fmt.Sprintf(format, v...),
}
if status == 401 {
res.Kind = errorKindLoginRequired
}
return &resp{
Type: ResponseTypeError,
Result: res,
Status: status,
}
}
}
// A FileResponse 's ServeHTTP method serves the file
type FileResponse string
// ServeHTTP from the Response interface
func (f FileResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
filename := fmt.Sprintf("attachment; filename=%s", filepath.Base(string(f)))
w.Header().Add("Content-Disposition", filename)
http.ServeFile(w, r, string(f))
}
// A journalLineReaderSeqResponse's ServeHTTP method reads lines (presumed to
// be, each one on its own, a JSON dump of a systemd.Log, as output by
// journalctl -o json) from an io.ReadCloser, loads that into a client.Log, and
// outputs the json dump of that, padded with RS and LF to make it a valid
// json-seq response.
//
// The reader is always closed when done (this is important for
// osutil.WatingStdoutPipe).
//
// Tip: “jq” knows how to read this; “jq --seq” both reads and writes this.
type journalLineReaderSeqResponse struct {
io.ReadCloser
follow bool
}
func (rr *journalLineReaderSeqResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json-seq")
flusher, hasFlusher := w.(http.Flusher)
var err error
dec := json.NewDecoder(rr)
writer := bufio.NewWriter(w)
enc := json.NewEncoder(writer)
for {
var log systemd.Log
if err = dec.Decode(&log); err != nil {
break
}
writer.WriteByte(0x1E) // RS -- see ascii(7), and RFC7464
// ignore the error...
t, _ := log.Time()
if err = enc.Encode(client.Log{
Timestamp: t,
Message: log.Message(),
SID: log.SID(),
PID: log.PID(),
}); err != nil {
break
}
if rr.follow {
if e := writer.Flush(); e != nil {
break
}
if hasFlusher {
flusher.Flush()
}
}
}
if err != nil && err != io.EOF {
fmt.Fprintf(writer, `\x1E{"error": %q}\n`, err)
logger.Noticef("cannot stream response; problem reading: %v", err)
}
if err := writer.Flush(); err != nil {
logger.Noticef("cannot stream response; problem writing: %v", err)
}
rr.Close()
}
type assertResponse struct {
assertions []asserts.Assertion
bundle bool
}
// AssertResponse builds a response whose ServerHTTP method serves one or a bundle of assertions.
func AssertResponse(asserts []asserts.Assertion, bundle bool) Response {
if len(asserts) > 1 {
bundle = true
}
return &assertResponse{assertions: asserts, bundle: bundle}
}
func (ar assertResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t := asserts.MediaType
if ar.bundle {
t = mime.FormatMediaType(t, map[string]string{"bundle": "y"})
}
w.Header().Set("Content-Type", t)
w.Header().Set("X-Ubuntu-Assertions-Count", strconv.Itoa(len(ar.assertions)))
w.WriteHeader(200)
enc := asserts.NewEncoder(w)
for _, a := range ar.assertions {
err := enc.Encode(a)
if err != nil {
logger.Noticef("cannot write encoded assertion into response: %v", err)
break
}
}
}
// errorResponder is a callable that produces an error Response.
// e.g., InternalError("something broke: %v", err), etc.
type errorResponder func(string, ...interface{}) Response
// standard error responses
var (
Unauthorized = makeErrorResponder(401)
NotFound = makeErrorResponder(404)
BadRequest = makeErrorResponder(400)
MethodNotAllowed = makeErrorResponder(405)
InternalError = makeErrorResponder(500)
NotImplemented = makeErrorResponder(501)
Forbidden = makeErrorResponder(403)
Conflict = makeErrorResponder(409)
)
// SnapNotFound is an error responder used when an operation is
// requested on a snap that doesn't exist.
func SnapNotFound(snapName string, err error) Response {
return &resp{
Type: ResponseTypeError,
Result: &errorResult{
Message: err.Error(),
Kind: errorKindSnapNotFound,
Value: snapName,
},
Status: 404,
}
}
// AppNotFound is an error responder used when an operation is
// requested on a app that doesn't exist.
func AppNotFound(format string, v ...interface{}) Response {
res := &errorResult{
Message: fmt.Sprintf(format, v...),
Kind: errorKindAppNotFound,
}
return &resp{
Type: ResponseTypeError,
Result: res,
Status: 404,
}
}