This repository has been archived by the owner on May 1, 2020. It is now read-only.
/
serverHttp.go
88 lines (75 loc) · 2.01 KB
/
serverHttp.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
package webwire
import (
"net/http"
)
// ServeHTTP will make the server listen for incoming HTTP requests
// eventually trying to upgrade them to WebSocket connections
func (srv *server) ServeHTTP(
resp http.ResponseWriter,
req *http.Request,
) {
// Reject incoming connections during shutdown, pretend the server is temporarily unavailable
srv.opsLock.Lock()
if srv.shutdown {
srv.opsLock.Unlock()
http.Error(resp, "Server shutting down", http.StatusServiceUnavailable)
return
}
srv.opsLock.Unlock()
switch req.Method {
case "OPTIONS":
srv.impl.OnOptions(resp)
return
case "WEBWIRE":
srv.handleMetadata(resp)
return
}
if !srv.impl.BeforeUpgrade(resp, req) {
return
}
// Establish connection
conn, err := srv.connUpgrader.Upgrade(resp, req)
if err != nil {
srv.errorLog.Print("Upgrade failed:", err)
return
}
defer conn.Close()
// Register connected client
newClient := newClientAgent(conn, req.Header.Get("User-Agent"), srv)
srv.clientsLock.Lock()
srv.clients = append(srv.clients, newClient)
srv.clientsLock.Unlock()
// Call hook on successful connection
srv.impl.OnClientConnected(newClient)
for {
// Await message
message, err := conn.Read()
if err != nil {
if err.IsAbnormalCloseErr() {
srv.warnLog.Printf("Abnormal closure error: %s", err)
}
newClient.unlink()
srv.impl.OnClientDisconnected(newClient)
return
}
// Parse message
var msgObject Message
msgTypeParsed, parserErr := msgObject.Parse(message)
if !msgTypeParsed {
// Couldn't determine message type, drop message
continue
} else if parserErr != nil {
// Couldn't parse message, protocol error
srv.warnLog.Println("Parser error:", parserErr)
// Respond with an error but don't break the connection
// because protocol errors are not critical errors
srv.failMsg(newClient, &msgObject, ProtocolErr{})
continue
}
// Handle message
if err := srv.handleMessage(newClient, &msgObject); err != nil {
srv.errorLog.Printf("CRITICAL FAILURE: %s", err)
break
}
}
}