ghettovoice / gosip

SIP in Go
BSD 2-Clause "Simplified" License
424 stars 90 forks source link

Provide the ability to add additional headers to CANCEL/ACK requests generated by a client transaction. #73

Closed kuzmig closed 2 weeks ago

kuzmig commented 3 weeks ago

Hello!

When transaction user calls tx.Cancel() client transaction generates SIP CANCEL request using sip.NewCancelRequest and sends it directly to the transport layer. sip.NewCancelRequest creates requests with predefined headers and, as result, there is no way to add any extra headers to this request. The same situation we have for ACK request generated by client transaction for 300+ response.

Is it possible to provide the ability to pass some "common headers" to the transaction?

ghettovoice commented 2 weeks ago

Hello @kuzmig , yeah, I agree, this can be useful.

There is a main problem how to not break current API (at least until I will introduce the new version). I think, that it is possible to add new method(s) to setup callbacks on the client transaction, something like OnCancel(fn func(req sip.Request)) and OnAck(fn func(req sip.Request)). In the callback there is possible to update generated ACK/CANCEL request in any way before sending.

kuzmig commented 2 weeks ago

Callback approach looks good for me.

ghettovoice commented 2 weeks ago

Done in 24d12c01bb2419918bb944a59486c5dc0a22d39b.

  1. Set up callbacks via client tx:
    
    // tx - sip.ClientTransaction
    tx.OnAck(func(ackReq sip.Request) {
    ...
    })

tx.OnCancel(func(cancReq sip.Request) { ... })

2. With server's RequestWithContext method:
```go
// srv - gosip.Server
srv.RequestWithContext(ctx, inviteReq, 
    gosip.WithClientTransactionCallbacks(gosip.ClientTransactionCallbacks{
        OnAck: func(ackReq sip.Request) { ... },
        OnCancel: func(cancReq sip.Request) { ... },
    })
)
kuzmig commented 2 weeks ago

Looks great! Will give it a try today. Thank you very much!

kuzmig commented 2 weeks ago

Works good! Thanks!