forked from mtfelian/golang-socketio
/
polling.go
266 lines (232 loc) · 8.01 KB
/
polling.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package transport
import (
"errors"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
"fmt"
"github.com/ik9999/golang-socketio/logging"
"github.com/ik9999/golang-socketio/protocol"
)
const (
PlDefaultPingInterval = 30 * time.Second
PlDefaultPingTimeout = 60 * time.Second
PlDefaultReceiveTimeout = 60 * time.Second
PlDefaultSendTimeout = 60 * time.Second
StopMessage = "stop"
UpgradedMessage = "upgrade"
noError = "0"
hijackingNotSupported = "webserver doesn't support hijacking"
)
var (
errGetMessageTimeout = errors.New("timeout waiting for the message")
errReceivedConnectionClose = errors.New("received connection close")
errWriteMessageTimeout = errors.New("timeout waiting for write")
)
// withLength returns s as a message with length
func withLength(m string) string { return fmt.Sprintf("%d:%s", len(m), m) }
// PollingTransportParams represents XHR polling transport params
type PollingTransportParams struct {
Headers http.Header
}
// PollingConnection represents a XHR polling connection
type PollingConnection struct {
Transport *PollingTransport
eventsInC chan string
eventsOutC chan string
errors chan string
sessionID string
}
// GetMessage waits for incoming message from the connection
func (polling *PollingConnection) GetMessage() (string, error) {
select {
case <-time.After(polling.Transport.ReceiveTimeout):
logging.Log().Debug("PollingConnection.GetMessage() timed out")
return "", errGetMessageTimeout
case m := <-polling.eventsInC:
logging.Log().Debug("PollingConnection.GetMessage() received:", m)
if m == protocol.MessageClose {
logging.Log().Debug("PollingConnection.GetMessage() received connection close")
return "", errReceivedConnectionClose
}
return m, nil
}
}
// WriteMessage to the connection
func (polling *PollingConnection) WriteMessage(message string) error {
logging.Log().Debug("PollingConnection.WriteMessage() fired with:", message)
polling.eventsOutC <- message
logging.Log().Debug("PollingConnection.WriteMessage() written to eventsOutC:", message)
select {
case <-time.After(polling.Transport.SendTimeout):
return errWriteMessageTimeout
case errString := <-polling.errors:
if errString != noError {
logging.Log().Debug("PollingConnection.WriteMessage() failed to write with err:", errString)
return errors.New(errString)
}
}
return nil
}
// Close the polling connection and delete session
func (polling *PollingConnection) Close() error {
logging.Log().Debug("PollingConnection.Close() fired for session:", polling.sessionID)
err := polling.WriteMessage(protocol.MessageBlank)
polling.Transport.sessions.Delete(polling.sessionID)
return err
}
// PingParams returns a connection ping params
func (polling *PollingConnection) PingParams() (time.Duration, time.Duration) {
return polling.Transport.PingInterval, polling.Transport.PingTimeout
}
// sessions describes sessions needed for identifying polling connections with socket.io connections
type sessions struct {
sync.Mutex
m map[string]*PollingConnection
}
// Set sets sessionID to the given connection
func (s *sessions) Set(sessionID string, conn *PollingConnection) {
logging.Log().Debug("sessions.Set() fired with:", sessionID)
s.Lock()
defer s.Unlock()
s.m[sessionID] = conn
}
// Delete the sessionID
func (s *sessions) Delete(sessionID string) {
logging.Log().Debug("sessions.Delete() fired with:", sessionID)
s.Lock()
defer s.Unlock()
delete(s.m, sessionID)
}
// Get returns polling connection if it exists, otherwise returns nil
func (s *sessions) Get(sessionID string) *PollingConnection {
s.Lock()
defer s.Unlock()
return s.m[sessionID]
}
// PollingTransport represens the XHR polling transport params
type PollingTransport struct {
PingInterval time.Duration
PingTimeout time.Duration
ReceiveTimeout time.Duration
SendTimeout time.Duration
Headers http.Header
sessions sessions
}
// Connect for the polling transport is a placeholder
func (t *PollingTransport) Connect(url string) (Connection, error) {
return nil, nil
}
// HandleConnection returns a pointer to a new Connection
func (t *PollingTransport) HandleConnection(w http.ResponseWriter, r *http.Request) (Connection, error) {
return &PollingConnection{
Transport: t,
eventsInC: make(chan string),
eventsOutC: make(chan string),
errors: make(chan string),
}, nil
}
// SetSid to the given sessionID and connection
func (t *PollingTransport) SetSid(sessionID string, connection Connection) {
t.sessions.Set(sessionID, connection.(*PollingConnection))
connection.(*PollingConnection).sessionID = sessionID
}
// Serve is for receiving messages from client, simple decoding also here
func (t *PollingTransport) Serve(w http.ResponseWriter, r *http.Request) {
sessionId := r.URL.Query().Get("sid")
conn := t.sessions.Get(sessionId)
if conn == nil {
return
}
switch r.Method {
case http.MethodGet:
logging.Log().Debug("PollingTransport.Serve() is serving GET request")
conn.PollingWriter(w, r)
case http.MethodPost:
bodyBytes, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
logging.Log().Debug("PollingTransport.Serve() error ioutil.ReadAll():", err)
return
}
bodyString := string(bodyBytes)
logging.Log().Debug("PollingTransport.Serve() POST bodyString before split:", bodyString)
index := strings.Index(bodyString, ":")
body := bodyString[index+1:]
setHeaders(w)
logging.Log().Debug("PollingTransport.Serve() POST body:", body)
w.Write([]byte("ok"))
logging.Log().Debug("PollingTransport.Serve() written POST response")
conn.eventsInC <- body
logging.Log().Debug("PollingTransport.Serve() sent to eventsInC")
}
}
// DefaultPollingTransport returns PollingTransport with default params
func DefaultPollingTransport() *PollingTransport {
return &PollingTransport{
PingInterval: PlDefaultPingInterval,
PingTimeout: PlDefaultPingTimeout,
ReceiveTimeout: PlDefaultReceiveTimeout,
SendTimeout: PlDefaultSendTimeout,
sessions: sessions{
Mutex: sync.Mutex{},
m: map[string]*PollingConnection{},
},
Headers: nil,
}
}
// PollingWriter for writing polling answer
func (polling *PollingConnection) PollingWriter(w http.ResponseWriter, r *http.Request) {
setHeaders(w)
select {
case <-time.After(polling.Transport.SendTimeout):
logging.Log().Debug("PollingTransport.PollingWriter() timed out")
polling.errors <- noError
case message := <-polling.eventsOutC:
logging.Log().Debug("PollingTransport.PollingWriter() prepares to write message:", message)
message = withLength(message)
if message == withLength(protocol.MessageBlank) {
logging.Log().Debug("PollingTransport.PollingWriter() writing 1:6")
hj, ok := w.(http.Hijacker)
if !ok {
http.Error(w, hijackingNotSupported, http.StatusInternalServerError)
return
}
conn, buffer, err := hj.Hijack()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer conn.Close()
buffer.WriteString("HTTP/1.1 200 OK\r\n" +
"Cache-Control: no-cache, private\r\n" +
"Content-Length: 3\r\n" +
"Date: Mon, 24 Nov 2016 10:21:21 GMT\r\n\r\n")
buffer.WriteString(withLength(protocol.MessageBlank))
buffer.Flush()
logging.Log().Debug("PollingTransport.PollingWriter() hijack returns")
polling.errors <- noError
polling.eventsInC <- StopMessage
} else {
_, err := w.Write([]byte(message))
logging.Log().Debug("PollingTransport.PollingWriter() written message:", message)
if err != nil {
logging.Log().Debug("PollingTransport.PollingWriter() failed to write message with err:", err)
polling.errors <- err.Error()
return
}
polling.errors <- noError
}
}
}
// setHeaders into w
func setHeaders(w http.ResponseWriter) {
// We are going to return JSON no matter what:
w.Header().Set("Content-Type", "application/json")
// Don't cache response:
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1
w.Header().Set("Pragma", "no-cache") // HTTP 1.0
w.Header().Set("Expires", "0") // Proxies
}