|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "encoding/xml" |
| 7 | + "errors" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + |
| 11 | + mxj "github.com/clbanning/mxj/v2" |
| 12 | + typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3" |
| 13 | + ep "github.com/wrossmorrow/envoy-extproc-sdk-go" |
| 14 | +) |
| 15 | + |
| 16 | +type convRequestProcessor struct { |
| 17 | + opts *ep.ProcessingOptions |
| 18 | +} |
| 19 | + |
| 20 | +func (s *convRequestProcessor) GetName() string { |
| 21 | + return "xml-json-conv" |
| 22 | +} |
| 23 | + |
| 24 | +func (s *convRequestProcessor) GetOptions() *ep.ProcessingOptions { |
| 25 | + return s.opts |
| 26 | +} |
| 27 | + |
| 28 | +func (s *convRequestProcessor) ProcessRequestHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error { |
| 29 | + return ctx.ContinueRequest() |
| 30 | +} |
| 31 | + |
| 32 | +const ( |
| 33 | + kXML = "xml" |
| 34 | + kJSON = "json" |
| 35 | +) |
| 36 | + |
| 37 | +// detectFormat attempts to determine whether the data is JSON or XML. |
| 38 | +func detectFormat(data []byte) (string, error) { |
| 39 | + if json.Valid(data) { |
| 40 | + return kJSON, nil |
| 41 | + } |
| 42 | + |
| 43 | + if isValidXML(data) { |
| 44 | + return kXML, nil |
| 45 | + } |
| 46 | + |
| 47 | + return "", errors.New("unknown format") |
| 48 | +} |
| 49 | + |
| 50 | +func isValidXML(input []byte) bool { |
| 51 | + decoder := xml.NewDecoder(bytes.NewReader(input)) |
| 52 | + for { |
| 53 | + err := decoder.Decode(new(any)) |
| 54 | + if err != nil { |
| 55 | + return err == io.EOF |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (s *convRequestProcessor) ProcessRequestBody(ctx *ep.RequestContext, body []byte) error { |
| 61 | + cancel := func(code int32) error { |
| 62 | + return ctx.CancelRequest(code, map[string]ep.HeaderValue{}, typev3.StatusCode_name[code]) |
| 63 | + } |
| 64 | + |
| 65 | + from, err := detectFormat(body) |
| 66 | + if err != nil { |
| 67 | + log.Println(err) |
| 68 | + return cancel(400) |
| 69 | + } |
| 70 | + |
| 71 | + var data []byte |
| 72 | + |
| 73 | + switch from { |
| 74 | + case kJSON: |
| 75 | + data, err = jsonToXML(body) |
| 76 | + case kXML: |
| 77 | + data, err = xmlToJSON(body) |
| 78 | + default: |
| 79 | + panic("never happen") |
| 80 | + } |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + log.Printf("convert data format is failed: %v", err) |
| 84 | + return cancel(400) |
| 85 | + } |
| 86 | + |
| 87 | + return ctx.CancelRequest(200, map[string]ep.HeaderValue{}, string(data)) |
| 88 | +} |
| 89 | + |
| 90 | +func (s *convRequestProcessor) ProcessRequestTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error { |
| 91 | + return ctx.ContinueRequest() |
| 92 | +} |
| 93 | + |
| 94 | +func (s *convRequestProcessor) ProcessResponseHeaders(ctx *ep.RequestContext, headers ep.AllHeaders) error { |
| 95 | + return ctx.ContinueRequest() |
| 96 | +} |
| 97 | + |
| 98 | +func (s *convRequestProcessor) ProcessResponseBody(ctx *ep.RequestContext, body []byte) error { |
| 99 | + return ctx.ContinueRequest() |
| 100 | +} |
| 101 | + |
| 102 | +func (s *convRequestProcessor) ProcessResponseTrailers(ctx *ep.RequestContext, trailers ep.AllHeaders) error { |
| 103 | + return ctx.ContinueRequest() |
| 104 | +} |
| 105 | + |
| 106 | +func (s *convRequestProcessor) Init(opts *ep.ProcessingOptions, nonFlagArgs []string) error { |
| 107 | + s.opts = opts |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +func (s *convRequestProcessor) Finish() {} |
| 112 | + |
| 113 | +// jsonToXML converts a JSON byte array to XML byte array. |
| 114 | +func jsonToXML(data []byte) ([]byte, error) { |
| 115 | + mxj.XMLEscapeChars(true) |
| 116 | + m, err := mxj.NewMapJsonReader(bytes.NewReader(data)) |
| 117 | + if err != nil { |
| 118 | + return nil, err |
| 119 | + } |
| 120 | + |
| 121 | + buf := &bytes.Buffer{} |
| 122 | + err = m.XmlIndentWriter(buf, "", "\t") |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + return buf.Bytes(), nil |
| 127 | +} |
| 128 | + |
| 129 | +// xmlToJSON converts XML data to JSON. |
| 130 | +func xmlToJSON(data []byte) ([]byte, error) { |
| 131 | + mxj.XMLEscapeChars(true) |
| 132 | + m, err := mxj.NewMapXmlReader(bytes.NewReader(data)) |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + buf := &bytes.Buffer{} |
| 138 | + err = m.JsonIndentWriter(buf, "", "\t") |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + return buf.Bytes(), nil |
| 143 | +} |
0 commit comments