/
rst_stream.go
112 lines (89 loc) · 2.73 KB
/
rst_stream.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
// Copyright 2014 Jamie Hall. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package frames
import (
"bytes"
"fmt"
"io"
"github.com/SlyMarbo/spdy/common"
)
type RST_STREAM struct {
StreamID common.StreamID
Status common.StatusCode
}
func (frame *RST_STREAM) Compress(comp common.Compressor) error {
return nil
}
func (frame *RST_STREAM) Decompress(decomp common.Decompressor) error {
return nil
}
func (frame *RST_STREAM) Error() string {
if err := frame.Status.String(); err != "" {
return err
}
return fmt.Sprintf("[unknown status code %d]", frame.Status)
}
func (frame *RST_STREAM) Name() string {
return "RST_STREAM"
}
func (frame *RST_STREAM) ReadFrom(reader io.Reader) (int64, error) {
c := common.ReadCounter{R: reader}
data, err := common.ReadExactly(&c, 16)
if err != nil {
return c.N, err
}
err = controlFrameCommonProcessing(data[:5], _RST_STREAM, 0)
if err != nil {
return c.N, err
}
// Get and check length.
length := int(common.BytesToUint24(data[5:8]))
if length != 8 {
return c.N, common.IncorrectDataLength(length, 8)
} else if length > common.MAX_FRAME_SIZE-8 {
return c.N, common.FrameTooLarge
}
frame.StreamID = common.StreamID(common.BytesToUint32(data[8:12]))
frame.Status = common.StatusCode(common.BytesToUint32(data[12:16]))
if !frame.StreamID.Valid() {
return c.N, common.StreamIdTooLarge
}
return c.N, nil
}
func (frame *RST_STREAM) String() string {
buf := new(bytes.Buffer)
buf.WriteString("RST_STREAM {\n\t")
buf.WriteString(fmt.Sprintf("Version: 2\n\t"))
buf.WriteString(fmt.Sprintf("Stream ID: %d\n\t", frame.StreamID))
buf.WriteString(fmt.Sprintf("Status code: %s\n}\n", frame.Status))
return buf.String()
}
func (frame *RST_STREAM) WriteTo(writer io.Writer) (int64, error) {
c := common.WriteCounter{W: writer}
if !frame.StreamID.Valid() {
return c.N, common.StreamIdTooLarge
}
out := make([]byte, 16)
out[0] = 128 // Control bit and Version
out[1] = 2 // Version
out[2] = 0 // Type
out[3] = 3 // Type
out[4] = 0 // Flags
out[5] = 0 // Length
out[6] = 0 // Length
out[7] = 8 // Length
out[8] = frame.StreamID.B1() // Stream ID
out[9] = frame.StreamID.B2() // Stream ID
out[10] = frame.StreamID.B3() // Stream ID
out[11] = frame.StreamID.B4() // Stream ID
out[12] = frame.Status.B1() // Status
out[13] = frame.Status.B2() // Status
out[14] = frame.Status.B3() // Status
out[15] = frame.Status.B4() // Status
err := common.WriteExactly(&c, out)
if err != nil {
return c.N, err
}
return c.N, nil
}