-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver_response.go
222 lines (195 loc) · 6.61 KB
/
server_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
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
package restful
import (
"encoding/json"
"net/http"
"strings"
"github.com/nokia/restful/messagepack"
log "github.com/sirupsen/logrus"
)
func getJSONBody(data any, sanitizeJSON bool) ([]byte, error) {
if data == nil {
return nil, nil // Otherwise "null" (4 bytes) would be returned.
}
body, err := json.Marshal(data)
if err != nil {
return nil, err
}
if sanitizeJSON {
body = SanitizeJSONBytes(body)
if len(body) == 2 && body[0] == '{' && body[1] == '}' {
return nil, nil
}
}
return body, nil
}
// SendJSONResponse sends an HTTP response with an optionally sanitized JSON data.
// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
func SendJSONResponse(w http.ResponseWriter, statusCode int, data any, sanitizeJSON bool) (err error) {
body, err := getJSONBody(data, sanitizeJSON)
if body != nil {
w.Header().Set(ContentTypeHeader, ContentTypeApplicationJSON)
w.WriteHeader(statusCode)
_, err = w.Write(body)
} else {
w.WriteHeader(statusCode)
}
return err
}
func sendResponse(w http.ResponseWriter, r *http.Request, data any, sanitizeJSON bool) (err error) {
okStatus := getOkStatus(w, r, data)
if data == nil {
w.WriteHeader(okStatus)
return nil
}
useMsgPack := false
writeHeaders := w.Header()
if writeHeaders == nil || writeHeaders.Get(ContentTypeHeader) == "" {
useMsgPack = acceptsMsgPack(r)
} else if isMsgPackContentType(GetBaseContentType(writeHeaders)) {
useMsgPack = true
}
if useMsgPack {
b, err := messagepack.Marshal(data)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return err
}
w.Header().Set(ContentTypeHeader, ContentTypeMsgPack)
w.WriteHeader(okStatus)
_, err = w.Write(b)
return err
}
return SendJSONResponse(w, okStatus, data, sanitizeJSON)
}
// SendResponse sends an HTTP response with a JSON data.
// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
func SendResponse(w http.ResponseWriter, statusCode int, data any) error {
return SendJSONResponse(w, statusCode, data, false)
}
func getOkStatus(w http.ResponseWriter, r *http.Request, data any) int {
status := http.StatusOK
if data == nil {
status = http.StatusNoContent
}
if r.Method == http.MethodPost && w.Header().Get("Location") != "" {
status = http.StatusCreated
}
return status
}
// SendResp sends an HTTP response with data.
// On no error 200/201/204 sent according to the request.
// On error send response depending on whether the error is created by NewError and the client supports RFC 7807.
// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
func SendResp(w http.ResponseWriter, r *http.Request, err error, data any) error {
if err == nil {
return sendResponse(w, r, data, LambdaSanitizeJSON)
}
if errStr := err.Error(); errStr != "" { // In some cases status like 404 does not indicate error, just a plain result. E.g. on a distributed cache query.
log.Error(errStr)
}
body, _ := getJSONBody(data, LambdaSanitizeJSON)
if body == nil {
return SendProblemDetails(w, r, err)
}
w.Header().Set(ContentTypeHeader, ContentTypeApplicationJSON)
w.WriteHeader(GetErrStatusCode(err))
_, err = w.Write(body)
return err
}
// SendEmptyResponse sends an empty HTTP response.
// Caller may set additional headers like `w.Header().Set("Location", "https://me")` before calling this function.
func SendEmptyResponse(w http.ResponseWriter, statusCode int) {
w.WriteHeader(statusCode)
}
// SendLocationResponse sends an empty "201 Created" HTTP response with Location header.
func SendLocationResponse(w http.ResponseWriter, location string) {
w.Header().Set("Location", location)
SendEmptyResponse(w, http.StatusCreated)
}
func getProblemContentType(r *http.Request) string {
ct := ContentTypeApplicationJSON
accepts := r.Header.Values(AcceptHeader)
for i := range accepts {
baseCT := BaseContentType(accepts[i])
if baseCT == ContentTypeProblemJSON || baseCT == ContentTypeApplicationAny || baseCT == ContentTypeAny {
ct = ContentTypeProblemJSON
break
}
}
return ct
}
func acceptsMsgPack(r *http.Request) bool {
accepts := r.Header.Values(AcceptHeader)
for i := range accepts {
if isMsgPackContentType(accepts[i]) {
return true
}
}
return false
}
// SendProblemResponse sends response with problem text, and extends it to problem+json format if it is a plain string.
func SendProblemResponse(w http.ResponseWriter, r *http.Request, statusCode int, problem string) (err error) {
if problem == "" {
SendEmptyResponse(w, statusCode)
return nil
}
if problem != "" && problem[0] != '{' {
problem = `{"detail":"` + strings.ReplaceAll(problem, `"`, "'") + `"}`
}
w.Header().Set(ContentTypeHeader, getProblemContentType(r))
w.WriteHeader(statusCode)
if len(problem) > 0 {
_, err = w.Write([]byte(problem))
}
return
}
func acceptContentType(r *http.Request, contentType string) bool {
acceptHeaders := r.Header.Values(AcceptHeader)
if len(acceptHeaders) == 0 {
return true
}
var mainContentType string
ctParts := strings.SplitN(contentType, "/", 2)
if len(ctParts) == 2 {
mainContentType = ctParts[0] + "/*"
}
for i := range acceptHeaders {
baseCT := BaseContentType(acceptHeaders[i])
if baseCT == contentType || baseCT == mainContentType || baseCT == ContentTypeAny {
return true
}
}
return false
}
func sendCustomResponse(r *http.Request, w http.ResponseWriter, body []byte, statusCode int, contentType string) (err error) {
if contentType != "" && acceptContentType(r, contentType) {
w.Header().Set(ContentTypeHeader, contentType)
w.WriteHeader(statusCode)
_, err = w.Write([]byte(body))
return
}
SendEmptyResponse(w, statusCode)
return nil
}
// SendProblemDetails adds detailed problem description to JSON body, if available. See RFC 7807.
func SendProblemDetails(w http.ResponseWriter, r *http.Request, err error) error {
if restErr, ok := err.(*restError); ok {
if len(restErr.body) != 0 {
return sendCustomResponse(r, w, restErr.body, GetErrStatusCode(restErr), restErr.contentType)
}
d := restErr.problemDetails.Detail
// check in case it is somehow already filled with JSON text...
if d != "" && d[0] != '{' {
if restErr.err != nil {
if embeddedStr := restErr.err.Error(); embeddedStr != "" {
restErr.problemDetails.Detail += ": " + embeddedStr
}
}
return SendProblemResponse(w, r, GetErrStatusCode(restErr), restErr.problemDetails.String())
}
}
return SendProblemResponse(w, r, GetErrStatusCode(err), err.Error())
}