-
Notifications
You must be signed in to change notification settings - Fork 9
/
request_or_response.go
61 lines (56 loc) · 1.33 KB
/
request_or_response.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
package main
import (
"fmt"
"github.com/antlabs/httparser"
)
func main() {
var data = []byte(
"HTTP/1.1 200 OK\r\n" +
"Content-Type: text/plain\r\n" +
"Transfer-Encoding: chunked\r\n" +
"\r\n" +
"7\r\n" +
"Mozilla\r\n" +
"9\r\n" +
"Developer\r\n" +
"7\r\n" +
"Network\r\n" +
"0\r\n" +
"\r\n")
var setting = httparser.Setting{
MessageBegin: func(*httparser.Parser) {
//解析器开始工作
fmt.Printf("begin\n")
},
URL: func(_ *httparser.Parser, buf []byte) {
//url数据
fmt.Printf("url->%s\n", buf)
},
Status: func(*httparser.Parser, []byte) {
// 响应包才需要用到
},
HeaderField: func(_ *httparser.Parser, buf []byte) {
// http header field
fmt.Printf("header field:%s\n", buf)
},
HeaderValue: func(_ *httparser.Parser, buf []byte) {
// http header value
fmt.Printf("header value:%s\n", buf)
},
HeadersComplete: func(_ *httparser.Parser) {
// http header解析结束
fmt.Printf("header complete\n")
},
Body: func(_ *httparser.Parser, buf []byte) {
fmt.Printf("%s", buf)
// Content-Length 或者chunked数据包
},
MessageComplete: func(_ *httparser.Parser) {
// 消息解析结束
fmt.Printf("\n")
},
}
p := httparser.New(httparser.BOTH)
success, err := p.Execute(&setting, data)
fmt.Printf("success:%d, err:%v\n", success, err)
}