Skip to content

Commit 1701afe

Browse files
authored
Merge pull request #576 from alexbakker/empty-reason
Support parsing SIP response with empty reason phrase
2 parents af689bf + 85c1ec0 commit 1701afe

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

sipparser/parser_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,23 @@ import (
1616

1717
var testInviteMsg = "INVITE sip:[email protected]:5060;user=phone SIP/2.0\r\nVia: SIP/2.0/UDP X.X.X.X:5060;branch=z9hG4bK34133a599ll241207INV21d7d0684e84a2d2\r\nMax-Forwards: 35\r\nContact: <sip:X.X.X.X:5060>\r\nTo: <sip:[email protected];user=phone;noa=national>\r\nFrom: \"Unavailable\"<sip:X.X.X.X;user=phone;noa=national>;tag=21d7d068-co2149-FOOI003\r\nCall-ID: [email protected]\r\nCSeq: 214901 INVITE\r\nAuthorization: Digest username=\"foobaruser124\", realm=\"FOOBAR\", algorithm=MD5, uri=\"sip:foo.bar.com\", nonce=\"4f6d7a1d\", response=\"6a79a5c75572b0f6a18963ae04e971cf\", opaque=\"\"\r\nAllow: INVITE,ACK,CANCEL,BYE,REFER,OPTIONS,NOTIFY,SUBSCRIBE,PRACK,INFO\r\nContent-Type: application/sdp\r\nDate: Thu, 29 Sep 2011 16:54:42 GMT\r\nUser-Agent: FAKE-UA-DATA\r\nP-Asserted-Identity: \"Unavailable\"<sip:[email protected]:5060>\r\nContent-Length: 322\r\n\r\nv=0\r\no=- 567791720 567791720 IN IP4 X.X.X.X\r\ns=FAKE-DATA\r\nc=IN IP4 X.X.X.X\r\nt=0 0\r\nm=audio 17354 RTP/AVP 0 8 86 18 96\r\na=rtpmap:0 PCMU/8000\r\na=rtpmap:8 PCMA/8000\r\na=rtpmap:86 G726-32/8000\r\na=rtpmap:18 G729/8000\r\na=rtpmap:96 telephone-event/8000\r\na=maxptime:20\r\na=fmtp:18 annexb=yes\r\na=fmtp:96 0-15\r\na=sendrecv\r\n"
1818
var test400ErrorResp = "SIP/2.0 400 Bad Request\r\n"
19+
var test500ErrorNoReasonResp = "SIP/2.0 500 \r\n"
1920

20-
// should succeed, i.e. not produce an error.
21+
// should succeed, i.e. not produce an error.
2122
func TestParse400Err(t *testing.T) {
2223
s := ParseMsg(test400ErrorResp, nil, nil)
2324
if s.Error != nil {
2425
t.Errorf("[TestParse400Err] Error parsing msg. Received: %v", s.Error)
2526
}
2627
}
2728

29+
func TestParse500ErrNoReason(t *testing.T) {
30+
s := ParseMsg(test500ErrorNoReasonResp, nil, nil)
31+
if s.Error != nil {
32+
t.Errorf("[TestParse500ErrNoReason] Error parsing msg. Received: %v", s.Error)
33+
}
34+
}
35+
2836
func TestHeader(t *testing.T) {
2937
h := Header{"t", "v"}
3038
if h.String() != "t: v" {

sipparser/startline.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func parseStartLine(s *StartLine) parseStartLineStateFn {
7171

7272
func parseStartLineResponse(s *StartLine) parseStartLineStateFn {
7373
parts := strings.SplitN(s.Val, " ", 3)
74-
if len(parts) != 3 {
74+
if len(parts) < 2 || len(parts) > 3 {
7575
s.Error = errors.New("parseStartLineRespone err: err getting parts from LWS")
7676
return nil
7777
}
@@ -87,7 +87,9 @@ func parseStartLineResponse(s *StartLine) parseStartLineStateFn {
8787
}
8888
s.Version = parts[0][charPos+1:]
8989
s.Resp = parts[1]
90-
s.RespText = parts[2]
90+
if len(parts) > 2 {
91+
s.RespText = parts[2]
92+
}
9193
return nil
9294
}
9395

sipparser/startline_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ func TestStartLine(t *testing.T) {
2323
if s.RespText != "Request Cancelled" {
2424
t.Error("[TestStartLine] Error parsing startline: SIP/2.0 487 Request Cancelled. s.RespText should be \"Request Cancelled\".")
2525
}
26+
str = "SIP/2.0 500"
27+
s = &StartLine{Val: str}
28+
s.run()
29+
if s.Type != SIP_RESPONSE {
30+
t.Error("[TestStartLine] Error parsing startline: SIP/2.0 500. s.Type should be \"RESPONSE\".")
31+
}
32+
if s.Resp != "500" {
33+
t.Error("[TestStartLine] Error parsing startline: SIP/2.0 500. s.Resp should be \"500\".")
34+
}
35+
if s.RespText != "" {
36+
t.Error("[TestStartLine] Error parsing startline: SIP/2.0 500. s.RespText should be \"\".")
37+
}
2638
str = "1412@34922@[email protected]:5061;transport=tcp;[email protected] 111111111"
2739
s = ParseStartLine(str)
2840
if s.Error == nil {

0 commit comments

Comments
 (0)