-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathresponse.go
249 lines (214 loc) · 7.15 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
// -*- 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 (
"encoding/json"
"fmt"
"mime"
"net/http"
"path/filepath"
"strconv"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/logger"
)
// 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 = http.StatusInternalServerError
}
hdr := w.Header()
if r.Status == http.StatusAccepted || r.Status == http.StatusCreated {
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")
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: http.StatusOK,
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: http.StatusAccepted,
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 == http.StatusUnauthorized {
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))
}
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(http.StatusOK)
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(http.StatusUnauthorized)
NotFound = makeErrorResponder(http.StatusNotFound)
BadRequest = makeErrorResponder(http.StatusBadRequest)
BadMethod = makeErrorResponder(http.StatusMethodNotAllowed)
InternalError = makeErrorResponder(http.StatusInternalServerError)
NotImplemented = makeErrorResponder(http.StatusNotImplemented)
Forbidden = makeErrorResponder(http.StatusForbidden)
Conflict = makeErrorResponder(http.StatusConflict)
)