forked from emicklei/go-restful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
142 lines (124 loc) · 4.7 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
package restful
// Copyright 2013 Ernest Micklei. All rights reserved.
// Use of this source code is governed by a license
// that can be found in the LICENSE file.
import (
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
"strings"
)
var defaultRequestContentType string
var doCacheReadEntityBytes = true
// Request is a wrapper for a http Request that provides convenience methods
type Request struct {
Request *http.Request
bodyContent *[]byte // to cache the request body for multiple reads of ReadEntity
pathParameters map[string]string
attributes map[string]interface{} // for storing request-scoped values
selectedRoutePath string // root path + route path that matched the request, e.g. /meetings/{id}/attendees
}
func NewRequest(httpRequest *http.Request) *Request {
return &Request{
Request: httpRequest,
pathParameters: map[string]string{},
attributes: map[string]interface{}{},
} // empty parameters, attributes
}
// If ContentType is missing or */* is given then fall back to this type, otherwise
// a "Unable to unmarshal content of type:" response is returned.
// Valid values are restful.MIME_JSON and restful.MIME_XML
// Example:
// restful.DefaultRequestContentType(restful.MIME_JSON)
func DefaultRequestContentType(mime string) {
defaultRequestContentType = mime
}
// SetCacheReadEntity controls whether the response data ([]byte) is cached such that ReadEntity is repeatable.
// Default is true (due to backwardcompatibility). For better performance, you should set it to false if you don't need it.
func SetCacheReadEntity(doCache bool) {
doCacheReadEntityBytes = doCache
}
// PathParameter accesses the Path parameter value by its name
func (r *Request) PathParameter(name string) string {
return r.pathParameters[name]
}
// PathParameters accesses the Path parameter values
func (r *Request) PathParameters() map[string]string {
return r.pathParameters
}
// QueryParameter returns the (first) Query parameter value by its name
func (r *Request) QueryParameter(name string) string {
return r.Request.FormValue(name)
}
// BodyParameter parses the body of the request (once for typically a POST or a PUT) and returns the value of the given name or an error.
func (r *Request) BodyParameter(name string) (string, error) {
err := r.Request.ParseForm()
if err != nil {
return "", err
}
return r.Request.PostFormValue(name), nil
}
// HeaderParameter returns the HTTP Header value of a Header name or empty if missing
func (r *Request) HeaderParameter(name string) string {
return r.Request.Header.Get(name)
}
// ReadEntity checks the Accept header and reads the content into the entityPointer
// May be called multiple times in the request-response flow
func (r *Request) ReadEntity(entityPointer interface{}) (err error) {
contentType := r.Request.Header.Get(HEADER_ContentType)
if doCacheReadEntityBytes {
return r.cachingReadEntity(contentType, entityPointer)
}
// unmarshall directly from request Body
if strings.Contains(contentType, MIME_XML) {
return xml.NewDecoder(r.Request.Body).Decode(entityPointer)
}
if strings.Contains(contentType, MIME_JSON) {
return json.NewDecoder(r.Request.Body).Decode(entityPointer)
}
if MIME_XML == defaultRequestContentType {
return xml.NewDecoder(r.Request.Body).Decode(entityPointer)
}
if MIME_JSON == defaultRequestContentType {
return json.NewDecoder(r.Request.Body).Decode(entityPointer)
}
return NewError(400, "Unable to unmarshal content of type:"+contentType)
}
func (r *Request) cachingReadEntity(contentType string, entityPointer interface{}) (err error) {
var buffer []byte
if r.bodyContent != nil {
buffer = *r.bodyContent
} else {
buffer, err = ioutil.ReadAll(r.Request.Body)
if err != nil {
return err
}
r.bodyContent = &buffer
}
if strings.Contains(contentType, MIME_XML) {
return xml.Unmarshal(buffer, entityPointer)
}
if strings.Contains(contentType, MIME_JSON) {
return json.Unmarshal(buffer, entityPointer)
}
if MIME_XML == defaultRequestContentType {
return xml.Unmarshal(buffer, entityPointer)
}
if MIME_JSON == defaultRequestContentType {
return json.Unmarshal(buffer, entityPointer)
}
return NewError(400, "Unable to unmarshal content of type:"+contentType)
}
// SetAttribute adds or replaces the attribute with the given value.
func (r *Request) SetAttribute(name string, value interface{}) {
r.attributes[name] = value
}
// Attribute returns the value associated to the given name. Returns nil if absent.
func (r Request) Attribute(name string) interface{} {
return r.attributes[name]
}
// SelectedRoutePath root path + route path that matched the request, e.g. /meetings/{id}/attendees
func (r Request) SelectedRoutePath() string {
return r.selectedRoutePath
}