2 // Copyright © 2011-2019 Guy M. Allard
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
26 Encode a string per STOMP 1.1+ specifications.
28 func encode(s
string) string {
30 for _
, tr
:= range codecValues
{
31 if strings
.Index(r
, tr
.decoded
) >= 0 {
32 r
= strings
.Replace(r
, tr
.decoded
, tr
.encoded
, -1)
39 Decode a string per STOMP 1.1+ specifications.
41 func decode(s
string) string {
43 for _
, tr
:= range codecValues
{
44 if strings
.Index(r
, tr
.encoded
) >= 0 {
45 r
= strings
.Replace(r
, tr
.encoded
, tr
.decoded
, -1)
52 A network helper. Read from the wire until a 0x00 byte is encountered.
54 func readUntilNul(c
*Connection
) ([]uint8, error
) {
56 b
, e
:= c
.rdr
.ReadBytes(0)
57 if c
.checkReadError(e
) != nil {
69 A network helper. Read a full message body with a known length that is
70 > 0. Then read the trailing 'null' byte expected for STOMP frames.
72 func readBody(c
*Connection
, l
int) ([]uint8, error
) {
75 n
, e
:= io
.ReadFull(c
.rdr
, b
)
76 if n
< l
&& n
!= 0 { // Short read, e is ErrUnexpectedEOF
77 c
.log("SHORT READ", n
, l
, e
)
80 if c
.checkReadError(e
) != nil { // Other erors
84 _
, _
= c
.rdr
.ReadByte() // trailing NUL
85 if c
.checkReadError(e
) != nil { // Other erors
92 Common Header Validation.
94 func checkHeaders(h Headers
, p
string) error
{
99 if e
:= h
.Validate(); e
!= nil {
102 // Empty key / value check
103 for i
:= 0; i
< len(h
); i
+= 2 {
107 if p
== SPL_10
&& h
[i
+1] == "" {
113 _
, e
:= h
.ValidateUTF8()
122 Internal function used by heartbeat initialization.
124 func max(a
, b
int64) int64 {
132 Debug helper. Get properly formatted destination.
134 func dumpmd(md MessageData
) {
135 fmt
.Printf("Command: %s\n", md
.Message
.Command
)
136 fmt
.Println("Headers:")
137 for i
:= 0; i
< len(md
.Message
.Headers
); i
+= 2 {
138 fmt
.Printf("key:%s\t\tvalue:%s\n",
139 md
.Message
.Headers
[i
], md
.Message
.Headers
[i
+1])
141 fmt
.Printf("Body: %s\n", string(md
.Message
.Body
))
143 fmt
.Printf("Error: %s\n", md
.Error
.Error())
145 fmt
.Println("Error: nil")