This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
265 lines (224 loc) · 7.05 KB
/
context.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
package huma
import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/fxamacker/cbor/v2"
"github.com/goccy/go-yaml"
"github.com/istreamlabs/huma/negotiation"
)
// ContextFromRequest returns a Huma context for a request, useful for
// accessing high-level convenience functions from e.g. middleware.
func ContextFromRequest(w http.ResponseWriter, r *http.Request) Context {
return &hcontext{
Context: r.Context(),
ResponseWriter: w,
r: r,
}
}
// Context provides a request context and response writer with convenience
// functions for error and model marshaling in handler functions.
type Context interface {
context.Context
http.ResponseWriter
// WithValue returns a shallow copy of the context with a new context value
// applied to it.
WithValue(key, value interface{}) Context
// SetValue sets a context value. The Huma context is modified in place while
// the underlying request context is copied. This is particularly useful for
// setting context values from input resolver functions.
SetValue(key, value interface{})
// AddError adds a new error to the list of errors for this request.
AddError(err error)
// HasError returns true if at least one error has been added to the context.
HasError() bool
// WriteError writes out an HTTP status code, friendly error message, and
// optionally a set of error details set with `AddError` and/or passed in.
WriteError(status int, message string, errors ...error)
// WriteModel writes out an HTTP status code and marshalled model based on
// content negotiation (e.g. JSON or CBOR). This must match the registered
// response status code & type.
WriteModel(status int, model interface{})
}
type hcontext struct {
context.Context
http.ResponseWriter
r *http.Request
errors []error
op *Operation
closed bool
}
func (c *hcontext) WithValue(key, value interface{}) Context {
r := c.r.WithContext(context.WithValue(c.r.Context(), key, value))
return &hcontext{
Context: context.WithValue(c.Context, key, value),
ResponseWriter: c.ResponseWriter,
r: r,
errors: append([]error{}, c.errors...),
op: c.op,
closed: c.closed,
}
}
func (c *hcontext) SetValue(key, value interface{}) {
c.r = c.r.WithContext(context.WithValue(c.r.Context(), key, value))
c.Context = c.r.Context()
}
func (c *hcontext) AddError(err error) {
c.errors = append(c.errors, err)
}
func (c *hcontext) HasError() bool {
return len(c.errors) > 0
}
func (c *hcontext) WriteHeader(status int) {
if c.op != nil {
allowed := []string{}
for _, r := range c.op.responses {
if r.status == status {
for _, h := range r.headers {
allowed = append(allowed, h)
}
}
}
// Check that all headers were allowed to be sent.
for name := range c.Header() {
found := false
for _, h := range allowed {
if strings.ToLower(name) == strings.ToLower(h) {
found = true
break
}
}
if !found {
panic(fmt.Errorf("Response header %s is not declared for %s %s with status code %d (allowed: %s)", name, c.r.Method, c.r.URL.Path, status, allowed))
}
}
}
c.ResponseWriter.WriteHeader(status)
}
func (c *hcontext) Write(data []byte) (int, error) {
if c.closed {
panic(fmt.Errorf("Trying to write to response after WriteModel or WriteError for %s %s", c.r.Method, c.r.URL.Path))
}
return c.ResponseWriter.Write(data)
}
func (c *hcontext) WriteError(status int, message string, errors ...error) {
if c.closed {
panic(fmt.Errorf("Trying to write to response after WriteModel or WriteError for %s %s", c.r.Method, c.r.URL.Path))
}
details := []*ErrorDetail{}
c.errors = append(c.errors, errors...)
for _, err := range c.errors {
if d, ok := err.(ErrorDetailer); ok {
details = append(details, d.ErrorDetail())
} else {
details = append(details, &ErrorDetail{Message: err.Error()})
}
}
model := &ErrorModel{
Title: http.StatusText(status),
Status: status,
Detail: message,
Errors: details,
}
// Select content type and transform it to the appropriate error type.
ct := selectContentType(c.r)
switch ct {
case "application/cbor":
ct = "application/problem+cbor"
case "", "application/json":
ct = "application/problem+json"
case "application/yaml", "application/x-yaml":
ct = "application/problem+yaml"
}
c.writeModel(ct, status, model)
}
func (c *hcontext) WriteModel(status int, model interface{}) {
if c.closed {
panic(fmt.Errorf("Trying to write to response after WriteModel or WriteError for %s %s", c.r.Method, c.r.URL.Path))
}
// Get the negotiated content type the client wants and we are willing to
// provide.
ct := selectContentType(c.r)
c.writeModel(ct, status, model)
}
func (c *hcontext) writeModel(ct string, status int, model interface{}) {
// Is this allowed? Find the right response.
if c.op != nil {
responses := []Response{}
names := []string{}
statuses := []string{}
for _, r := range c.op.responses {
statuses = append(statuses, fmt.Sprintf("%d", r.status))
if r.status == status {
responses = append(responses, r)
if r.model != nil {
names = append(names, r.model.Name())
}
}
}
if len(responses) == 0 {
panic(fmt.Errorf("HTTP status %d not allowed for %s %s, expected one of %s", status, c.r.Method, c.r.URL.Path, statuses))
}
found := false
for _, r := range responses {
if r.model == reflect.TypeOf(model) {
found = true
break
}
}
if !found {
panic(fmt.Errorf("Invalid model %s, expecting %s for %s %s", reflect.TypeOf(model), strings.Join(names, ", "), c.r.Method, c.r.URL.Path))
}
}
// Do the appropriate encoding.
var encoded []byte
var err error
if strings.HasPrefix(ct, "application/json") || strings.HasSuffix(ct, "+json") {
encoded, err = json.Marshal(model)
if err != nil {
panic(fmt.Errorf("Unable to marshal JSON: %w", err))
}
} else if strings.HasPrefix(ct, "application/yaml") || strings.HasPrefix(ct, "application/x-yaml") || strings.HasSuffix(ct, "+yaml") {
encoded, err = yaml.Marshal(model)
if err != nil {
panic(fmt.Errorf("Unable to marshal YAML: %w", err))
}
} else if strings.HasPrefix(ct, "application/cbor") || strings.HasSuffix(ct, "+cbor") {
opts := cbor.CanonicalEncOptions()
opts.Time = cbor.TimeRFC3339Nano
opts.TimeTag = cbor.EncTagRequired
mode, err := opts.EncMode()
if err != nil {
panic(fmt.Errorf("Unable to marshal CBOR: %w", err))
}
encoded, err = mode.Marshal(model)
if err != nil {
panic(fmt.Errorf("Unable to marshal JSON: %w", err))
}
}
// Encoding succeeded, write the data!
c.Header().Set("Content-Type", ct)
c.WriteHeader(status)
c.Write(encoded)
c.closed = true
}
// selectContentType selects the best availalable content type via content
// negotiation with the client, defaulting to JSON.
func selectContentType(r *http.Request) string {
ct := "application/json"
if accept := r.Header.Get("Accept"); accept != "" {
best := negotiation.SelectQValue(accept, []string{
"application/cbor",
"application/json",
"application/yaml",
"application/x-yaml",
})
if best != "" {
ct = best
}
}
return ct
}