Skip to content

Commit dc085c1

Browse files
committed
feat(BIG CHANGE): new request use recipient uri as value instead pointer to avoid some nil exceptions
1 parent b625de0 commit dc085c1

11 files changed

+38
-41
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ certs
66
/self-signed-root-ca-and-certificates.sh
77
/example/goproxy
88
/example/astscale
9-
/go.work
9+
/go.work
10+
/LONG_BENCHES.md

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Fetch lib with:
2121

2222
*Also, if you are using lib in any way, we would like to share it here.*
2323

24+
25+
For open discussion you can join on [slack sipgo](https://app.slack.com/client/T06FPMTT46Q/C06FPMTV70U)
26+
2427
## Supported protocols
2528

2629
- [x] UDP

client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestClientRequestBuild(t *testing.T) {
3030
UriParams: sip.HeaderParams{"foo": "bar"},
3131
}
3232

33-
req := sip.NewRequest(sip.OPTIONS, &recipment)
33+
req := sip.NewRequest(sip.OPTIONS, recipment)
3434
clientRequestBuildReq(c, req)
3535

3636
via := req.Via()
@@ -76,7 +76,7 @@ func TestClientRequestBuildWithNAT(t *testing.T) {
7676
UriParams: sip.NewParams(),
7777
}
7878

79-
req := sip.NewRequest(sip.OPTIONS, &recipment)
79+
req := sip.NewRequest(sip.OPTIONS, recipment)
8080
clientRequestBuildReq(c, req)
8181

8282
via := req.Via()
@@ -103,7 +103,7 @@ func TestClientRequestBuildWithHostAndPort(t *testing.T) {
103103
Port: 5060,
104104
}
105105

106-
req := sip.NewRequest(sip.OPTIONS, &recipment)
106+
req := sip.NewRequest(sip.OPTIONS, recipment)
107107
clientRequestBuildReq(c, req)
108108

109109
via := req.Via()

dialog_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (e ErrDialogResponse) Error() string {
5959
// Invite sends INVITE request and creates early dialog session.
6060
// You need to call WaitAnswer after for establishing dialog
6161
// For passing custom Invite request use WriteInvite
62-
func (dc *DialogClient) Invite(ctx context.Context, recipient *sip.Uri, body []byte, headers ...sip.Header) (*DialogClientSession, error) {
62+
func (dc *DialogClient) Invite(ctx context.Context, recipient sip.Uri, body []byte, headers ...sip.Header) (*DialogClientSession, error) {
6363
req := sip.NewRequest(sip.INVITE, recipient)
6464
if body != nil {
6565
req.SetBody(body)

dialog_integration_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestIntegrationDialog(t *testing.T) {
111111
t.Run("UAS hangup", func(t *testing.T) {
112112
// INVITE
113113
t.Log("UAC: INVITE")
114-
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address.Clone(), nil)
114+
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
115115
require.NoError(t, err)
116116

117117
err = sess.WaitAnswer(ctx, AnswerOptions{})
@@ -129,7 +129,7 @@ func TestIntegrationDialog(t *testing.T) {
129129
t.Run("UAC hangup", func(t *testing.T) {
130130
// INVITE
131131
t.Log("UAC: INVITE")
132-
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address.Clone(), nil)
132+
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
133133
require.NoError(t, err)
134134

135135
err = sess.WaitAnswer(ctx, AnswerOptions{})
@@ -242,7 +242,7 @@ func TestIntegrationDialogBrokenUAC(t *testing.T) {
242242
})
243243
// INVITE
244244
t.Log("UAC: INVITE")
245-
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address.Clone(), nil)
245+
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
246246
require.NoError(t, err)
247247

248248
err = sess.WaitAnswer(ctx, AnswerOptions{})
@@ -263,7 +263,7 @@ func TestIntegrationDialogBrokenUAC(t *testing.T) {
263263
t.Run("UAS ACK Error", func(t *testing.T) {
264264
// INVITE
265265
t.Log("UAC: INVITE")
266-
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address.Clone(), nil)
266+
sess, err := dialogCli.Invite(context.TODO(), uasContact.Address, nil)
267267
require.NoError(t, err)
268268

269269
err = sess.WaitAnswer(ctx, AnswerOptions{})

dialog_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (s *DialogServerSession) Bye(ctx context.Context) error {
227227
}
228228

229229
cont := req.Contact()
230-
bye := sip.NewRequest(sip.BYE, &cont.Address)
230+
bye := sip.NewRequest(sip.BYE, cont.Address)
231231

232232
// Reverse from and to
233233
from := res.From()

server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func testCreateMessage(t testing.TB, rawMsg []string) sip.Message {
2626
}
2727

2828
func createSimpleRequest(method sip.RequestMethod, sender sip.Uri, recipment sip.Uri, transport string) *sip.Request {
29-
req := sip.NewRequest(method, &recipment)
29+
req := sip.NewRequest(method, recipment)
3030
params := sip.NewParams()
3131
params["branch"] = sip.GenerateBranch()
3232
req.AppendHeader(&sip.ViaHeader{

sip/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func parseLine(startLine string) (msg Message, err error) {
171171
return nil, err
172172
}
173173

174-
m := NewRequest(method, &recipient)
174+
m := NewRequest(method, recipient)
175175
m.SipVersion = sipVersion
176176
return m, nil
177177
}

sip/parser_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func testParseHeader(t *testing.T, parser *Parser, header string) Header {
134134

135135
func testParseHeaderOnRequest(t *testing.T, parser *Parser, header string) (*Request, Header) {
136136
// This is fake way to get parsing done. We use fake message and read first header
137-
msg := NewRequest(INVITE, nil)
137+
msg := NewRequest(INVITE, Uri{})
138138
name := strings.Split(header, ":")[0]
139139
err := parser.headersParsers.parseMsgHeader(msg, header)
140140
require.Nil(t, err)

sip/request.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111
type Request struct {
1212
MessageData
1313
Method RequestMethod
14-
Recipient *Uri
14+
Recipient Uri
1515
}
1616

1717
// NewRequest creates base for building sip Request
18-
// sipVersion must be SIP/2.0
18+
// A Request-Line contains a method name, a Request-URI, and the SIP/2.0 as version
1919
// No headers are added. AppendHeader should be called to add Headers.
2020
// r.SetBody can be called to set proper ContentLength header
21-
func NewRequest(method RequestMethod, recipient *Uri) *Request {
21+
func NewRequest(method RequestMethod, recipient Uri) *Request {
2222
req := &Request{}
2323
req.SipVersion = "SIP/2.0"
2424
// req.headers = newHeaders()
@@ -115,22 +115,20 @@ func (req *Request) Transport() string {
115115

116116
uri := req.Recipient
117117
if hdr := req.Route(); hdr != nil {
118-
uri = &hdr.Address
118+
uri = hdr.Address
119119
}
120120

121-
if uri != nil {
122-
if uri.UriParams != nil {
123-
if val, ok := uri.UriParams.Get("transport"); ok && val != "" {
124-
tp = strings.ToUpper(val)
125-
}
121+
if uri.UriParams != nil {
122+
if val, ok := uri.UriParams.Get("transport"); ok && val != "" {
123+
tp = strings.ToUpper(val)
126124
}
125+
}
127126

128-
if uri.IsEncrypted() {
129-
if tp == "TCP" {
130-
tp = "TLS"
131-
} else if tp == "WS" {
132-
tp = "WSS"
133-
}
127+
if uri.IsEncrypted() {
128+
if tp == "TCP" {
129+
tp = "TLS"
130+
} else if tp == "WS" {
131+
tp = "WSS"
134132
}
135133
}
136134

@@ -189,14 +187,9 @@ func (req *Request) Destination() string {
189187
var uri *Uri
190188
if hdr := req.Route(); hdr != nil {
191189
uri = &hdr.Address
192-
193190
}
194191
if uri == nil {
195-
if u := req.Recipient; u != nil {
196-
uri = u
197-
} else {
198-
return ""
199-
}
192+
uri = &req.Recipient
200193
}
201194

202195
host := uri.Host
@@ -213,13 +206,13 @@ func (req *Request) Destination() string {
213206
// NOTE: it does not copy Via header. This is left to transport or caller to enforce
214207
// Deprecated: use DialogClient for building dialogs
215208
func NewAckRequest(inviteRequest *Request, inviteResponse *Response, body []byte) *Request {
216-
Recipient := inviteRequest.Recipient
209+
Recipient := &inviteRequest.Recipient
217210
if contact := inviteResponse.Contact(); contact != nil {
218211
Recipient = &contact.Address
219212
}
220213
ackRequest := NewRequest(
221214
ACK,
222-
Recipient,
215+
*Recipient.Clone(),
223216
)
224217
ackRequest.SipVersion = inviteRequest.SipVersion
225218

@@ -330,7 +323,7 @@ func NewCancelRequest(requestForCancel *Request) *Request {
330323
// NOTE: it does not copy Via header. This is left to transport or caller to enforce
331324
// Deprecated: use DialogClient for building dialogs
332325
func NewByeRequestUAC(inviteRequest *Request, inviteResponse *Response, body []byte) *Request {
333-
recipient := inviteRequest.Recipient
326+
recipient := &inviteRequest.Recipient
334327
cont := inviteResponse.Contact()
335328
if cont != nil {
336329
// BYE is subsequent request
@@ -339,7 +332,7 @@ func NewByeRequestUAC(inviteRequest *Request, inviteResponse *Response, body []b
339332

340333
byeRequest := NewRequest(
341334
BYE,
342-
recipient,
335+
*recipient.Clone(),
343336
)
344337
byeRequest.SipVersion = inviteRequest.SipVersion
345338

@@ -385,7 +378,7 @@ func NewByeRequestUAC(inviteRequest *Request, inviteResponse *Response, body []b
385378
func cloneRequest(req *Request) *Request {
386379
newReq := NewRequest(
387380
req.Method,
388-
req.Recipient.Clone(),
381+
*req.Recipient.Clone(),
389382
)
390383
newReq.SipVersion = req.SipVersion
391384

0 commit comments

Comments
 (0)