-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathhttp_handler.go
192 lines (155 loc) · 3.81 KB
/
http_handler.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
package butlerd
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
"github.com/sourcegraph/jsonrpc2"
)
type httpHandler struct {
jrh jsonrpc2.Handler
feedStreams map[string]*httpFeedStream
feedStreamsMutex sync.Mutex
callStreams map[string]*httpCallStream
callStreamsMutex sync.Mutex
handlerFunc http.HandlerFunc
secret string
}
var _ http.Handler = (*httpHandler)(nil)
func (hh *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if hh.handlerFunc == nil {
hh.handlerFunc = H(hh.handle)
}
if hh.feedStreams == nil {
hh.feedStreams = make(map[string]*httpFeedStream)
}
if hh.callStreams == nil {
hh.callStreams = make(map[string]*httpCallStream)
}
hh.handlerFunc(w, r)
}
func (hh *httpHandler) getFeedStream(cid string) (*httpFeedStream, bool) {
hh.feedStreamsMutex.Lock()
defer hh.feedStreamsMutex.Unlock()
s, ok := hh.feedStreams[cid]
return s, ok
}
func (hh *httpHandler) putFeedStream(cid string, s *httpFeedStream) {
hh.feedStreamsMutex.Lock()
defer hh.feedStreamsMutex.Unlock()
hh.feedStreams[cid] = s
}
func (hh *httpHandler) removeFeedStream(cid string) {
hh.feedStreamsMutex.Lock()
defer hh.feedStreamsMutex.Unlock()
delete(hh.feedStreams, cid)
}
func (hh *httpHandler) getCallStream(cid string) (*httpCallStream, bool) {
hh.callStreamsMutex.Lock()
defer hh.callStreamsMutex.Unlock()
s, ok := hh.callStreams[cid]
return s, ok
}
func (hh *httpHandler) putCallStream(cid string, s *httpCallStream) {
hh.callStreamsMutex.Lock()
defer hh.callStreamsMutex.Unlock()
hh.callStreams[cid] = s
}
func (hh *httpHandler) removeCallStream(cid string) {
hh.callStreamsMutex.Lock()
defer hh.callStreamsMutex.Unlock()
delete(hh.callStreams, cid)
}
func (hh *httpHandler) handle(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
switch r.Method {
case "GET":
{
if r.URL.Path != "/feed" {
return HTTPError(404, "Not found")
}
secret := r.URL.Query().Get("secret")
if secret != hh.secret {
return HTTPError(401, "Missing or invalid authorization")
}
cid := r.URL.Query().Get("cid")
if cid == "" {
return HTTPError(400, "Missing cid parameter")
}
s := &httpFeedStream{
r: r,
w: w.(responseWriter),
cid: cid,
hh: hh,
}
return s.Wait(ctx)
}
case "POST":
{
secret := r.Header.Get("x-secret")
if secret != hh.secret {
return HTTPError(401, "Missing or invalid authorization error")
}
cid := r.Header.Get("x-cid")
path := strings.TrimLeft(r.URL.Path, "/")
pathTokens := strings.Split(path, "/")
if len(pathTokens) == 0 {
return HTTPError(404, "Not found")
}
switch pathTokens[0] {
case "call":
if len(pathTokens) < 2 {
return HTTPError(404, "Method missing")
}
method := pathTokens[1]
s := &httpCallStream{
r: r,
w: w,
cid: cid,
hh: hh,
jrh: hh.jrh,
method: method,
}
return s.Wait(ctx)
case "cancel":
cs, err := hh.assertCallStream(r)
if err != nil {
return err
}
cs.cancelGracefully()
w.WriteHeader(204)
return nil
case "reply":
cs, err := hh.assertCallStream(r)
if err != nil {
return err
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
cs.readCh <- body
w.WriteHeader(204)
return nil
default:
return HTTPError(404, "Not found")
}
}
default:
log.Printf("Called with invalid method: %s", r.Method)
return HTTPError(400, "Expected GET or POST")
}
}
func (hh *httpHandler) assertCallStream(r *http.Request) (*httpCallStream, error) {
cid := r.Header.Get("x-cid")
if cid == "" {
return nil, HTTPError(400, "Missing cid")
}
cs, ok := hh.getCallStream(cid)
if !ok {
return nil, HTTPError(404, fmt.Sprintf("No in-flight request with cid '%s'", cid))
}
return cs, nil
}