-
Notifications
You must be signed in to change notification settings - Fork 4
/
state.go
284 lines (245 loc) · 6.07 KB
/
state.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
package http2
import (
"fmt"
. "github.com/Jxck/color"
. "github.com/Jxck/http2/frame"
. "github.com/Jxck/logger"
"log"
)
func init() {
log.SetFlags(log.Lshortfile)
}
// state of stream
type State int
const (
IDLE State = iota
RESERVED_LOCAL
RESERVED_REMOTE
OPEN
HALF_CLOSED_LOCAL
HALF_CLOSED_REMOTE
CLOSED
)
func (s State) String() string {
states := []string{
"IDLE",
"RESERVED_LOCAL",
"RESERVED_REMOTE",
"OPEN",
"HALF_CLOSED_LOCAL",
"HALF_CLOSED_REMOTE",
"CLOSED",
}
return states[int(s)]
}
type Context int
const (
RECV Context = iota
SEND
)
func (c Context) String() string {
return []string{
"RECV",
"SEND",
}[int(c)]
}
// Stream States
// +--------+
// send PP | | recv PP
// ,--------| idle |--------.
// / | | \
// v +--------+ v
// +----------+ | +----------+
// | | | send H / | |
// ,------| reserved | | recv H | reserved |------.
// | | (local) | | | (remote) | |
// | +----------+ v +----------+ |
// | | +--------+ | |
// | | recv ES | | send ES | |
// | send H | ,-------| open |-------. | recv H |
// | | / | | \ | |
// | v v +--------+ v v |
// | +----------+ | +----------+ |
// | | half | | | half | |
// | | closed | | send R / | closed | |
// | | (remote) | | recv R | (local) | |
// | +----------+ | +----------+ |
// | | | | |
// | | send ES / | recv ES / | |
// | | send R / v send R / | |
// | | recv R +--------+ recv R | |
// | send R / `----------->| |<-----------' send R / |
// | recv R | closed | recv R |
// `----------------------->| |<----------------------'
// +--------+
//
// send: endpoint sends this frame
// recv: endpoint receives this frame
//
// H: HEADERS frame (with implied CONTINUATIONs)
// PP: PUSH_PROMISE frame (with implied CONTINUATIONs)
// ES: END_STREAM flag
// R: RST_STREAM frame
func (stream *Stream) ChangeState(frame Frame, context Context) (err error) {
header := frame.Header()
types := header.Type
flags := header.Flags
state := stream.State
Trace("change state(%v) with %v frame type(%v)", state, context, types)
if types == SettingsFrameType ||
types == GoAwayFrameType {
// not a type for consider
return nil
}
switch stream.State {
case IDLE:
// H
if types == HeadersFrameType {
stream.changeState(OPEN)
// ES
if flags&END_STREAM == END_STREAM {
if context == RECV {
stream.changeState(HALF_CLOSED_REMOTE)
} else {
stream.changeState(HALF_CLOSED_LOCAL)
}
}
return
}
// PP
if types == PushPromiseFrameType {
if context == RECV {
stream.changeState(RESERVED_REMOTE)
} else {
stream.changeState(RESERVED_LOCAL)
}
return
}
// P
if types == PriorityFrameType {
// accepted
return
}
case OPEN:
// ES
if flags&END_STREAM == END_STREAM {
if context == RECV {
stream.changeState(HALF_CLOSED_REMOTE)
} else {
stream.changeState(HALF_CLOSED_LOCAL)
}
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
// every type of frame accepted
return
case RESERVED_LOCAL:
// H
if types == HeadersFrameType && context == SEND {
stream.changeState(HALF_CLOSED_REMOTE)
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
case RESERVED_REMOTE:
// H
if types == HeadersFrameType && context == RECV {
stream.changeState(HALF_CLOSED_LOCAL)
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
case HALF_CLOSED_LOCAL:
if context == SEND {
if types == WindowUpdateFrameType ||
types == PriorityFrameType {
// valid frame
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
}
if context == RECV {
// ES
if flags&END_STREAM == END_STREAM {
stream.changeState(CLOSED)
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
// recv any type of frames are valid
return
}
case HALF_CLOSED_REMOTE:
if context == SEND {
// ES
if flags&END_STREAM == END_STREAM {
stream.changeState(CLOSED)
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
// send any type of frames are valid
return
}
if context == RECV {
if types == WindowUpdateFrameType ||
types == PriorityFrameType {
// valid frame
return
}
// R
if types == RstStreamFrameType {
stream.changeState(CLOSED)
return
}
msg := fmt.Sprintf("invalid frame type %v at %v state", types, state)
Error(Red(msg))
return &H2Error{STREAM_CLOSED, msg}
}
case CLOSED:
if context == SEND {
if types == PriorityFrameType {
// valid frame
return
}
}
if context == RECV {
if types == WindowUpdateFrameType ||
types == PriorityFrameType ||
types == RstStreamFrameType {
// valid frame
return
}
msg := fmt.Sprintf("invalid frame type %v at %v state", types, state)
Error(Red(msg))
return &H2Error{STREAM_CLOSED, msg}
}
}
msg := fmt.Sprintf("invalid frame type %v at %v state", types, state)
Error(Red(msg))
return &H2Error{PROTOCOL_ERROR, msg}
}
func (stream *Stream) changeState(state State) {
Info("change stream (%d) state (%s -> %s)", stream.ID, stream.State, Pink(state.String()))
stream.State = state
}