-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.go
183 lines (151 loc) · 3.81 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package wrpssp
import (
"fmt"
"strconv"
"strings"
"github.com/xmidt-org/wrp-go/v3"
)
const (
// These are the string literals found in the messages.
stream_id = "stream-id"
stream_packet_number = "stream-packet-number"
stream_estimated_length = "stream-estimated-total-length"
stream_final_packet = "stream-final-packet"
)
// headers provides an easy way to consistently set, get, and validate the headers.
type headers struct {
id string
currentPacketNumber int64
totalLength uint64
finalPacket string
}
func (h headers) set(msg *wrp.Message) {
if msg.Headers == nil {
msg.Headers = make([]string, 0, 1)
}
clearExisting(msg)
setS(msg, stream_id, h.id)
setI(msg, stream_packet_number, h.currentPacketNumber)
if h.totalLength > 0 {
setU(msg, stream_estimated_length, h.totalLength)
}
if h.finalPacket != "" {
setS(msg, stream_final_packet, h.finalPacket)
}
}
func get(msg *wrp.Message) (headers, error) {
h := headers{
// default the currentPacketNumber to -1 to indicate that it is not set
currentPacketNumber: -1,
}
for _, header := range msg.Headers {
key, value, found := strings.Cut(header, ":")
if !found {
continue
}
key = strings.TrimSpace(key)
key = strings.ToLower(key)
value = strings.TrimSpace(value)
switch key {
case stream_id:
h.id = value
case stream_packet_number:
i, err := getI(value)
if err != nil {
return headers{}, err
}
h.currentPacketNumber = i
case stream_estimated_length:
i, err := getU(value)
if err != nil {
return headers{}, err
}
h.totalLength = i
case stream_final_packet:
// we don't care about the value, just the presence of the header
if value == "" {
value = "EOF"
}
h.finalPacket = value
}
}
if err := h.isValid(); err != nil {
return headers{}, err
}
return h, nil
}
func isSSP(msg *wrp.Message) bool {
if msg.MessageType() == wrp.SimpleEventMessageType {
for _, header := range msg.Headers {
header = strings.TrimSpace(header)
header = strings.ToLower(header)
if strings.HasPrefix(header, stream_id) {
return true
}
}
}
return false
}
func (h headers) isValid() error {
if h.id == "" {
return fmt.Errorf("%w: id must not be empty", ErrInvalidInput)
}
if h.currentPacketNumber < 0 {
return fmt.Errorf("%w: currentPacketNumber must be set", ErrInvalidInput)
}
return nil
}
func getU(s string) (uint64, error) {
if s == "0" {
return 0, nil
}
s = strings.TrimLeft(s, "0")
return strconv.ParseUint(s, 10, 64)
}
// getI is a helper function to parse an int64 from a string. It trims leading zeros.
// If the string is "0", it returns 0. Negative numbers are not supported because
// the stream_packet_number is unsigned.
func getI(s string) (int64, error) {
if s == "0" {
return 0, nil
}
s = strings.TrimLeft(s, "0")
return strconv.ParseInt(s, 10, 64)
}
func setI(msg *wrp.Message, key string, value int64) {
s := strconv.FormatInt(value, 10)
setS(msg, key, s)
}
func setU(msg *wrp.Message, key string, value uint64) {
s := strconv.FormatUint(value, 10)
setS(msg, key, s)
}
func setS(msg *wrp.Message, key, value string) {
msg.Headers = append(msg.Headers, key+": "+value)
}
var headerList = []string{
stream_id,
stream_packet_number,
stream_estimated_length,
stream_final_packet,
}
func clearExisting(msg *wrp.Message) {
replacements := make([]string, 0, len(msg.Headers))
for _, header := range msg.Headers {
s := strings.TrimSpace(header)
s = strings.ToLower(s)
var found bool
for _, key := range headerList {
if strings.HasPrefix(s, key) {
found = true
break
}
}
if !found {
replacements = append(replacements, header)
}
}
msg.Headers = replacements
}