-
Notifications
You must be signed in to change notification settings - Fork 17
/
lphandler.go
168 lines (145 loc) · 4.94 KB
/
lphandler.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
/******************************************************************************
*
* Copyright (C) 2014 Tinode, All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see <
*
* This code is available under licenses for commercial use.
*
* File : lphandler.go
* Author : Gene Sokolov
* Created : 18-May-2014
*
******************************************************************************
*
* Description :
*
* Handler of long polling clients (see also wshandler for web sockets)
*
*****************************************************************************/
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
func lp_writePkt(wrt http.ResponseWriter, pkt *ServerComMessage) error {
data, _ := json.Marshal(pkt)
_, err := wrt.Write(data)
return err
}
func (sess *Session) writeOnce() {
// Next call may change wrt, save it here
wrt := sess.wrt
notifier, _ := wrt.(http.CloseNotifier)
closed := notifier.CloseNotify()
select {
case msg, ok := <-sess.send:
if !ok {
log.Println("writeOnce: reading from a closed channel")
} else if _, err := wrt.Write(msg); err != nil {
log.Println("sess.writeOnce: " + err.Error())
sess.wrt = nil
}
case <-closed:
log.Println("conn.writeOnce: connection closed by peer")
sess.wrt = nil
case <-sess.stop:
sess.wrt = nil
case topic := <-sess.detach:
delete(sess.subs, topic)
case <-time.After(pingPeriod):
// just write an empty packet on timeout
if _, err := wrt.Write([]byte{}); err != nil {
log.Println("sess.writeOnce: timout/" + err.Error())
}
}
}
func (sess *Session) readOnce(req *http.Request) {
if raw, err := ioutil.ReadAll(req.Body); err == nil {
sess.dispatch(raw)
} else {
log.Println("longPoll: " + err.Error())
}
}
// serveLongPoll handles long poll connections when WebSocket is not available
// Connection could be without sid or with sid:
// - if sid is empty, create session, expect a login in the same request, respond and close
// - if sid is not empty and there is an initialized session, payload is optional
// - if no payload, perform long poll
// - if payload exists, process it and close
// - if sid is not empty but there is no session, report an error
func serveLongPoll(wrt http.ResponseWriter, req *http.Request) {
var appid uint32
// Use lowest common denominator - this is a legacy handler after all
wrt.Header().Set("Content-Type", "text/plain")
enc := json.NewEncoder(wrt)
if appid, _ = checkApiKey(getApiKey(req)); appid == 0 {
wrt.WriteHeader(http.StatusForbidden)
enc.Encode(
&ServerComMessage{Ctrl: &MsgServerCtrl{
Code: http.StatusForbidden,
Text: "Valid API key is required"}})
return
}
// TODO(gene): should it be configurable?
// Currently any domain is allowed to get data from the chat server
wrt.Header().Set("Access-Control-Allow-Origin", "*")
// Ensure the response is not cached
if req.ProtoAtLeast(1, 1) {
wrt.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1
} else {
wrt.Header().Set("Pragma", "no-cache") // HTTP 1.0
}
wrt.Header().Set("Expires", "0") // Proxies
// TODO(gene): respond differently to valious HTTP methods
log.Printf("HTTP %s %s?%s from '%s' %d bytes", req.Method,
req.URL.Path, req.URL.RawQuery, req.RemoteAddr, req.ContentLength)
// Get session id
sid := req.FormValue("sid")
if sid == "" {
sess := globals.sessionStore.Create(wrt, appid)
log.Println("longPoll: new session created, sid=", sess.sid)
wrt.WriteHeader(http.StatusCreated)
enc.Encode(
&ServerComMessage{Ctrl: &MsgServerCtrl{
Code: http.StatusCreated,
Text: http.StatusText(http.StatusCreated),
Params: map[string]interface{}{"sid": sess.sid, "ver": VERSION, "build": buildstamp},
Timestamp: time.Now().UTC().Round(time.Millisecond)}})
// Any payload is ignored
return
}
sess := globals.sessionStore.Get(sid)
if sess == nil {
log.Println("longPoll: invalid or expired session id ", sid)
wrt.WriteHeader(http.StatusForbidden)
enc.Encode(
&ServerComMessage{Ctrl: &MsgServerCtrl{
Code: http.StatusForbidden,
Text: "Invalid or expired session id"}})
return
}
sess.wrt = wrt
sess.remoteAddr = req.RemoteAddr
if req.ContentLength > 0 {
// Got payload. Process it and return right away
sess.readOnce(req)
return
}
// Wait for data, write it to the connection or timeout
sess.writeOnce()
}