/
server.go
132 lines (107 loc) · 2.59 KB
/
server.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
package xmlrpc
import (
"context"
"net"
"net/http"
"net/url"
"sync"
"time"
)
// ErrorRes is a special response that sends status code 400.
// in case of errors, the C++ implementation replies with
// <methodResponse><fault><value>..., a structure which would require additional parsing.
type ErrorRes struct{}
// Server is a XML-RPC server.
type Server struct {
ctx context.Context
ctxCancel func()
wg sync.WaitGroup
ln net.Listener
hs *http.Server
handler func(*RequestRaw) interface{}
setHandler chan func(*RequestRaw) interface{}
done chan struct{}
}
// NewServer allocates a server.
func NewServer(address string, writeTimeout time.Duration) (*Server, error) {
// net.Listen and http.Server are splitted since the latter
// does not allow to use 0 as port
ln, err := net.Listen("tcp", address)
if err != nil {
return nil, err
}
ctx, ctxCancel := context.WithCancel(context.Background())
s := &Server{
ctx: ctx,
ctxCancel: ctxCancel,
ln: ln,
setHandler: make(chan func(*RequestRaw) interface{}),
done: make(chan struct{}),
}
s.hs = &http.Server{
Handler: http.HandlerFunc(s.handleRequest),
WriteTimeout: writeTimeout,
}
go s.run()
return s, nil
}
// Close closes all the server resources.
func (s *Server) Close() error {
s.ctxCancel()
<-s.done
return nil
}
// URL returns the server URL.
func (s *Server) URL(ip net.IP, zone string) string {
return (&url.URL{
Scheme: "http",
Host: (&net.TCPAddr{
IP: ip,
Port: s.ln.Addr().(*net.TCPAddr).Port,
Zone: zone,
}).String(),
}).String()
}
func (s *Server) run() {
defer close(s.done)
select {
case handler := <-s.setHandler:
s.handler = handler
case <-s.ctx.Done():
s.ln.Close()
return
}
go s.hs.Serve(s.ln)
<-s.ctx.Done()
s.hs.Shutdown(context.Background())
s.ln.Close() // in case Shutdown() is called before Serve()
}
func (s *Server) handleRequest(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/RPC2" && req.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
return
}
if req.Method != http.MethodPost {
w.WriteHeader(http.StatusNotFound)
return
}
raw, err := requestDecodeRaw(req.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
res := s.handler(raw)
if _, ok := res.(ErrorRes); ok {
w.WriteHeader(http.StatusBadRequest)
return
}
responseEncode(w, res)
}
// Serve starts serving requests and waits until the server is closed.
func (s *Server) Serve(handler func(*RequestRaw) interface{}) {
select {
case s.setHandler <- handler:
case <-s.ctx.Done():
}
s.wg.Wait()
}