-
Notifications
You must be signed in to change notification settings - Fork 173
/
request.go
150 lines (117 loc) · 3.04 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
// Package base contains the primitives of the RTSP protocol.
package base
import (
"bufio"
"fmt"
"strconv"
"github.com/bluenviron/gortsplib/v3/pkg/url"
)
const (
rtspProtocol10 = "RTSP/1.0"
requestMaxMethodLength = 64
requestMaxURLLength = 2048
requestMaxProtocolLength = 64
)
// Method is the method of a RTSP request.
type Method string
// methods.
const (
Announce Method = "ANNOUNCE"
Describe Method = "DESCRIBE"
GetParameter Method = "GET_PARAMETER"
Options Method = "OPTIONS"
Pause Method = "PAUSE"
Play Method = "PLAY"
Record Method = "RECORD"
Setup Method = "SETUP"
SetParameter Method = "SET_PARAMETER"
Teardown Method = "TEARDOWN"
)
// Request is a RTSP request.
type Request struct {
// request method
Method Method
// request url
URL *url.URL
// map of header values
Header Header
// optional body
Body []byte
}
// Unmarshal reads a request.
func (req *Request) Unmarshal(br *bufio.Reader) error {
byts, err := readBytesLimited(br, ' ', requestMaxMethodLength)
if err != nil {
return err
}
req.Method = Method(byts[:len(byts)-1])
if req.Method == "" {
return fmt.Errorf("empty method")
}
byts, err = readBytesLimited(br, ' ', requestMaxURLLength)
if err != nil {
return err
}
rawURL := string(byts[:len(byts)-1])
ur, err := url.Parse(rawURL)
if err != nil {
return fmt.Errorf("invalid URL (%v)", rawURL)
}
req.URL = ur
byts, err = readBytesLimited(br, '\r', requestMaxProtocolLength)
if err != nil {
return err
}
proto := byts[:len(byts)-1]
if string(proto) != rtspProtocol10 {
return fmt.Errorf("expected '%s', got %v", rtspProtocol10, proto)
}
err = readByteEqual(br, '\n')
if err != nil {
return err
}
err = req.Header.unmarshal(br)
if err != nil {
return err
}
err = (*body)(&req.Body).unmarshal(req.Header, br)
if err != nil {
return err
}
return nil
}
// MarshalSize returns the size of a Request.
func (req Request) MarshalSize() int {
n := 0
urStr := req.URL.CloneWithoutCredentials().String()
n += len([]byte(string(req.Method) + " " + urStr + " " + rtspProtocol10 + "\r\n"))
if len(req.Body) != 0 {
req.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(req.Body)), 10)}
}
n += req.Header.marshalSize()
n += body(req.Body).marshalSize()
return n
}
// MarshalTo writes a Request.
func (req Request) MarshalTo(buf []byte) (int, error) {
pos := 0
urStr := req.URL.CloneWithoutCredentials().String()
pos += copy(buf[pos:], []byte(string(req.Method)+" "+urStr+" "+rtspProtocol10+"\r\n"))
if len(req.Body) != 0 {
req.Header["Content-Length"] = HeaderValue{strconv.FormatInt(int64(len(req.Body)), 10)}
}
pos += req.Header.marshalTo(buf[pos:])
pos += body(req.Body).marshalTo(buf[pos:])
return pos, nil
}
// Marshal writes a Request.
func (req Request) Marshal() ([]byte, error) {
buf := make([]byte, req.MarshalSize())
_, err := req.MarshalTo(buf)
return buf, err
}
// String implements fmt.Stringer.
func (req Request) String() string {
buf, _ := req.Marshal()
return string(buf)
}