-
Notifications
You must be signed in to change notification settings - Fork 3
/
xmlrpc.go
123 lines (113 loc) · 2.74 KB
/
xmlrpc.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
// Copyright 2017, 2022 Tamás Gulácsi
//
// SPDX-License-Identifier: Apache-2.0
package grpcer
import (
"context"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"time"
"unicode"
"unicode/utf8"
"github.com/klauspost/compress/gzhttp"
"github.com/mitchellh/mapstructure"
"github.com/tgulacsi/go-xmlrpc"
)
type XMLRPCHandler struct {
Client
*slog.Logger
Timeout time.Duration
}
func (h XMLRPCHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gzhttp.GzipHandler(http.HandlerFunc(h.serveHTTP)).ServeHTTP(w, r)
}
func (h XMLRPCHandler) serveHTTP(w http.ResponseWriter, r *http.Request) {
logger := h.Logger
name, params, err := xmlrpc.Unmarshal(r.Body)
logger.Info("unmarshal", "name", name, "params", params, "error", err)
if err != nil {
http.Error(w, fmt.Sprintf("ERROR unmarshaling: %v", err), http.StatusBadRequest)
return
}
inp := h.Input(name)
if inp == nil {
http.Error(w, fmt.Sprintf("No unmarshaler for %q.", name), http.StatusNotFound)
return
}
if len(params) != 1 {
http.Error(w, fmt.Sprintf("Wanted 1 struct param, got %d.", len(params)), http.StatusBadRequest)
return
}
m, ok := params[0].(map[string]interface{})
if !ok {
http.Error(w, fmt.Sprintf("Wanted struct, got %T", params[0]), http.StatusBadRequest)
return
}
// mapstruct
for k, v := range m {
if s, ok := v.(string); ok && s == "" {
delete(m, k)
continue
}
f, _ := utf8.DecodeRune([]byte(k))
if unicode.IsLower(f) {
m[CamelCase(k)] = v
}
}
if err := mapstructure.WeakDecode(m, inp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logger.Info("decoded", "inp", inp)
ctx := r.Context()
if u, p, ok := r.BasicAuth(); ok {
ctx = WithBasicAuth(ctx, u, p)
}
if _, ok := ctx.Deadline(); !ok {
timeout := h.Timeout
if timeout == 0 {
timeout = DefaultTimeout
}
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
}
recv, err := h.Call(name, ctx, inp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
part, err := recv.Recv()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
parts := []interface{}{nil}[:0]
for {
parts = append(parts, part)
if part, err = recv.Recv(); err != nil {
if !errors.Is(err, io.EOF) {
logger.Error("recv", "error", err)
parts = parts[:1]
parts[0] = xmlrpc.Fault{Code: 111, Message: err.Error()}
}
break
}
}
w.Header().Set("Content-Type", "application/xml")
w.WriteHeader(200)
if len(parts) == 1 {
err = xmlrpc.Marshal(w, "", parts[0])
} else {
err = xmlrpc.Marshal(w, "", parts)
}
if err != nil {
logger.Error("marshal", "error", err)
}
}
// vim: set fileencoding=utf-8 noet: