aboutsip / pkts

Pure java based pcap library capable of reading and writing to/from pcaps.
Other
198 stars 92 forks source link

SipMessageBuilder - How to replace existing `To:` header #125

Closed teerapap closed 4 years ago

teerapap commented 4 years ago

Problem: I want to replace existing To: header. How to do that? It seems the SipMessageBuilder has not method to do it because this withHeader(..) method adds the input as a new header. The message output results in double To: header.

https://github.com/aboutsip/pkts/blob/1d72e989b0325b97f06484b28679444ad97a36ee/pkts-sip/src/main/java/io/pkts/packet/sip/impl/SipMessageBuilder.java#L220

Example code:

SipMessage.Builder<SipResponse> builder = invite.createResponse(180).withReasonPhrase("Ringing");
ToHeader toHeader = invite.getToHeader();
if (toHeader.getTag() == null) {
    toHeader = toHeader.copy().withDefaultTag().build();
    builder.withToHeader(toHeader);
}
SipResponse res = builder.build();

Thanks

jonbo372 commented 4 years ago

Have you looked at some of the examples? I have some basic here: http://www.siplib.io/ and checkout the unit tests in SipResponseTest and in particular here: https://github.com/aboutsip/pkts/blob/1d72e989b0325b97f06484b28679444ad97a36ee/pkts-sip/src/test/java/io/pkts/packet/sip/SipResponseTest.java#L114

The philosophy around creating a response from a request is slightly different than perhaps what you're used to. The idea is that you often want to copy over various headers as is (all headers are immutable) so when you build a response from a request, you kind of "stream" them over to the response and when that happens you have a chance to either drop them, keep them, or change them. The benefit is that I can under the hood have everything share the same underlying byte-array since, again, everything is immutable.

Hope that answers your question.

teerapap commented 4 years ago

Thanks @jonbo372