-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
356 lines (291 loc) · 6.39 KB
/
request.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package rtsp
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
)
type MethodEnum int
const (
UnknownMethod MethodEnum = iota - 1
OptionsMethod
DescribeMethod
AnnounceMethod
SetupMethod
PlayMethod
PauseMethod
TeardownMethod
GetParameterMethod
SetParameterMethod
RecordMethod
)
type Request struct {
method string
url string
version string
lines HeaderLines
content []byte
}
type IRequest interface {
Url() string
MethodStr() string
GetLine(key string) string
SetLine(key, value string)
String() string
CSeq() int
Session() string
ContentType() string
GetContent() []byte
SetContent(content string)
}
func UnmarshalRequest(buf []byte) (*Request, int, error) {
headerEndOffset := bytes.Index(buf, []byte("\r\n\r\n"))
if headerEndOffset == -1 {
return nil, 0, nil
}
endOffset := headerEndOffset + 4
contentLength := 0
req := &Request{
lines: make(HeaderLines),
}
lines := bytes.Split(buf[:headerEndOffset], []byte("\r\n"))
if len(lines) < 2 {
return nil, endOffset, errors.New("read lines error, invalid packet")
}
// parse first line
methodLine := lines[0]
methodLineParts := bytes.Split(methodLine, []byte(" "))
if len(methodLineParts) != 3 {
return nil, endOffset, fmt.Errorf("invalid method line: %s", string(methodLine))
}
req.method = strings.ToLower(string(methodLineParts[0]))
req.url = strings.ToLower(string(methodLineParts[1]))
req.version = strings.ToLower(string(methodLineParts[2]))
// parse other lines
for _, line := range lines[1:] {
if len(line) == 0 {
continue
}
idx := bytes.Index(line, []byte(":"))
if idx == -1 {
continue
}
key := strings.ToLower(string(line[:idx]))
value := string(line[idx+1:])
value = strings.TrimSpace(value)
req.lines[key] = value
if key == "content-length" {
var err error
contentLength, err = strconv.Atoi(value)
if err != nil {
return nil, endOffset, err
}
}
}
if contentLength > len(buf[endOffset:]) {
return nil, 0, nil
}
req.content = make([]byte, 0)
if contentLength > 0 {
req.content = append(req.content, buf[endOffset:endOffset+contentLength]...)
endOffset += contentLength
}
return req, endOffset, nil
}
func (lines HeaderLines) String() string {
s := ""
for k, v := range lines {
s += k + ": " + v + "\r\n"
}
return s
}
func (req *Request) Method() MethodEnum {
switch req.method {
case "options":
return OptionsMethod
case "describe":
return DescribeMethod
case "announce":
return AnnounceMethod
case "setup":
return SetupMethod
case "play":
return PlayMethod
case "pause":
return PauseMethod
case "teardown":
return TeardownMethod
case "getparameter":
return GetParameterMethod
case "setparameter":
return SetParameterMethod
case "record":
return RecordMethod
default:
return UnknownMethod
}
}
func (req *Request) Url() string {
return req.url
}
func (req *Request) MethodStr() string {
return req.method
}
func (req *Request) GetLine(key string) string {
return req.lines[key]
}
func (req *Request) SetLine(key, value string) {
req.lines[key] = value
}
func (req *Request) String() string {
ret := req.method + " " + req.url + " " + req.version + "\r\n" +
req.lines.String() +
"\r\n"
if len(req.content) > 0 {
ret = ret + string(req.content)
}
return ret
}
func (req *Request) CSeq() int {
cseqLine := req.lines["cseq"]
if cseqLine == "" {
return -1
}
cseq, err := strconv.Atoi(cseqLine)
if err != nil {
return -1
}
return cseq
}
func (req *Request) Session() string {
return req.lines["session"]
}
func (req *Request) ContentType() string {
return req.lines["content-type"]
}
func (req *Request) SetContent(content string) {
req.content = []byte(content)
req.lines["Content-Length"] = strconv.Itoa(len(content))
}
func (req *Request) GetContent() []byte {
return req.content
}
func (req *Request) Option() *OptionsRequest {
return &OptionsRequest{
IRequest: req,
}
}
func (req *Request) Describe() *DescribeRequest {
return &DescribeRequest{
IRequest: req,
}
}
func (req *Request) Announce() *AnnounceRequest {
return &AnnounceRequest{
IRequest: req,
}
}
func (req *Request) Setup() *SetupRequest {
return &SetupRequest{
IRequest: req,
}
}
func (req *Request) Play() *PlayRequest {
return &PlayRequest{
IRequest: req,
}
}
func (req *Request) Pause() *PauseRequest {
return &PauseRequest{
IRequest: req,
}
}
func (req *Request) Teardown() *TeardownRequest {
return &TeardownRequest{
IRequest: req,
}
}
func (req *Request) GetParameter() *GetParameterRequest {
return &GetParameterRequest{
IRequest: req,
}
}
func (req *Request) SetParameter() *SetParameterRequest {
return &SetParameterRequest{
IRequest: req,
}
}
func (req *Request) Record() *RecordRequest {
return &RecordRequest{
IRequest: req,
}
}
// OptionsRequest is a RTSP OPTIONS request
type OptionsRequest struct {
IRequest
}
func (req *OptionsRequest) Require() string {
return req.GetLine("require")
}
func (req *OptionsRequest) ProxyRequire() string {
return req.GetLine("proxy-require")
}
// DescribeRequest is a RTSP DESCRIBE request
type DescribeRequest struct {
IRequest
}
func (req *DescribeRequest) Accept() string {
return req.GetLine("accept")
}
func (req *DescribeRequest) SetSdp(sdp string) {
req.SetLine("content-type", "application/sdp")
req.SetContent(sdp)
}
// AnnounceRequest is a RTSP ANNOUNCE request
type AnnounceRequest struct {
IRequest
}
// SetupRequest is a RTSP SETUP request
type SetupRequest struct {
IRequest
}
func (req *SetupRequest) TransportString() string {
return req.GetLine("transport")
}
func (req *SetupRequest) Transport() (*Transport, error) {
return UnmarshalTransport(req.TransportString())
}
func (req *SetupRequest) SetTransport(transport *Transport) {
req.SetLine("transport", transport.String())
}
// PlayRequest is a RTSP PLAY request
type PlayRequest struct {
IRequest
}
func (req *PlayRequest) Range() string {
return req.GetLine("range")
}
// PauseRequest is a RTSP PAUSE request
type PauseRequest struct {
IRequest
}
func (req *PauseRequest) Range() string {
return req.GetLine("range")
}
// TeardownRequest is a RTSP TEARDOWN request
type TeardownRequest struct {
IRequest
}
// GetParameterRequest is a RTSP GET_PARAMETER request
type GetParameterRequest struct {
IRequest
}
// SetParameterRequest is a RTSP SET_PARAMETER request
type SetParameterRequest struct {
IRequest
}
// RecordRequest is a RTSP RECORD request
type RecordRequest struct {
IRequest
}