/
libhttpagent.go
267 lines (239 loc) · 7.45 KB
/
libhttpagent.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package agents
import (
"encoding/xml"
"fmt"
"io"
"net"
"net/http"
"net/http/httputil"
"strconv"
"strings"
"github.com/antchfx/xmlquery"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
)
// newHADataProvider constructs a DataProvider
func newHADataProvider(reqPayload string,
req *http.Request) (dP utils.DataProvider, err error) {
switch reqPayload {
default:
return nil, fmt.Errorf("unsupported decoder type <%s>", reqPayload)
case utils.MetaUrl:
return newHTTPUrlDP(req)
case utils.MetaXml:
return newHTTPXmlDP(req)
}
}
func newHTTPUrlDP(req *http.Request) (dP utils.DataProvider, err error) {
dP = &httpUrlDP{req: req, cache: utils.MapStorage{}}
return
}
// httpUrlDP implements engine.DataProvider, serving as url data decoder
// decoded data is only searched once and cached
type httpUrlDP struct {
req *http.Request
cache utils.MapStorage
}
// String is part of engine.DataProvider interface
// when called, it will display the already parsed values out of cache
func (hU *httpUrlDP) String() string {
byts, _ := httputil.DumpRequest(hU.req, true)
return string(byts)
}
// FieldAsInterface is part of engine.DataProvider interface
func (hU *httpUrlDP) FieldAsInterface(fldPath []string) (data any, err error) {
if len(fldPath) != 1 {
return nil, utils.ErrNotFound
}
if data, err = hU.cache.FieldAsInterface(fldPath); err != nil {
if err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
} else {
return // data found in cache
}
data = hU.req.FormValue(fldPath[0])
hU.cache.Set(fldPath, data)
return
}
// FieldAsString is part of engine.DataProvider interface
func (hU *httpUrlDP) FieldAsString(fldPath []string) (data string, err error) {
var valIface any
valIface, err = hU.FieldAsInterface(fldPath)
if err != nil {
return
}
return utils.IfaceAsString(valIface), nil
}
// RemoteHost is part of engine.DataProvider interface
func (hU *httpUrlDP) RemoteHost() net.Addr {
return utils.NewNetAddr("TCP", hU.req.RemoteAddr)
}
func newHTTPXmlDP(req *http.Request) (dP utils.DataProvider, err error) {
byteData, err := io.ReadAll(req.Body)
if err != nil {
return nil, err
}
//convert the byteData into a xmlquery Node
doc, err := xmlquery.Parse(strings.NewReader(string(byteData)))
if err != nil {
return nil, err
}
dP = &httpXmlDP{xmlDoc: doc, cache: utils.MapStorage{}, addr: req.RemoteAddr}
return
}
// httpXmlDP implements engine.DataProvider, serving as xml data decoder
// decoded data is only searched once and cached
type httpXmlDP struct {
cache utils.MapStorage
xmlDoc *xmlquery.Node
addr string
}
// String is part of engine.DataProvider interface
// when called, it will display the already parsed values out of cache
func (hU *httpXmlDP) String() string {
return hU.xmlDoc.OutputXML(true)
}
// FieldAsInterface is part of engine.DataProvider interface
func (hU *httpXmlDP) FieldAsInterface(fldPath []string) (data any, err error) {
//if path is missing return here error because if it arrived in xmlquery library will panic
if len(fldPath) == 0 {
return nil, fmt.Errorf("Empty path")
}
if data, err = hU.cache.FieldAsInterface(fldPath); err == nil ||
err != utils.ErrNotFound { // item found in cache
return
}
err = nil // cancel previous err
var slctrStr string
workPath := make([]string, len(fldPath))
for i, val := range fldPath {
workPath[i] = val
}
for i := range workPath {
if sIdx := strings.Index(workPath[i], "["); sIdx != -1 {
slctrStr = workPath[i][sIdx:]
if slctrStr[len(slctrStr)-1:] != "]" {
return nil, fmt.Errorf("filter rule <%s> needs to end in ]", slctrStr)
}
workPath[i] = workPath[i][:sIdx]
if slctrStr[1:2] != "@" {
i, err := strconv.Atoi(slctrStr[1 : len(slctrStr)-1])
if err != nil {
return nil, err
}
slctrStr = "[" + strconv.Itoa(i+1) + "]"
}
workPath[i] = workPath[i] + slctrStr
}
}
//convert workPath to HierarchyPath
hrPath := utils.HierarchyPath(workPath)
elmnt := xmlquery.FindOne(hU.xmlDoc, hrPath.AsString("/", false))
if elmnt == nil {
return
}
//add the content in data and cache it
data = elmnt.InnerText()
hU.cache.Set(fldPath, data)
return
}
// FieldAsString is part of engine.DataProvider interface
func (hU *httpXmlDP) FieldAsString(fldPath []string) (data string, err error) {
var valIface any
valIface, err = hU.FieldAsInterface(fldPath)
if err != nil {
return
}
return utils.IfaceAsString(valIface), nil
}
// RemoteHost is part of engine.DataProvider interface
func (hU *httpXmlDP) RemoteHost() net.Addr {
return utils.NewNetAddr("TCP", hU.addr)
}
// httpAgentReplyEncoder will encode []*engine.NMElement
// and write content to http writer
type httpAgentReplyEncoder interface {
Encode(*utils.OrderedNavigableMap) error
}
// newHAReplyEncoder constructs a httpAgentReqDecoder based on encoder type
func newHAReplyEncoder(encType string,
w http.ResponseWriter) (rE httpAgentReplyEncoder, err error) {
switch encType {
default:
return nil, fmt.Errorf("unsupported encoder type <%s>", encType)
case utils.MetaXml:
return newHAXMLEncoder(w)
case utils.MetaTextPlain:
return newHATextPlainEncoder(w)
}
}
func newHAXMLEncoder(w http.ResponseWriter) (xE httpAgentReplyEncoder, err error) {
return &haXMLEncoder{w: w}, nil
}
type haXMLEncoder struct {
w http.ResponseWriter
}
// Encode implements httpAgentReplyEncoder
func (xE *haXMLEncoder) Encode(nM *utils.OrderedNavigableMap) (err error) {
var xmlElmnts []*config.XMLElement
if xmlElmnts, err = config.NMAsXMLElements(nM); err != nil {
return
}
if len(xmlElmnts) == 0 {
return
}
var xmlOut []byte
if xmlOut, err = xml.MarshalIndent(xmlElmnts, "", " "); err != nil {
return
}
if _, err = xE.w.Write([]byte(xml.Header)); err != nil {
return
}
_, err = xE.w.Write(xmlOut)
return
}
func newHATextPlainEncoder(w http.ResponseWriter) (xE httpAgentReplyEncoder, err error) {
return &haTextPlainEncoder{w: w}, nil
}
type haTextPlainEncoder struct {
w http.ResponseWriter
}
// Encode implements httpAgentReplyEncoder
func (xE *haTextPlainEncoder) Encode(nM *utils.OrderedNavigableMap) (err error) {
var str, nmPath string
msgFields := make(map[string]string) // work around to NMap issue
for el := nM.GetFirstElement(); el != nil; el = el.Next() {
val := el.Value
var nmIt utils.NMInterface
if nmIt, err = nM.Field(val); err != nil {
return
}
nmItem, isNMItems := nmIt.(*config.NMItem)
if !isNMItems {
return fmt.Errorf("value: %s is not *NMItem", val)
}
nmPath = strings.Join(nmItem.Path, utils.NestingSep)
msgFields[utils.ConcatenatedKey(nmPath, utils.IfaceAsString(nmItem.Data))] = utils.IfaceAsString(nmItem.Data)
}
for key, val := range msgFields {
str += fmt.Sprintf("%s=%s\n", strings.Split(key, utils.InInFieldSep)[0], val)
}
_, err = xE.w.Write([]byte(str))
return
}