-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
156 lines (139 loc) · 3.76 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
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-util/serv/req"
)
type (
Request struct {
w http.ResponseWriter
r *http.Request
handle_gzip bool
accept_gzip bool
header_sent bool
header List
}
List map[string]string
)
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())
}
}
// 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, error){
b, err := req.Post_limit_read(a.w, a.r, post_limit)
if err != nil {
if error_request_too_large(err) {
a.Error(http.StatusRequestEntityTooLarge, nil)
return b, err
}
a.Errorf(http.StatusInternalServerError, "Unable to read request body")
return b, err
}
return b, nil
}
// Parse request POST body as JSON
func (a *Request) Request_JSON(post_limit int, input any) error {
b, err := a.request_JSON(post_limit)
if err != nil {
return err
}
if err := json.Unmarshal(b, input, json.RejectUnknownMembers(true)); err != nil {
var serr *json.SemanticError
if errors.As(err, &serr) {
err = parse_unmarshal_json_error(serr, b, input)
a.Error(http.StatusBadRequest, err)
return err
}
a.Errorf(http.StatusBadRequest, "Unable to unmarshal JSON")
return err
}
return nil
}
// Parse request POST body as JSON array
func (a *Request) Request_JSON_slice(post_limit int, input any) (error, []error, bool){
b, err := a.request_JSON(post_limit)
if err != nil {
return err, nil, false
}
if err := json.Unmarshal(b, input, json.RejectUnknownMembers(true)); err != nil {
var serr *json.SemanticError
if errors.As(err, &serr) {
err, slice_errs := parse_unmarshal_json_slice_error(serr, 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, true
}
func (a *Request) request_JSON(post_limit int) ([]byte, error){
b, err := req.Post_limit_read(a.w, a.r, post_limit)
if err != nil {
if error_request_too_large(err) {
a.Error(http.StatusRequestEntityTooLarge, nil)
return b, err
}
a.Errorf(http.StatusInternalServerError, "Unable to read request body")
return b, err
}
if !head.Request_JSON(a.r) {
a.Error(http.StatusUnsupportedMediaType, nil)
return b, fmt.Errorf("Unsupported media type")
}
return b, 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"
}