Browse code

pv: new variable $hflc(hname)

- return the number of bodies for known headers that can also be as a comma
separated list
- supported now is Via header

Daniel-Constantin Mierla authored on 08/06/2021 11:25:54
Showing 3 changed files
... ...
@@ -110,6 +110,8 @@ static pv_export_t mod_pvs[] = {
110 110
 		0, 0, 0},
111 111
 	{{"hfl", (sizeof("hfl")-1)}, PVT_HDR, pv_get_hfl, 0, pv_parse_hfl_name,
112 112
 		pv_parse_index, 0, 0},
113
+	{{"hflc", (sizeof("hflc")-1)}, PVT_HDRC, pv_get_hflc, 0, pv_parse_hfl_name,
114
+		0, 0, 0},
113 115
 	{{"var", (sizeof("var")-1)}, PVT_SCRIPTVAR, pv_get_scriptvar,
114 116
 		pv_set_scriptvar, pv_parse_scriptvar_name, 0, 0, 0},
115 117
 	{{"vz", (sizeof("vz")-1)}, PVT_SCRIPTVAR, pv_get_scriptvar,
... ...
@@ -2421,6 +2421,70 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2421 2421
 	return pv_get_hdr_helper(msg, param, res, &tv, idx, idxf);
2422 2422
 }
2423 2423
 
2424
+/**
2425
+ *
2426
+ */
2427
+int pv_get_hflc(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2428
+{
2429
+	pv_value_t tv = {0};
2430
+	via_body_t *vb = NULL;
2431
+	hdr_field_t *hf = NULL;
2432
+	int n = 0;
2433
+
2434
+	if(msg==NULL || res==NULL || param==NULL)
2435
+		return -1;
2436
+
2437
+	/* get the name */
2438
+	if(param->pvn.type == PV_NAME_PVAR) {
2439
+		if(pv_get_spec_name(msg, param, &tv)!=0 || (!(tv.flags&PV_VAL_STR))) {
2440
+			LM_ERR("invalid name\n");
2441
+			return pv_get_sintval(msg, param, res, 0);
2442
+		}
2443
+	} else {
2444
+		if(param->pvn.u.isname.type == AVP_NAME_STR) {
2445
+			tv.flags = PV_VAL_STR;
2446
+			tv.rs = param->pvn.u.isname.name.s;
2447
+		} else {
2448
+			tv.flags = 0;
2449
+			tv.ri = param->pvn.u.isname.name.n;
2450
+		}
2451
+	}
2452
+
2453
+	if (parse_headers(msg, HDR_EOH_F, 0)<0) {
2454
+		LM_DBG("failed to parse sip headers\n");
2455
+		return pv_get_sintval(msg, param, res, 0);
2456
+	}
2457
+
2458
+	if((tv.flags == 0) && (tv.ri==HDR_VIA_T)) {
2459
+		if(msg->h_via1==NULL) {
2460
+			LM_WARN("no Via header\n");
2461
+			return pv_get_sintval(msg, param, res, 0);
2462
+		}
2463
+		/* count Via header bodies */
2464
+		for(hf=msg->h_via1; hf!=NULL; hf=hf->next) {
2465
+			if(hf->type==HDR_VIA_T) {
2466
+				for(vb=(via_body_t*)hf->parsed; vb!=NULL; vb=vb->next) {
2467
+					n++;
2468
+				}
2469
+			}
2470
+		}
2471
+		return pv_get_sintval(msg, param, res, n);
2472
+	}
2473
+
2474
+	for (hf=msg->headers; hf; hf=hf->next) {
2475
+		if(tv.flags == 0) {
2476
+			if (tv.ri==hf->type) {
2477
+				n++;
2478
+			}
2479
+		} else {
2480
+			if (cmp_hdrname_str(&hf->name, &tv.rs)==0) {
2481
+				n++;
2482
+			}
2483
+		}
2484
+	}
2485
+	return pv_get_sintval(msg, param, res, n);
2486
+}
2487
+
2424 2488
 /**
2425 2489
  *
2426 2490
  */
... ...
@@ -255,6 +255,8 @@ int pv_get_hdrc(struct sip_msg *msg,  pv_param_t *param, pv_value_t *res);
255 255
 
256 256
 int pv_get_hfl(sip_msg_t *msg,  pv_param_t *param, pv_value_t *res);
257 257
 
258
+int pv_get_hflc(struct sip_msg *msg,  pv_param_t *param, pv_value_t *res);
259
+
258 260
 int pv_get_scriptvar(struct sip_msg *msg,  pv_param_t *param,
259 261
 		pv_value_t *res);
260 262