Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jun 12, 2024
1 parent 3e465ee commit 833262c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 73 deletions.
16 changes: 8 additions & 8 deletions parser_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ var benchData = []byte(
"Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n")

var setting = Setting{
MessageBegin: func(*Parser) {},
URL: func(*Parser, []byte) {},
Status: func(*Parser, []byte) {},
HeaderField: func(*Parser, []byte) {},
HeaderValue: func(*Parser, []byte) {},
HeadersComplete: func(*Parser) {},
Body: func(*Parser, []byte) {},
MessageComplete: func(*Parser) {},
MessageBegin: func(*Parser, int) {},
URL: func(*Parser, []byte, int) {},
Status: func(*Parser, []byte, int) {},
HeaderField: func(*Parser, []byte, int) {},
HeaderValue: func(*Parser, []byte, int) {},
HeadersComplete: func(*Parser, int) {},
Body: func(*Parser, []byte, int) {},
MessageComplete: func(*Parser, int) {},
}

func Benchmark_Parser(b *testing.B) {
Expand Down
26 changes: 13 additions & 13 deletions parser_both_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,28 @@ func Test_ParserResponse_RequestBody_BOTH(t *testing.T) {
"Cache-Control")

setting := Setting{
MessageBegin: func(*Parser) {
MessageBegin: func(*Parser, int) {
messageBegin = true
},
URL: func(p *Parser, buf []byte) {
URL: func(p *Parser, buf []byte, _ int) {
url = append(url, buf...)
},
Status: func(*Parser, []byte) {
Status: func(*Parser, []byte, int) {
// 响应包才需要用到
},
HeaderField: func(p *Parser, buf []byte) {
HeaderField: func(p *Parser, buf []byte, _ int) {
field = append(field, buf...)
},
HeaderValue: func(p *Parser, buf []byte) {
HeaderValue: func(p *Parser, buf []byte, _ int) {
value = append(value, buf...)
},
HeadersComplete: func(*Parser) {
HeadersComplete: func(*Parser, int) {
headersComplete = true
},
Body: func(p *Parser, buf []byte) {
Body: func(p *Parser, buf []byte, _ int) {
body = append(body, buf...)
},
MessageComplete: func(p *Parser) {
MessageComplete: func(p *Parser, _ int) {
messageComplete = true
},
}
Expand Down Expand Up @@ -120,13 +120,13 @@ func Test_ParserResponse_Chunked_Both(t *testing.T) {
messageBegin := false
rcvBuf := []byte{}
setting := &Setting{
Status: func(p *Parser, buf []byte) {
Status: func(p *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(p *Parser) {
}, MessageBegin: func(p *Parser, _ int) {
messageBegin = true
}, HeaderField: func(p *Parser, buf []byte) {
}, HeaderValue: func(p *Parser, buf []byte) {
}, Body: func(p *Parser, buf []byte) {
}, HeaderField: func(p *Parser, buf []byte, _ int) {
}, HeaderValue: func(p *Parser, buf []byte, _ int) {
}, Body: func(p *Parser, buf []byte, _ int) {
rcvBuf = append(rcvBuf, buf...)
},
}
Expand Down
16 changes: 8 additions & 8 deletions parser_porting_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1938,36 +1938,36 @@ var responses = []message{
}

var settingTest Setting = Setting{
MessageBegin: func(p *Parser) {
MessageBegin: func(p *Parser, _ int) {
m := p.GetUserData().(*message)
m.messageBeginCbCalled = true
m.hType = p.hType
},
URL: func(p *Parser, url []byte) {
URL: func(p *Parser, url []byte, _ int) {
m := p.GetUserData().(*message)
m.requestURL += string(url)
},
Status: func(p *Parser, status []byte) {
Status: func(p *Parser, status []byte, _ int) {
m := p.GetUserData().(*message)
m.responseStatus += string(status)
},
HeaderField: func(p *Parser, headerField []byte) {
HeaderField: func(p *Parser, headerField []byte, _ int) {
m := p.GetUserData().(*message)
m.headers = append(m.headers, [2]string{string(headerField), ""})
},
HeaderValue: func(p *Parser, headerValue []byte) {
HeaderValue: func(p *Parser, headerValue []byte, _ int) {
m := p.GetUserData().(*message)
m.headers[len(m.headers)-1][1] = string(headerValue)
},
HeadersComplete: func(p *Parser) {
HeadersComplete: func(p *Parser, _ int) {
m := p.GetUserData().(*message)
m.headersCompleteCbCalled = true
},
Body: func(p *Parser, body []byte) {
Body: func(p *Parser, body []byte, _ int) {
m := p.GetUserData().(*message)
m.body += string(body)
},
MessageComplete: func(p *Parser) {
MessageComplete: func(p *Parser, _ int) {
m := p.GetUserData().(*message)
m.method = p.Method
m.messageCompleteCbCalled = true
Expand Down
46 changes: 25 additions & 21 deletions parser_req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@ func Test_ParserResponse_RequestLine(t *testing.T) {

url := []byte("/somedir/page.html")
setting := &Setting{
URL: func(p *Parser, buf []byte) {
URL: func(p *Parser, buf []byte, _ int) {
assert.Equal(t, string(url), string(buf))
}, MessageBegin: func(p *Parser) {
}, MessageBegin: func(p *Parser, _ int) {
messageBegin = true
},
}

_, err := p.Execute(setting, rsp)

assert.NoError(t, err)
assert.True(t, messageBegin)
if err != nil {
t.Fatalf("Execute:%v", err)
}
if messageBegin == false {
t.Fatalf("messageBegin is false\n")
}
}

// 测试请求body
Expand Down Expand Up @@ -87,28 +91,28 @@ func Test_ParserResponse_RequestBody(t *testing.T) {
"Cache-Control")

var setting = Setting{
MessageBegin: func(*Parser) {
MessageBegin: func(*Parser, int) {
messageBegin = true
},
URL: func(p *Parser, buf []byte) {
URL: func(p *Parser, buf []byte, _ int) {
url = append(url, buf...)
},
Status: func(*Parser, []byte) {
Status: func(*Parser, []byte, int) {
// 响应包才需要用到
},
HeaderField: func(p *Parser, buf []byte) {
HeaderField: func(p *Parser, buf []byte, _ int) {
field = append(field, buf...)
},
HeaderValue: func(_ *Parser, buf []byte) {
HeaderValue: func(_ *Parser, buf []byte, _ int) {
value = append(value, buf...)
},
HeadersComplete: func(*Parser) {
HeadersComplete: func(*Parser, int) {
headersComplete = true
},
Body: func(_ *Parser, buf []byte) {
Body: func(_ *Parser, buf []byte, _ int) {
body = append(body, buf...)
},
MessageComplete: func(*Parser) {
MessageComplete: func(*Parser, int) {
messageComplete = true
},
}
Expand Down Expand Up @@ -184,28 +188,28 @@ func Test_ParserResponse_RequestBody2(t *testing.T) {
"Cache-Control")

var setting = Setting{
MessageBegin: func(*Parser) {
MessageBegin: func(*Parser, int) {
messageBegin = true
},
URL: func(_ *Parser, buf []byte) {
URL: func(_ *Parser, buf []byte, _ int) {
url = append(url, buf...)
},
Status: func(*Parser, []byte) {
Status: func(*Parser, []byte, int) {
// 响应包才需要用到
},
HeaderField: func(_ *Parser, buf []byte) {
HeaderField: func(_ *Parser, buf []byte, _ int) {
field = append(field, buf...)
},
HeaderValue: func(_ *Parser, buf []byte) {
HeaderValue: func(_ *Parser, buf []byte, _ int) {
value = append(value, buf...)
},
HeadersComplete: func(*Parser) {
HeadersComplete: func(*Parser, int) {
headersComplete = true
},
Body: func(_ *Parser, buf []byte) {
Body: func(_ *Parser, buf []byte, _ int) {
body = append(body, buf...)
},
MessageComplete: func(_ *Parser) {
MessageComplete: func(_ *Parser, _ int) {
messageComplete = true
},
}
Expand Down Expand Up @@ -259,7 +263,7 @@ func Test_ParserRequest_chunked_segment(t *testing.T) {

var body []byte
var setting = Setting{
Body: func(_ *Parser, buf []byte) {
Body: func(_ *Parser, buf []byte, _ int) {
//fmt.Printf("###:%s\n", buf)
body = append(body, buf...)
},
Expand Down
46 changes: 23 additions & 23 deletions parser_res_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ func Test_ParserResponse_StatusLine(t *testing.T) {
messageBegin := false
rsp := []byte("HTTP/1.1 200 OK\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte) {
Status: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(*Parser) {
}, MessageBegin: func(*Parser, int) {
messageBegin = true
},
}, rsp)
Expand Down Expand Up @@ -47,11 +47,11 @@ func Test_ParserResponse_HeaderField(t *testing.T) {
rsp := []byte("HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte) {
Status: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(*Parser) {
}, MessageBegin: func(*Parser, int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte) {
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("Connection"))
},
}, rsp)
Expand All @@ -78,13 +78,13 @@ func Test_ParserResponse_HeaderValue(t *testing.T) {
rsp := []byte("HTTP/1.1 200 OK\r\n" +
"Connection: close\r\n\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte) {
Status: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(*Parser) {
}, MessageBegin: func(*Parser, int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte) {
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, string(buf), "Connection")
}, HeaderValue: func(_ *Parser, buf []byte) {
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, string(buf), "close")
},
}, rsp)
Expand Down Expand Up @@ -115,13 +115,13 @@ func Test_ParserResponse_Multiple_HeaderValue(t *testing.T) {
"Content-Length: 10\r\n" +
"Connection: close\r\n\r\n")
_, err := p.Execute(&Setting{
Status: func(_ *Parser, buf []byte) {
Status: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(*Parser) {
}, MessageBegin: func(*Parser, int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte) {
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
field = append(field, string(buf))
}, HeaderValue: func(_ *Parser, buf []byte) {
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
fieldValue = append(fieldValue, string(buf))
},
}, rsp)
Expand Down Expand Up @@ -155,13 +155,13 @@ func Test_ParserResponse_Content_Length_Body(t *testing.T) {
messageBegin := false
rcvBuf := []byte{}
setting := &Setting{
Status: func(_ *Parser, buf []byte) {
Status: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(_ *Parser) {
}, MessageBegin: func(_ *Parser, _ int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte) {
}, HeaderValue: func(_ *Parser, buf []byte) {
}, Body: func(_ *Parser, buf []byte) {
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
}, Body: func(_ *Parser, buf []byte, _ int) {
rcvBuf = append(rcvBuf, buf...)
},
}
Expand Down Expand Up @@ -205,13 +205,13 @@ func Test_ParserResponse_Chunked(t *testing.T) {
messageBegin := false
rcvBuf := []byte{}
setting := &Setting{
Status: func(_ *Parser, buf []byte) {
Status: func(_ *Parser, buf []byte, _ int) {
assert.Equal(t, buf, []byte("OK"))
}, MessageBegin: func(_ *Parser) {
}, MessageBegin: func(_ *Parser, _ int) {
messageBegin = true
}, HeaderField: func(_ *Parser, buf []byte) {
}, HeaderValue: func(_ *Parser, buf []byte) {
}, Body: func(_ *Parser, buf []byte) {
}, HeaderField: func(_ *Parser, buf []byte, _ int) {
}, HeaderValue: func(_ *Parser, buf []byte, _ int) {
}, Body: func(_ *Parser, buf []byte, _ int) {
rcvBuf = append(rcvBuf, buf...)
},
}
Expand Down

0 comments on commit 833262c

Please sign in to comment.