Skip to content

Commit 3632e69

Browse files
j-lichtemiago
authored andcommitted
fix: ParserStream handling StartLine and incomplete Header
Before the fix, when ParserStream got an partial message only containing a StartLine and parts of the following header, it failed with 'field name with no value in header: EOF on reading line' We need to adjust the unparsed slice after reading the StartLine.
1 parent be7c0f4 commit 3632e69

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

sip/parser_stream.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (p *ParserStream) parseSingle(reader *bytes.Buffer, unparsed *[]byte) (err
115115
return err
116116
}
117117

118+
*unparsed = reader.Bytes()
118119
p.state = stateHeader
119120
p.msg = msg
120121
fallthrough

sip/parser_stream_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,34 @@ func TestParserStreamMessageSizeLimit(t *testing.T) {
327327
require.Equal(t, "Message exceeds ParseMaxMessageLength", err.Error())
328328
}
329329

330+
func TestParserStreamPartialAfterStartLine(t *testing.T) {
331+
p := NewParser()
332+
parser := p.NewSIPStream()
333+
334+
lines1 := []string{
335+
"SIP/2.0 481 Call/Transaction Does Not Exist",
336+
"Via: SIP/2.0/TCP 10.10.42.37:48476;received=10.10.42.37;bran",
337+
}
338+
input1 := []byte(strings.Join(lines1, "\r\n"))
339+
340+
lines2 := []string{
341+
"ch=z9hG4bK.9WUsakU92PFG5mIv",
342+
"Call-ID: 2227040c-914c-4496-a715-1a93c9501360",
343+
"From: \"sipgo\" <sip:sipgo@localhost>;tag=Ffl4DGpHt5yvhgU2",
344+
"To: <sip:[email protected]>;tag=z9hG4bK.9WUsakU92PFG5mIv",
345+
"CSeq: 1 CANCEL",
346+
"Content-Length: 0",
347+
"",
348+
"",
349+
}
350+
input2 := []byte(strings.Join(lines2, "\r\n"))
351+
352+
_, err := parser.ParseSIPStream(input1)
353+
require.Error(t, err)
354+
_, err = parser.ParseSIPStream(input2)
355+
require.NoError(t, err)
356+
}
357+
330358
func BenchmarkParserStream(b *testing.B) {
331359
branch := GenerateBranch()
332360
callid := fmt.Sprintf("gotest-%d", time.Now().UnixNano())

0 commit comments

Comments
 (0)