-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsetting.go
180 lines (168 loc) · 4.8 KB
/
setting.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
// Copyright 2021 guonaihong. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package httparser
// Setting 查阅#6 看设计变更原因
type Setting struct {
// 解析开始
MessageBegin func(*Parser, int)
// url 回调函数, 只有在request包才会回调
// 解析一个包时,URL回调可能会多次调用
URL func(*Parser, []byte, int)
// 状态短语
// 解析一个包时, Status回调可能会多次调用
Status func(*Parser, []byte, int)
// http field 回调函数
HeaderField func(*Parser, []byte, int)
// http value 回调函数
HeaderValue func(*Parser, []byte, int)
// http 解析完成之后的回调函数
HeadersComplete func(*Parser, int)
// body的回调函数
Body func(*Parser, []byte, int)
// 所有消息成功解析
MessageComplete func(*Parser, int)
}
// ReqOrRsp 请求还是响应
type ReqOrRsp uint8
const (
// REQUEST 解析请求包
REQUEST ReqOrRsp = iota + 1
// RESPONSE 解析响应包
RESPONSE
// BOTH 让解析器根据报文自己判断
BOTH
)
type state uint8
func (s state) String() string {
return stateTab[s]
}
const (
// 错误的状态
dead state = iota + 1
// request状态
startReq
// reqMethod 后面的SP
reqMethodAfterSP
// 请求URL
reqURL
// 请求URL后面的SP
reqURLAfterSP
//
reqHTTPVersion
// HTTP-Version中的major
reqHTTPVersionMajor
// HTTP-Version中的.
reqHTTPVersionDot
// HTTP-Version中的minor
reqHTTPVersionMinor
// request-line \r的位置
reqRequestLineAlomstDone
// response状态
startRsp
// HTTP
rspHTTP
// response 版本号数字
rspHTTPVersionNum
// response 版本号后面的空格
rspHTTPVersionNumAfterSP
// response 状态吗
rspStatusCode
// statuscode后面的sp符号
rspStatusCodeAfterSP
// response状态短语
rspStatus
// 状态短语后面的SP符号
rspStatusAfterSP
// request or response状态,这里让解析器自己选择
startReqOrRsp
// http header解析结束
headersDone
// 解析http field
headerField
// 进入http header分隔符号
headerValueDiscardWs
// 进入http value
headerValue
// 刚开始进入http value后面的OWS
headerValueStartOWS
// 快要离开http value后面的OWS
headerValueOWS
// 进入http body
httpBody
// 开始进入到chunked 数字解析
chunkedSizeStart
// 进入到chunked 数字
chunkedSize
// chunked size结束
chunkedSizeAlmostDone
// chunked ext
chunkedExt
// chunked data
chunkedData
// chunked 检查是否真的结束
chunkedDataAlmostDone
// chunked data结束
chunkedDataDone
// 快要结束
messageAlmostDone
// 一直读到socket eof
bodyIdentityEOF
// 解析结束
messageDone
)
// debug使用
var stateTab = []string{
startReq: "startReq",
reqMethodAfterSP: "reqMethodAfterSP",
reqURL: "reqURL",
reqURLAfterSP: "reqURLAfterSP",
reqHTTPVersion: "reqHTTPVersion",
reqHTTPVersionMajor: "reqHTTPVersionMajor",
reqHTTPVersionDot: "reqHTTPVersionDot",
reqHTTPVersionMinor: "reqHTTPVersionMinor",
reqRequestLineAlomstDone: "reqRequestLineAlomstDone",
startRsp: "startRsp",
rspHTTP: "rspHTTP",
rspHTTPVersionNum: "rspHTTPVersionNum",
rspHTTPVersionNumAfterSP: "rspHTTPVersionNumAfterSP",
rspStatusCode: "rspStatusCode",
rspStatusCodeAfterSP: "rspStatusCodeAfterSP",
rspStatus: "rspStatus",
rspStatusAfterSP: "rspStatusCodeAfterSP",
startReqOrRsp: "startReqOrRsp",
headersDone: "headersDone",
headerField: "headerField",
headerValueDiscardWs: "headerValueDiscardWs",
headerValue: "headerValue",
headerValueStartOWS: "headerValueStartOWS",
headerValueOWS: "headerValueOWS",
httpBody: "httpBody",
chunkedSizeStart: "chunkedSizeStart",
chunkedSize: "chunkedSize",
chunkedSizeAlmostDone: "chunkedSizeAlmostDone",
chunkedExt: "chunkedExt",
chunkedData: "chunkedData",
chunkedDataAlmostDone: "chunkedDataAlmostDone",
chunkedDataDone: "chunkedDataDone",
messageAlmostDone: "messageAlmostDone",
bodyIdentityEOF: "bodyIdentityEOF",
messageDone: "messageDone",
}
type headerState uint8
const (
hGeneral headerState = iota
hContentLength
hTransferEncoding
hConnection
)