Browse code

pv: support Contact header in $hfl(...)

Daniel-Constantin Mierla authored on 03/06/2021 09:11:50
Showing 1 changed files
... ...
@@ -2191,6 +2191,7 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2191 2191
 	pv_value_t tv = {0};
2192 2192
 	via_body_t *vb = NULL;
2193 2193
 	rr_t *rrb = NULL;
2194
+	contact_t *cb = NULL;
2194 2195
 	hdr_field_t *hf = NULL;
2195 2196
 	int n = 0;
2196 2197
 	str sval = STR_NULL;
... ...
@@ -2358,6 +2359,62 @@ int pv_get_hfl(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2358 2359
 		return pv_get_null(msg, param, res);
2359 2360
 	}
2360 2361
 
2362
+	if((tv.flags == 0) && (tv.ri==HDR_CONTACT_T)) {
2363
+		if(msg->contact==NULL) {
2364
+			LM_DBG("no Contact header\n");
2365
+			return pv_get_null(msg, param, res);
2366
+		}
2367
+		if(parse_contact_headers(msg) < 0) {
2368
+			LM_DBG("failed to parse Contact headers\n");
2369
+			return pv_get_null(msg, param, res);
2370
+		}
2371
+		if(idx<0) {
2372
+			n = 1;
2373
+			/* count Contact header bodies */
2374
+			for(hf=msg->contact; hf!=NULL; hf=hf->next) {
2375
+				if(hf->type==HDR_CONTACT_T) {
2376
+					for(cb=(((contact_body_t*)hf->parsed)->contacts);
2377
+							cb!=NULL; cb=cb->next) {
2378
+						n++;
2379
+					}
2380
+				}
2381
+			}
2382
+
2383
+			idx = -idx;
2384
+			if(idx>n) {
2385
+				LM_DBG("index out of range\n");
2386
+				return pv_get_null(msg, param, res);
2387
+			}
2388
+			idx = n - idx;
2389
+		}
2390
+		if(idx==0) {
2391
+			cb = ((contact_body_t*)msg->contact->parsed)->contacts;
2392
+			sval.s = cb->name.s;
2393
+			sval.len = cb->len;
2394
+			trim(&sval);
2395
+			res->rs = sval;
2396
+			return 0;
2397
+		}
2398
+		n=0;
2399
+		for(hf=msg->contact; hf!=NULL; hf=hf->next) {
2400
+			if(hf->type==HDR_CONTACT_T) {
2401
+				for(cb=(((contact_body_t*)hf->parsed)->contacts);
2402
+						cb!=NULL; cb=cb->next) {
2403
+					if(n==idx) {
2404
+						sval.s = cb->name.s;
2405
+						sval.len = cb->len;
2406
+						trim(&sval);
2407
+						res->rs = sval;
2408
+						return 0;
2409
+					}
2410
+					n++;
2411
+				}
2412
+			}
2413
+		}
2414
+		LM_DBG("unexpected contact index out of range\n");
2415
+		return pv_get_null(msg, param, res);
2416
+	}
2417
+
2361 2418
 	return pv_get_hdr_helper(msg, param, res, &tv, idx, idxf);
2362 2419
 }
2363 2420