forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
365 lines (323 loc) · 10.1 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015-2020 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"
"time"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/client"
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/overlord/restart"
"github.com/snapcore/snapd/overlord/snapshotstate"
"github.com/snapcore/snapd/overlord/state"
"github.com/snapcore/snapd/snap"
"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.
type Response interface {
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
// A StructuredResponse serializes itself to our standard JSON response format.
type StructuredResponse interface {
Response
JSON() *respJSON
}
// respJSON represents our standard JSON response format.
type respJSON struct {
Type ResponseType `json:"type"`
// Status is the HTTP status code.
Status int `json:"status-code"`
// StatusText is filled by the serving pipeline.
StatusText string `json:"status"`
// Result is a free-form optional result object.
Result interface{} `json:"result"`
// Change is the change ID for an async response.
Change string `json:"change,omitempty"`
// Sources is used in find responses.
Sources []string `json:"sources,omitempty"`
// XXX SuggestedCurrency is part of unsupported paid snap code.
SuggestedCurrency string `json:"suggested-currency,omitempty"`
// Maintenance... are filled as needed by the serving pipeline.
WarningTimestamp *time.Time `json:"warning-timestamp,omitempty"`
WarningCount int `json:"warning-count,omitempty"`
Maintenance *errorResult `json:"maintenance,omitempty"`
}
func (r *respJSON) JSON() *respJSON {
return r
}
func maintenanceForRestartType(rst restart.RestartType) *errorResult {
e := &errorResult{}
switch rst {
case restart.RestartSystem, restart.RestartSystemNow:
e.Kind = client.ErrorKindSystemRestart
e.Message = systemRestartMsg
e.Value = map[string]interface{}{
"op": "reboot",
}
case restart.RestartSystemHaltNow:
e.Kind = client.ErrorKindSystemRestart
e.Message = systemHaltMsg
e.Value = map[string]interface{}{
"op": "halt",
}
case restart.RestartSystemPoweroffNow:
e.Kind = client.ErrorKindSystemRestart
e.Message = systemPoweroffMsg
e.Value = map[string]interface{}{
"op": "poweroff",
}
case restart.RestartDaemon:
e.Kind = client.ErrorKindDaemonRestart
e.Message = daemonRestartMsg
case restart.RestartSocket:
e.Kind = client.ErrorKindDaemonRestart
e.Message = socketRestartMsg
case restart.RestartUnset:
// shouldn't happen, maintenance for unset type should just be nil
panic("internal error: cannot marshal maintenance for RestartUnset")
}
return e
}
func (r *respJSON) addMaintenanceFromRestartType(rst restart.RestartType) {
if rst == restart.RestartUnset {
// nothing to do
return
}
r.Maintenance = maintenanceForRestartType(rst)
}
func (r *respJSON) addWarningCount(count int, stamp time.Time) {
if count == 0 {
return
}
r.WarningCount = count
r.WarningTimestamp = &stamp
}
func (r *respJSON) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
status := r.Status
r.StatusText = http.StatusText(r.Status)
bs, err := json.Marshal(r)
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)
}
// SyncResponse builds a "sync" response from the given result.
func SyncResponse(result interface{}) Response {
if rsp, ok := result.(Response); ok {
return rsp
}
if err, ok := result.(error); ok {
return InternalError("internal error: %v", err)
}
return &respJSON{
Type: ResponseTypeSync,
Status: 200,
Result: result,
}
}
// AsyncResponse builds an "async" response for a created change
func AsyncResponse(result map[string]interface{}, change string) Response {
return &respJSON{
Type: ResponseTypeAsync,
Status: 202,
Result: result,
Change: change,
}
}
// A snapStream ServeHTTP method streams a snap
type snapStream struct {
SnapName string
Filename string
Info *snap.DownloadInfo
Token string
stream io.ReadCloser
resume int64
}
// ServeHTTP from the Response interface
func (s *snapStream) ServeHTTP(w http.ResponseWriter, _ *http.Request) {
hdr := w.Header()
hdr.Set("Content-Type", "application/octet-stream")
snapname := fmt.Sprintf("attachment; filename=%s", s.Filename)
hdr.Set("Content-Disposition", snapname)
hdr.Set("Snap-Sha3-384", s.Info.Sha3_384)
// can't set Content-Length when stream is nil as it breaks http clients
// setting it also when there is a stream, for consistency
hdr.Set("Snap-Length", strconv.FormatInt(s.Info.Size, 10))
if s.Token != "" {
hdr.Set("Snap-Download-Token", s.Token)
}
if s.stream == nil {
// nothing to actually stream
return
}
hdr.Set("Content-Length", strconv.FormatInt(s.Info.Size-s.resume, 10))
if s.resume > 0 {
hdr.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", s.resume, s.Info.Size-1, s.Info.Size))
w.WriteHeader(206)
}
defer s.stream.Close()
bytesCopied, err := io.Copy(w, s.stream)
if err != nil {
logger.Noticef("cannot copy snap %s (%#v) to the stream: %v", s.SnapName, s.Info, err)
http.Error(w, err.Error(), 500)
}
if bytesCopied != s.Info.Size-s.resume {
logger.Noticef("cannot copy snap %s (%#v) to the stream: bytes copied=%d, expected=%d", s.SnapName, s.Info, bytesCopied, s.Info.Size)
http.Error(w, io.EOF.Error(), 502)
}
}
// A snapshotExportResponse 's ServeHTTP method serves a specific snapshot ID
type snapshotExportResponse struct {
*snapshotstate.SnapshotExport
setID uint64
st *state.State
}
// ServeHTTP from the Response interface
func (s snapshotExportResponse) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Length", strconv.FormatInt(s.Size(), 10))
w.Header().Add("Content-Type", client.SnapshotExportMediaType)
if err := s.StreamTo(w); err != nil {
logger.Debugf("cannot export snapshot: %v", err)
}
s.Close()
s.st.Lock()
defer s.st.Unlock()
snapshotstate.UnsetSnapshotOpInProgress(s.st, s.setID)
}
// 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
}
}
}