Skip to content

Commit

Permalink
修改setting的形参
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Jun 12, 2024
1 parent 1f1e308 commit 88b706a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
46 changes: 23 additions & 23 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ func (p *Parser) ReadyUpgradeData() bool {
return p.callMessageComplete && p.Upgrade
}

func (p *Parser) complete(s *Setting) {
func (p *Parser) complete(s *Setting, pos int) {
p.callMessageComplete = true
if s.MessageComplete != nil {
s.MessageComplete(p)
s.MessageComplete(p, pos)
}
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
if len(buf) == 0 {
switch currState {
case bodyIdentityEOF:
p.complete(setting)
p.complete(setting, i)
return 0, nil
default:
return 0, nil
Expand All @@ -189,7 +189,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
for ; i < len(buf); i++ {
c = buf[i]

//fmt.Printf("---->debug state(%s):(%s)method(%#v)\n", currState, buf[i:], p.Method)
// fmt.Printf("---->debug state(%s):(%s)method(%#v)\n", currState, buf[i:], p.Method)
reExec:
switch currState {
case startReqOrRsp:
Expand All @@ -199,7 +199,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)

if c == 'H' {
if setting.MessageBegin != nil {
setting.MessageBegin(p)
setting.MessageBegin(p, i)
}
currState = rspHTTP
continue
Expand All @@ -218,7 +218,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
}

if setting.MessageBegin != nil {
setting.MessageBegin(p)
setting.MessageBegin(p, i)
}

buf2 := buf[i : i+pos]
Expand Down Expand Up @@ -309,7 +309,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
if c == ' ' || c == '\t' {
currState = reqURLAfterSP
if setting.URL != nil {
setting.URL(p, buf[urlStartIndex:i])
setting.URL(p, buf[urlStartIndex:i], i)
}
}

Expand Down Expand Up @@ -346,7 +346,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
}

if setting.MessageBegin != nil {
setting.MessageBegin(p)
setting.MessageBegin(p, i)
}

currState = rspHTTP
Expand Down Expand Up @@ -410,15 +410,15 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)

if c == '\r' {
if setting.Status != nil {
setting.Status(p, buf[reasonPhraseIndex:i])
setting.Status(p, buf[reasonPhraseIndex:i], i)
}
currState = rspStatusAfterSP
continue
}

if c == '\n' {
if setting.Status != nil {
setting.Status(p, buf[reasonPhraseIndex:i])
setting.Status(p, buf[reasonPhraseIndex:i], i)
}
currState = headerField
}
Expand Down Expand Up @@ -450,7 +450,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)

field := buf[i : i+pos]
if setting.HeaderField != nil {
setting.HeaderField(p, field)
setting.HeaderField(p, field, i+pos)
}

field = bytes.TrimRight(field, " ")
Expand Down Expand Up @@ -500,7 +500,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)

hValue := buf[i : i+end]
if setting.HeaderValue != nil {
setting.HeaderValue(p, hValue)
setting.HeaderValue(p, hValue, i+end)
}

err2 := Split(hValue, bytesCommaSep, func(hValue []byte) error {
Expand Down Expand Up @@ -576,28 +576,28 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)

//fmt.Printf("p.Upgrade:%t, hasBody:%t, hasTrailing:%t\n", p.Upgrade, hasBody, p.hasTrailing)
if p.Upgrade && !hasBody || p.Method == CONNECT {
p.complete(setting)
p.complete(setting, i)

p.currState = p.newMessage()
return i + 1, nil
}

if p.hasTrailing {
p.complete(setting)
p.complete(setting, i)

currState = messageDone
goto reExec
}

if setting.HeadersComplete != nil {
setting.HeadersComplete(p)
setting.HeadersComplete(p, i)
}

// TODO hasContentLength, hasTransferEncoding同时为true
if p.hasContentLength {
// 如果contentLength 等于0,说明body的内容为空,可以直接退出
if p.contentLength == 0 {
p.complete(setting)
p.complete(setting, i)
return i, nil
}
currState = httpBody
Expand All @@ -612,7 +612,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
if p.EOF() {
currState = messageDone

p.complete(setting)
p.complete(setting, i)
continue
}
//一直读到socket eof
Expand All @@ -621,7 +621,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
if p.hasContentLength {
nread := min(int32(len(buf[i:])), p.contentLength)
if setting.Body != nil && nread > 0 {
setting.Body(p, buf[i:int32(i)+nread])
setting.Body(p, buf[i:int32(i)+nread], i)
}

p.contentLength -= nread
Expand All @@ -634,13 +634,13 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)

// httpBody没有后继结点,所以这里发现数据消费完, 调用下MessageComplete方法
if p.contentLength == 0 {
p.complete(setting)
p.complete(setting, i)
}
}

case bodyIdentityEOF:
if setting.Body != nil {
setting.Body(p, buf[i:])
setting.Body(p, buf[i:], i)
i = len(buf) - 1
}

Expand Down Expand Up @@ -693,7 +693,7 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
case chunkedData:
nread := min(int32(len(buf[i:])), p.contentLength)
if setting.Body != nil && nread > 0 {
setting.Body(p, buf[chunkDataStartIndex:int32(chunkDataStartIndex)+nread])
setting.Body(p, buf[chunkDataStartIndex:int32(chunkDataStartIndex)+nread], i)
}

p.contentLength -= nread
Expand Down Expand Up @@ -728,12 +728,12 @@ func (p *Parser) Execute(setting *Setting, buf []byte) (success int, err error)
switch currState {
case reqURL:
if setting.URL != nil && len(buf[urlStartIndex:]) > 0 {
setting.URL(p, buf[urlStartIndex:])
setting.URL(p, buf[urlStartIndex:], len(buf))
}

case rspStatus:
if setting.Status != nil && len(buf[reasonPhraseIndex:]) > 0 {
setting.Status(p, buf[reasonPhraseIndex:])
setting.Status(p, buf[reasonPhraseIndex:], len(buf))
}
}

Expand Down
16 changes: 8 additions & 8 deletions setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ package httparser
// Setting 查阅#6 看设计变更原因
type Setting struct {
// 解析开始
MessageBegin func(*Parser)
MessageBegin func(*Parser, int)
// url 回调函数, 只有在request包才会回调
// 解析一个包时,URL回调可能会多次调用
URL func(*Parser, []byte)
URL func(*Parser, []byte, int)
// 状态短语
// 解析一个包时, Status回调可能会多次调用
Status func(*Parser, []byte)
Status func(*Parser, []byte, int)
// http field 回调函数
HeaderField func(*Parser, []byte)
HeaderField func(*Parser, []byte, int)
// http value 回调函数
HeaderValue func(*Parser, []byte)
HeaderValue func(*Parser, []byte, int)
// http 解析完成之后的回调函数
HeadersComplete func(*Parser)
HeadersComplete func(*Parser, int)
// body的回调函数
Body func(*Parser, []byte)
Body func(*Parser, []byte, int)
// 所有消息成功解析
MessageComplete func(*Parser)
MessageComplete func(*Parser, int)
}

// ReqOrRsp 请求还是响应
Expand Down

0 comments on commit 88b706a

Please sign in to comment.