Skip to content

Commit

Permalink
feat: use content length as fast reference
Browse files Browse the repository at this point in the history
  • Loading branch information
emiago committed Dec 7, 2024
1 parent 8aa7572 commit ca92830
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 19 deletions.
3 changes: 3 additions & 0 deletions sip/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ type Message interface {
To() *ToHeader
// CSeq returns 'CSeq' header field.
CSeq() *CSeqHeader
// Content Length headers
ContentLength() *ContentLengthHeader

// Body returns message body.
Body() []byte
// SetBody sets message body.
Expand Down
22 changes: 3 additions & 19 deletions sip/parser_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"io"
"strconv"
"sync"
)

Expand Down Expand Up @@ -106,27 +105,12 @@ func (p *ParserStream) ParseSIPStream(data []byte) (msgs []Message, err error) {
}
unparsed = reader.Bytes()

// Grab content length header
// TODO: Maybe this is not best approach
hdrs := msg.GetHeaders("Content-Length")
if len(hdrs) == 0 {
// No body then
h := msg.ContentLength()
if h == nil {
return msg, nil
}

h := hdrs[0]

// TODO: Have fast reference instead casting
var contentLength int
if clh, ok := h.(*ContentLengthHeader); ok {
contentLength = int(*clh)
} else {
n, err := strconv.Atoi(h.Value())
if err != nil {
return nil, fmt.Errorf("fail to parse content length: %w", err)
}
contentLength = n
}
contentLength := int(*h)

if contentLength <= 0 {
return msg, nil
Expand Down

0 comments on commit ca92830

Please sign in to comment.