Browse Source

core: reworked how rport, received and extra params are added to first via of generated replies

Daniel-Constantin Mierla 7 months ago
parent
commit
d613f30214
1 changed files with 34 additions and 41 deletions
  1. 34 41
      src/core/msg_translator.c

+ 34 - 41
src/core/msg_translator.c

@@ -2667,57 +2667,50 @@ char *build_res_buf_from_sip_req(unsigned int code, str *text, str *new_tag,
 				if(unlikely(httpreq))
 				if(unlikely(httpreq))
 					pvia = p;
 					pvia = p;
 				if(hdr == msg->h_via1) {
 				if(hdr == msg->h_via1) {
-					if(rport_buf) {
-						if(msg->via1->rport) { /* delete the old one */
-							/* copy until rport */
-							append_str_trans(p, hdr->name.s,
-									msg->via1->rport->start - hdr->name.s - 1,
-									msg);
-							/* copy new rport */
-							append_str(p, rport_buf, rport_len);
-							/* copy the rest of the via */
-							append_str_trans(p,
-									msg->via1->rport->start
-											+ msg->via1->rport->size,
-									hdr->body.s + hdr->body.len
-											- msg->via1->rport->start
-											- msg->via1->rport->size,
-									msg);
-						} else if(msg->via1->branch) { /* add after branch */
-							/* copy until after branch */
-							append_str_trans(p, hdr->name.s,
-									msg->via1->branch->start - hdr->name.s
-											+ msg->via1->branch->size,
-									msg);
-							/* copy new rport */
-							append_str(p, rport_buf, rport_len);
-							/* copy the rest of the via */
-							append_str_trans(p,
-									msg->via1->branch->start
-											+ msg->via1->branch->size,
-									hdr->body.s + hdr->body.len
-											- msg->via1->branch->start
-											- msg->via1->branch->size,
-									msg);
-						} else { /* just append the new one */
-							/* normal whole via copy */
-							append_str_trans(p, hdr->name.s,
-									(hdr->body.s + hdr->body.len) - hdr->name.s,
-									msg);
-							append_str(p, rport_buf, rport_len);
-						}
-					} else {
-						/* normal whole via copy */
+					if(rport_buf && msg->via1->rport) { /* replace old rport */
+						/* copy until rport */
+						append_str_trans(p, hdr->name.s,
+								msg->via1->rport->start - hdr->name.s - 1, msg);
+					} else if(msg->via1->branch) { /* add after branch */
+						append_str_trans(p, hdr->name.s,
+								msg->via1->branch->start - hdr->name.s
+										+ msg->via1->branch->size,
+								msg);
+					} else { /* append after header */
 						append_str_trans(p, hdr->name.s,
 						append_str_trans(p, hdr->name.s,
 								(hdr->body.s + hdr->body.len) - hdr->name.s,
 								(hdr->body.s + hdr->body.len) - hdr->name.s,
 								msg);
 								msg);
 					}
 					}
+					if(rport_buf) {
+						/* add rport */
+						append_str(p, rport_buf, rport_len);
+					}
 					if(received_buf) {
 					if(received_buf) {
+						/* add received */
 						append_str(p, received_buf, received_len);
 						append_str(p, received_buf, received_len);
 					}
 					}
 					if(xparams.len > 0) {
 					if(xparams.len > 0) {
+						/* add extra parameters */
 						append_str(p, xparams.s, xparams.len);
 						append_str(p, xparams.s, xparams.len);
 					}
 					}
+					/* copy the rest of the via */
+					if(rport_buf && msg->via1->rport) {
+						append_str_trans(p,
+								msg->via1->rport->start
+										+ msg->via1->rport->size,
+								hdr->body.s + hdr->body.len
+										- msg->via1->rport->start
+										- msg->via1->rport->size,
+								msg);
+					} else if(msg->via1->branch) {
+						append_str_trans(p,
+								msg->via1->branch->start
+										+ msg->via1->branch->size,
+								hdr->body.s + hdr->body.len
+										- msg->via1->branch->start
+										- msg->via1->branch->size,
+								msg);
+					}
 				} else {
 				} else {
 					/* normal whole via copy */
 					/* normal whole via copy */
 					append_str_trans(p, hdr->name.s,
 					append_str_trans(p, hdr->name.s,