-
Notifications
You must be signed in to change notification settings - Fork 173
/
body.go
54 lines (43 loc) · 858 Bytes
/
body.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
package base
import (
"bufio"
"fmt"
"io"
"strconv"
)
const (
rtspMaxContentLength = 128 * 1024
)
type body []byte
func (b *body) unmarshal(header Header, rb *bufio.Reader) error {
cls, ok := header["Content-Length"]
if !ok || len(cls) != 1 {
*b = nil
return nil
}
cl, err := strconv.ParseUint(cls[0], 10, 64)
if err != nil {
return fmt.Errorf("invalid Content-Length")
}
if cl > rtspMaxContentLength {
return fmt.Errorf("Content-Length exceeds %d (it's %d)",
rtspMaxContentLength, cl)
}
*b = make([]byte, cl)
n, err := io.ReadFull(rb, *b)
if err != nil && n != len(*b) {
return err
}
return nil
}
func (b body) marshalSize() int {
return len(b)
}
func (b body) marshalTo(buf []byte) int {
return copy(buf, b)
}
func (b body) marshal() []byte {
buf := make([]byte, b.marshalSize())
b.marshalTo(buf)
return buf
}