-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforward.go
115 lines (101 loc) · 2.84 KB
/
forward.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
// Copyright (c) 2014 SameGoal LLC. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wc
import (
"fmt"
"net/http"
"strconv"
"strings"
)
func newSessionHandler(sw *sessionWrapper, reqRequest *reqRegister) {
debug("wc: %s forward channel (new session)", sw.SID())
defer func() {
reqRequest.done <- struct{}{}
}()
createMsg := []byte(jsonArray(
[]interface{}{"c", sw.SID(), sm.HostPrefix(), 8},
))
if err := sw.BackChannelAdd(createMsg); err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Unable to add create message to back channel",
http.StatusInternalServerError)
return
}
if err := sw.BackChannelNewSessionMessages(); err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Unable to add messages for new session",
http.StatusInternalServerError)
return
}
msgs, err := sw.BackChannelPeek()
if err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Unable to get messages",
http.StatusInternalServerError)
return
}
p := newPadder(reqRequest.w, reqRequest.r)
p.writeMessages(msgs)
}
func fcHandler(sw *sessionWrapper, reqRequest *reqRegister) {
debug("wc: %s forward channel", sw.SID())
defer func() {
reqRequest.done <- struct{}{}
}()
if !maybeACKBackChannel(sw, reqRequest.w, reqRequest.r, true) {
// HTTP error codes written directly in maybeACKBackChannel().
return
}
count, err := strconv.Atoi(reqRequest.r.PostFormValue("count"))
if err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Unable to parse count", 400)
return
}
msgs := []*Message{}
if count > 0 {
offset, err := strconv.Atoi(reqRequest.r.PostFormValue("ofs"))
if err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Unable to parse ofs", 400)
return
}
for i := 0; i < count; i++ {
req := fmt.Sprintf("req%d", i)
jsonMap := make(map[string]interface{})
for key, value := range reqRequest.r.PostForm {
keyParts := strings.SplitN(key, "_", 2)
if len(keyParts) < 2 || keyParts[0] != req {
continue
}
jsonMap[keyParts[1]] = value[0]
}
effectiveID := offset + i
if effectiveID <= sw.si.ForwardChannelAID {
// skip incoming messages which have already been received
continue
}
msg := &Message{ID: offset + i, Body: []byte(jsonObject(jsonMap))}
msgs = append(msgs, msg)
debug("wc: %s new forward channel message %d %s", sw.Session.SID(),
msg.ID, msg.Body)
}
}
if len(msgs) > 0 {
err = sw.ForwardChannel(msgs)
if err != nil {
sm.Error(reqRequest.r, err)
http.Error(reqRequest.w, "Incoming message error",
http.StatusInternalServerError)
return
}
}
reply := []interface{}{
sw.bc != nil,
sw.si.BackChannelAID,
sw.backChannelBytes,
}
p := newPadder(reqRequest.w, reqRequest.r)
p.write(jsonArray(reply))
}