[sr-dev] [kamailio/kamailio] $(hdr(foo)[idx]) is 0-based, textopsx.remove_hf_value("foo[idx]") is 1-based, they should be the same (#2387)

Sebastian Damm notifications at github.com
Thu Jul 2 17:05:21 CEST 2020


### Description

We have some JS code looping over all P-Asserted-Identity headers removing some of the entries:

```
    let headerIndex = 0;
    const telUriMatcher = new RegExp("tel:");
    let paiValue = ksr.pv
      .gete("$(hdr(P-Asserted-Identity)[" + headerIndex + "])")
      .toString();
    while (paiValue.length > 0) {
      ksr.info(
        `${ksr.pv.getw("$ci")}: Found P-A-I header with content <${ksr.pv.getw(
          "$(hdr(P-Asserted-Identity)[" + headerIndex + "])"
        )}> at index ${headerIndex}\n`
      );
      if (telUriMatcher.test(paiValue)) {
        ksr.info(
          `${ksr.pv.getw("$ci")}: Removed P-A-I header ${ksr.pv.getw(
            "$(hdr(P-Asserted-Identity)[" + headerIndex + "])"
          )}\n`
        );
        ksr.textopsx.remove_hf_value(
          "P-Asserted-Identity[" + headerIndex + "]"
        );
      }
      headerIndex++;
      paiValue = ksr.pv
        .gete("$(hdr(P-Asserted-Identity)[" + headerIndex + "])")
        .toString();
    }
```
In our tests, we wanted to remove the second header, but the first one got removed instead. The pseudovars docs say, the index of a header is 0-based. In textopsx docs it doesn't explicitly say, it's 1-based, but the first example shows it actually is.

### Possible Solutions

Right now, we replaced the line
```
ksr.textopsx.remove_hf_value("P-Asserted-Identity[" + headerIndex + "]");
```
with this one:
```
ksr.textopsx.remove_hf_value("P-Asserted-Identity[" + (headerIndex + 1) + "]");
```
This solves the problem, but it is not what we would expect. Both header access methods should use the same index base.

### Additional Information

  * **Kamailio Version** - output of `kamailio -v`

```
version: kamailio 5.3.5 (x86_64/linux)
flags: USE_TCP, USE_TLS, USE_SCTP, TLS_HOOKS, USE_RAW_SOCKS, DISABLE_NAGLE, USE_MCAST, DNS_IP_HACK, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, TLSF_MALLOC, DBG_SR_MEMORY, USE_FUTEX, FAST_LOCK-ADAPTIVE_WAIT, USE_DNS_CACHE, USE_DNS_FAILOVER, USE_NAPTR, USE_DST_BLACKLIST, HAVE_RESOLV_RES, TLS_PTHREAD_MUTEX_SHARED
ADAPTIVE_WAIT_LOOPS 1024, MAX_RECV_BUFFER_SIZE 262144, MAX_URI_SIZE 1024, BUF_SIZE 65535, DEFAULT PKG_SIZE 8MB
poll method support: poll, epoll_lt, epoll_et, sigio_rt, select.
id: unknown
compiled with gcc 8.3.0
```

* **Operating System**:

<!--
Details about the operating system, the type: Linux (e.g.,: Debian 8.4, Ubuntu 16.04, CentOS 7.1, ...), MacOS, xBSD, Solaris, ...;
Kernel details (output of `uname -a`)
-->

```
Linux hostname 4.19.0-9-amd64 #1 SMP Debian 4.19.118-2+deb10u1 (2020-06-07) x86_64 GNU/Linux
```


-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/kamailio/kamailio/issues/2387
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.kamailio.org/pipermail/sr-dev/attachments/20200702/5de12b6b/attachment-0001.html>


More information about the sr-dev mailing list