-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathheader.go
141 lines (118 loc) · 2.72 KB
/
header.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
package websocket
import (
"encoding/binary"
"fmt"
"io"
"math"
"golang.org/x/xerrors"
)
// First byte contains fin, rsv1, rsv2, rsv3.
// Second byte contains mask flag and payload len.
// Next 8 bytes are the maximum extended payload length.
// Last 4 bytes are the mask key.
// https://tools.ietf.org/html/rfc6455#section-5.2
const maxHeaderSize = 1 + 1 + 8 + 4
// header represents a WebSocket frame header.
// See https://tools.ietf.org/html/rfc6455#section-5.2
type header struct {
fin bool
rsv1 bool
rsv2 bool
rsv3 bool
opcode opcode
payloadLength int64
masked bool
maskKey [4]byte
}
// bytes returns the bytes of the header.
// See https://tools.ietf.org/html/rfc6455#section-5.2
func marshalHeader(h header) []byte {
b := make([]byte, 2, maxHeaderSize)
if h.fin {
b[0] |= 1 << 7
}
if h.rsv1 {
b[0] |= 1 << 6
}
if h.rsv2 {
b[0] |= 1 << 5
}
if h.rsv3 {
b[0] |= 1 << 4
}
b[0] |= byte(h.opcode)
switch {
case h.payloadLength < 0:
panic(fmt.Sprintf("websocket: invalid header: negative length: %v", h.payloadLength))
case h.payloadLength <= 125:
b[1] = byte(h.payloadLength)
case h.payloadLength <= math.MaxUint16:
b[1] = 126
b = b[:len(b)+2]
binary.BigEndian.PutUint16(b[len(b)-2:], uint16(h.payloadLength))
default:
b[1] = 127
b = b[:len(b)+8]
binary.BigEndian.PutUint64(b[len(b)-8:], uint64(h.payloadLength))
}
if h.masked {
b[1] |= 1 << 7
b = b[:len(b)+4]
copy(b[len(b)-4:], h.maskKey[:])
}
return b
}
// readHeader reads a header from the reader.
// See https://tools.ietf.org/html/rfc6455#section-5.2
func readHeader(r io.Reader) (header, error) {
// We read the first two bytes directly so that we know
// exactly how long the header is.
b := make([]byte, 2, maxHeaderSize-2)
_, err := io.ReadFull(r, b)
if err != nil {
return header{}, err
}
var h header
h.fin = b[0]&(1<<7) != 0
h.rsv1 = b[0]&(1<<6) != 0
h.rsv2 = b[0]&(1<<5) != 0
h.rsv3 = b[0]&(1<<4) != 0
h.opcode = opcode(b[0] & 0xf)
var extra int
h.masked = b[1]&(1<<7) != 0
if h.masked {
extra += 4
}
payloadLength := b[1] &^ (1 << 7)
switch {
case payloadLength < 126:
h.payloadLength = int64(payloadLength)
case payloadLength == 126:
extra += 2
case payloadLength == 127:
extra += 8
}
if extra == 0 {
return h, nil
}
b = b[:extra]
_, err = io.ReadFull(r, b)
if err != nil {
return header{}, err
}
switch {
case payloadLength == 126:
h.payloadLength = int64(binary.BigEndian.Uint16(b))
b = b[2:]
case payloadLength == 127:
h.payloadLength = int64(binary.BigEndian.Uint64(b))
if h.payloadLength < 0 {
return header{}, xerrors.Errorf("header with negative payload length: %v", h.payloadLength)
}
b = b[8:]
}
if h.masked {
copy(h.maskKey[:], b)
}
return h, nil
}