forked from googollee/go-engine.io
/
server.go
148 lines (130 loc) · 3.6 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package engineio
import (
"bytes"
"crypto/md5"
"encoding/base64"
"fmt"
"net/http"
"time"
)
type config struct {
PingTimeout time.Duration
PingInterval time.Duration
AllowRequest func(*http.Request) error
AllowUpgrades bool
Cookie string
}
// Server is the server of engine.io.
type Server struct {
config config
socketChan chan Conn
sessions *sessions
transports transportsType
}
// NewServer returns the server suppported given transports. If transports is nil, server will use ["polling", "webosocket"] as default.
func NewServer(transports []string) (*Server, error) {
t, err := newTransportsType(transports)
if err != nil {
return nil, err
}
return &Server{
config: config{
PingTimeout: 60000 * time.Millisecond,
PingInterval: 25000 * time.Millisecond,
AllowRequest: func(*http.Request) error { return nil },
AllowUpgrades: true,
Cookie: "io",
},
socketChan: make(chan Conn),
sessions: newSessions(),
transports: t,
}, nil
}
// SetPingTimeout sets the timeout of ping. When time out, server will close connection. Default is 60s.
func (s *Server) SetPingTimeout(t time.Duration) {
s.config.PingTimeout = t
}
// SetPingInterval sets the interval of ping. Default is 25s.
func (s *Server) SetPingInterval(t time.Duration) {
s.config.PingInterval = t
}
// SetAllowRequest sets the middleware function when establish connection. If it return non-nil, connection won't be established. Default will allow all request.
func (s *Server) SetAllowRequest(f func(*http.Request) error) {
s.config.AllowRequest = f
}
// SetAllowUpgrades sets whether server allows transport upgrade. Default is true.
func (s *Server) SetAllowUpgrades(allow bool) {
s.config.AllowUpgrades = allow
}
// SetCookie sets the name of cookie which used by engine.io. Default is "io".
func (s *Server) SetCookie(prefix string) {
s.config.Cookie = prefix
}
// ServeHTTP handles http request.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
cookies := r.Cookies()
sid := r.URL.Query().Get("sid")
if sid == "" {
if err := s.config.AllowRequest(r); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
transportName := r.URL.Query().Get("transport")
transportCreater := s.transports.GetCreater(transportName)
if transportCreater == nil {
http.Error(w, "invalid transport", http.StatusBadRequest)
return
}
transport, err := transportCreater(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
sid = s.newId(r)
conn, err := newConn(sid, s, transport, r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
s.sessions.Set(sid, conn)
cookies = append(cookies, &http.Cookie{
Name: s.config.Cookie,
Value: sid,
})
go func() {
err := conn.onOpen()
if err != nil {
conn.Close()
defer s.onClose(conn)
return
}
s.socketChan <- conn
}()
}
conn := s.sessions.Get(sid)
if conn == nil {
http.Error(w, "invalid sid", http.StatusBadRequest)
return
}
for _, c := range cookies {
w.Header().Set("Set-Cookie", c.String())
}
conn.serveHTTP(w, r)
}
// Accept returns Conn when client connect to server.
func (s *Server) Accept() (Conn, error) {
return <-s.socketChan, nil
}
func (s *Server) onClose(so *conn) {
s.sessions.Remove(so.Id())
}
func (s *Server) newId(r *http.Request) string {
hash := fmt.Sprintf("%s %s", r.RemoteAddr, time.Now())
buf := bytes.NewBuffer(nil)
sum := md5.Sum([]byte(hash))
encoder := base64.NewEncoder(base64.URLEncoding, buf)
encoder.Write(sum[:])
encoder.Close()
return buf.String()[:20]
}