forked from h2oai/wave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
136 lines (121 loc) · 3.65 KB
/
app.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
// Copyright 2020 H2O.ai, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wave
import (
"bytes"
"fmt"
"net/http"
)
// AppMode represents app modes.
type AppMode int
const (
unicastMode AppMode = iota
multicastMode
broadcastMode
)
// App represents an app
type App struct {
broker *Broker
client *http.Client
mode AppMode // mode
route string // route
addr string // upstream address http://host:port
keyID string // access key ID
keySecret string // access key secret
}
func toAppMode(mode string) AppMode {
switch mode {
case "broadcast":
return broadcastMode
case "multicast":
return multicastMode
}
return unicastMode
}
func newApp(broker *Broker, mode, route, addr, keyID, keySecret string) *App {
return &App{
broker,
&http.Client{}, // TODO tune keep-alive and idle timeout
toAppMode(mode),
route,
addr,
keyID,
keySecret,
}
}
func (app *App) disconnect(clientID string) error {
req, err := http.NewRequest("POST", app.addr+"/disconnect", nil)
if err != nil {
return fmt.Errorf("failed creating request: %v", err)
}
req.SetBasicAuth(app.keyID, app.keySecret)
req.Header.Set("Wave-Client-ID", clientID)
resp, err := app.client.Do(req)
if err != nil {
return fmt.Errorf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("request failed: %s", http.StatusText(resp.StatusCode))
}
if _, err := readWithLimit(resp.Body, 0); err != nil { // apps always return empty plain-text responses.
return fmt.Errorf("failed reading response: %v", err)
}
return nil
}
func (app *App) forward(clientID string, session *Session, data []byte) {
if err := app.send(clientID, session, data); err != nil {
echo(Log{"t": "app", "route": app.route, "host": app.addr, "error": err.Error()})
if !app.broker.keepAppLive {
app.broker.dropApp(app.route)
}
}
}
func (app *App) send(clientID string, session *Session, data []byte) error {
req, err := http.NewRequest("POST", app.addr, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("failed creating request: %v", err)
}
req.SetBasicAuth(app.keyID, app.keySecret)
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if len(clientID) > 0 {
req.Header.Set("Wave-Client-ID", clientID)
}
req.Header.Set("Wave-Subject-ID", session.subject)
req.Header.Set("Wave-Username", session.username)
if session.subject != anon {
req.Header.Set("Wave-Session-ID", session.id)
// TODO: Figure out how can the token be nil.
if session.token != nil {
req.Header.Set("Wave-Access-Token", session.token.AccessToken)
req.Header.Set("Wave-Refresh-Token", session.token.RefreshToken)
} else {
if app.broker.debug {
echo(Log{"t": "app_send", "error": "session token is nil"})
}
}
}
resp, err := app.client.Do(req)
if err != nil {
return fmt.Errorf("request failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return fmt.Errorf("request failed: %s", http.StatusText(resp.StatusCode))
}
if _, err := readWithLimit(resp.Body, 0); err != nil { // apps always return empty plain-text responses.
return fmt.Errorf("failed reading response: %v", err)
}
return nil
}