-
Notifications
You must be signed in to change notification settings - Fork 0
/
headers.go
167 lines (145 loc) · 3.35 KB
/
headers.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
package stompy
import "strings"
type StompHeaders map[string]string
type encoding struct {
To string
From string
}
var encoders = map[string][]encoding{
"1.1": []encoding{
encoding{"\\\\", "\\"},
encoding{"\\n", "\n"},
encoding{"\\c", ":"},
},
"1.2": []encoding{
encoding{"\\\\", "\\"},
encoding{"\\n", "\n"},
encoding{"\\c", ":"},
encoding{"\\r", "\r"},
},
}
type encoder interface {
Encode(val string) string
}
type decoder interface {
Decode(val string) string
}
type encoderDecoder interface {
encoder
decoder
}
type headerEncoderDecoder struct {
version string
}
func (hd headerEncoderDecoder) Decode(val string) string {
encodings := encoders[hd.version]
for _, enc := range encodings {
val = strings.Replace(val, enc.To, enc.From, -1)
}
return val
}
func (hd headerEncoderDecoder) Encode(val string) string {
encodings := encoders[hd.version]
for _, enc := range encodings {
val = strings.Replace(val, enc.From, enc.To, -1)
}
return val
}
type InvalidHeader struct {
message string
}
func (ih *InvalidHeader) Error() string {
return ih.message
}
type headers struct {
version string
}
func (h headers) connectionHeaders(opts ClientOpts) (StompHeaders, error) {
headers := StompHeaders{}
if opts.User != "" && opts.PassCode != "" {
headers["login"] = opts.User
headers["passcode"] = opts.PassCode
}
if "" == opts.Version {
return nil, &InvalidHeader{"missing header accept-version ensure Version set in opts "}
}
headers["accept-version"] = opts.Version
if "" == opts.Vhost {
return nil, &InvalidHeader{"missing header host ensure Vhost set in opts"}
}
headers["host"] = opts.Vhost
return headers, nil
}
func (h headers) sendHeaders(dest string, addedHeaders StompHeaders) StompHeaders {
headers := StompHeaders{}
headers["destination"] = dest
if nil == addedHeaders {
return headers
}
for k, v := range addedHeaders {
headers[k] = v
}
return headers
}
func (h headers) subscribeHeaders(id, dest string, addedHeaders StompHeaders) StompHeaders {
headers := StompHeaders{}
headers["id"] = id
headers["destination"] = dest
if nil == addedHeaders {
return headers
}
for k, v := range addedHeaders {
headers[k] = v
}
return headers
}
func (h headers) transactionHeaders(transactionId string, addedHeaders StompHeaders) StompHeaders {
headers := StompHeaders{}
headers["transaction"] = transactionId
if nil == addedHeaders {
return headers
}
for k, v := range addedHeaders {
headers[k] = v
}
return headers
}
func (h headers) unSubscribeHeaders(subId string, addedHeaders StompHeaders) StompHeaders {
headers := StompHeaders{}
headers["id"] = subId
if nil == addedHeaders {
return headers
}
for k, v := range addedHeaders {
headers[k] = v
}
return headers
}
func (h headers) nackHeaders(messageId, subId, ackId, transId string) StompHeaders {
headers := StompHeaders{}
headers["message-id"] = messageId
if "" != ackId {
headers["id"] = ackId
}
if "" != transId {
headers["transaction"] = transId
}
if "" != subId {
headers["subscription"] = subId
}
return headers
}
func (h headers) ackHeaders(messageId, subId, ackId, transId string) StompHeaders {
headers := StompHeaders{}
headers["message-id"] = messageId
if "" != ackId {
headers["id"] = ackId
}
if "" != transId {
headers["transaction"] = transId
}
if "" != subId {
headers["subscription"] = subId
}
return headers
}