-
Notifications
You must be signed in to change notification settings - Fork 492
/
headertojson.go
213 lines (181 loc) · 4.71 KB
/
headertojson.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
/*
* Copyright (c) 2017, The Easegress Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package headertojson implements a filter to convert HTTP request header to json.
package headertojson
import (
"errors"
"io"
"net/http"
"github.com/easegress-io/easegress/v2/pkg/context"
"github.com/easegress-io/easegress/v2/pkg/filters"
json "github.com/goccy/go-json"
)
const (
// Kind is the kind of Kafka
Kind = "HeaderToJSON"
resultJSONEncodeDecodeErr = "jsonEncodeDecodeErr"
resultBodyReadErr = "bodyReadErr"
)
var (
errJSONEncodeDecode = errors.New(resultJSONEncodeDecodeErr)
)
var kind = &filters.Kind{
Name: Kind,
Description: "HeaderToJSON convert http request header to json",
Results: []string{
resultJSONEncodeDecodeErr,
resultBodyReadErr,
},
DefaultSpec: func() filters.Spec {
return &Spec{}
},
CreateInstance: func(spec filters.Spec) filters.Filter {
return &HeaderToJSON{spec: spec.(*Spec)}
},
}
func init() {
filters.Register(kind)
}
type (
// HeaderToJSON put http request headers into body as JSON fields.
HeaderToJSON struct {
spec *Spec
headerMap map[string]string
}
)
var _ filters.Filter = (*HeaderToJSON)(nil)
// Name returns the name of the HeaderToJSON filter instance.
func (h *HeaderToJSON) Name() string {
return h.spec.Name()
}
// Kind return kind of HeaderToJSON
func (h *HeaderToJSON) Kind() *filters.Kind {
return kind
}
// Spec returns the spec used by the HeaderToJSON
func (h *HeaderToJSON) Spec() filters.Spec {
return h.spec
}
func (h *HeaderToJSON) init() {
h.headerMap = make(map[string]string)
for _, header := range h.spec.HeaderMap {
h.headerMap[http.CanonicalHeaderKey(header.Header)] = header.JSON
}
}
// Init init HeaderToJSON
func (h *HeaderToJSON) Init() {
h.init()
}
// Inherit init HeaderToJSON based on previous generation
func (h *HeaderToJSON) Inherit(previousGeneration filters.Filter) {
h.Init()
}
// Close close HeaderToJSON
func (h *HeaderToJSON) Close() {
}
// Status return status of HeaderToJSON
func (h *HeaderToJSON) Status() interface{} {
return nil
}
// Handle handle Context
func (h *HeaderToJSON) Handle(ctx *context.Context) string {
req := ctx.GetInputRequest()
headerMap := make(map[string]interface{})
for header, json := range h.headerMap {
value := req.Header().Get(header)
if value != "" {
headerMap[json] = value
}
}
if len(headerMap) == 0 {
return ""
}
if req.IsStream() {
return resultBodyReadErr
}
reqBody := req.RawPayload()
var body interface{}
if len(reqBody) == 0 {
body = headerMap
} else {
var err error
if body, err = getNewBody(reqBody, headerMap); err != nil {
return resultJSONEncodeDecodeErr
}
}
bodyBytes, err := json.Marshal(body)
if err != nil {
return resultJSONEncodeDecodeErr
}
req.SetPayload(bodyBytes)
return ""
}
func decodeMapJSON(body []byte) (map[string]interface{}, error) {
res := make(map[string]interface{})
err := json.Unmarshal(body, &res)
if err != nil && err != io.EOF {
return nil, err
}
return res, nil
}
func decodeArrayJSON(body []byte) ([]map[string]interface{}, error) {
res := []map[string]interface{}{}
err := json.Unmarshal(body, &res)
if err != nil && err != io.EOF {
return nil, err
}
return res, nil
}
func mergeMap(m1 map[string]interface{}, m2 map[string]interface{}) map[string]interface{} {
for k, v := range m2 {
m1[k] = v
}
return m1
}
func mergeMapToArrayMap(arrayMap []map[string]interface{}, m map[string]interface{}) []map[string]interface{} {
for i := range arrayMap {
arrayMap[i] = mergeMap(arrayMap[i], m)
}
return arrayMap
}
func firstNonBlandByte(bytes []byte) byte {
for _, b := range bytes {
switch b {
case '\n', '\t', '\r', ' ':
continue
}
return b
}
return 0
}
func getNewBody(reqBody []byte, headerMap map[string]interface{}) (interface{}, error) {
char := firstNonBlandByte(reqBody)
if char == '{' {
bodyMap, err := decodeMapJSON(reqBody)
if err != nil {
return nil, errJSONEncodeDecode
}
return mergeMap(bodyMap, headerMap), nil
} else if char == '[' {
bodyArray, err := decodeArrayJSON(reqBody)
if err != nil {
return nil, errJSONEncodeDecode
}
return mergeMapToArrayMap(bodyArray, headerMap), nil
}
return nil, errJSONEncodeDecode
}