-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
178 lines (155 loc) · 4.29 KB
/
request.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
package api
import (
"fmt"
"log"
"context"
"strings"
"net/http"
"path/filepath"
"github.com/go-errors/errors"
"github.com/go-json-experiment/json"
"github.com/clarkk/go-api/head"
"github.com/clarkk/go-api/invalid_json"
"github.com/clarkk/go-util/serv/req"
)
type (
Request struct {
w http.ResponseWriter
r *http.Request
handle_gzip bool
accept_gzip bool
code int
header List
header_sent bool
bytes_sent int
deferred func(*Request)
}
Input interface {
Required() error
}
List map[string]string
)
func Input_required_error(s []string) error {
if len(s) == 0 {
return nil
}
return fmt.Errorf("Required fields: %s", strings.Join(s, ", "))
}
func New(w http.ResponseWriter, r *http.Request, handle_gzip bool) *Request {
return &Request{
w: w,
r: r,
handle_gzip: handle_gzip,
accept_gzip: accept_gzip(r, handle_gzip),
header: List{},
}
}
// Recover from panic inside route handler
func (a *Request) Recover(){
if err := recover(); err != nil {
a.Errorf(http.StatusInternalServerError, "Unexpected error")
log.Println(errors.Wrap(err, 2).ErrorStack())
}
}
// Set deferred function
func (a *Request) Defer(fn func(*Request)){
a.deferred = fn
}
// Get request context
func (a *Request) Request_context() context.Context {
return a.r.Context()
}
// Get request header
func (a *Request) Request_header(name string) string {
return a.r.Header.Get(name)
}
// Get request URL path
func (a *Request) Request_URL_path() string {
return filepath.Clean(a.r.URL.Path)
}
// Parse request POST body
func (a *Request) Request(post_limit int) ([]byte, int, error){
b, err := req.Post_limit_read(a.w, a.r, post_limit)
if err != nil {
if error_request_too_large(err) {
return nil, http.StatusRequestEntityTooLarge, fmt.Errorf("POST payload too large")
}
return nil, http.StatusInternalServerError, fmt.Errorf("Unable to read request body")
}
return b, 0, nil
}
// Parse request POST body as JSON
func (a *Request) Request_JSON(post_limit int, input any) (int, error){
b, code, err := a.request_JSON(post_limit)
if err != nil {
return code, err
}
if err := json.Unmarshal(b, input, json.RejectUnknownMembers(true)); err != nil {
switch t := err.(type) {
case *json.SemanticError:
return http.StatusBadRequest, invalid_json.Fields(t, b, input)
}
return http.StatusBadRequest, fmt.Errorf("Unable to unmarshal JSON")
}
return 0, nil
}
// Parse request POST body as JSON and require input
func (a *Request) Request_JSON_required(post_limit int, input Input) (int, error){
if code, err := a.Request_JSON(post_limit, input); err != nil {
return code, err
}
if err := input.Required(); err != nil {
return http.StatusBadRequest, err
}
return 0, nil
}
// Parse request POST body as JSON array
/*func (a *Request) Request_JSON_slice(post_limit int, input any) (error, []error, int){
b, err, code := a.request_JSON(post_limit)
if err != nil {
return err, nil, code
}
if err := json.Unmarshal(b, input, json.RejectUnknownMembers(true)); err != nil {
switch t := err.(type) {
case *json.SemanticError:
err, slice_errs := parse_unmarshal_json_slice_error(t, b, input)
if err != nil {
a.Error(http.StatusBadRequest, err)
} else {
a.Bulk_semantic_errors(http.StatusBadRequest, slice_errs)
}
return err, slice_errs, false
}
a.Errorf(http.StatusBadRequest, "Unable to unmarshal JSON")
return err, nil, false
}
return nil, nil, 0
}*/
func (a *Request) request_JSON(post_limit int) ([]byte, int, error){
b, code, err := a.Request(post_limit)
if err != nil {
return nil, code, err
}
if !head.Request_JSON(a.r) {
return nil, http.StatusUnsupportedMediaType, fmt.Errorf("POST payload must be JSON")
}
return b, 0, nil
}
func accept_gzip(r *http.Request, handle_gzip bool) bool {
if !handle_gzip {
return false
}
header := r.Header.Get(head.ACCEPT_ENCODING)
if header == "" {
return false
}
for _, value := range strings.Split(header, ",") {
if strings.TrimSpace(value) == head.ENCODING_GZIP {
return true
}
}
return false
}
func error_request_too_large(err error) bool {
return err.Error() == "http: request body too large"
}