Browse code

pv: added {val.jsonqe} - quoted json value

Daniel-Constantin Mierla authored on 06/12/2021 13:52:45
Showing 2 changed files
... ...
@@ -2590,6 +2590,41 @@ int tr_eval_val(struct sip_msg *msg, tr_param_t *tp, int subtype,
2590 2590
 				val->rs.len = sv.len;
2591 2591
 			}
2592 2592
 			break;
2593
+		case TR_VAL_JSONQE:
2594
+			if(val->flags&PV_VAL_NULL) {
2595
+				val->ri = 0;
2596
+				tr_set_crt_buffer();
2597
+				val->rs.s = _tr_buffer;
2598
+				val->rs.s[0] = '"';
2599
+				val->rs.s[1] = '"';
2600
+				val->rs.s[2] = '\0';
2601
+				val->rs.len = 2;
2602
+				val->flags = PV_VAL_STR;
2603
+			} else if(val->flags&PV_TYPE_INT) {
2604
+				break;
2605
+			} else if(val->flags&PV_VAL_STR) {
2606
+				ksr_str_json_escape(&val->rs, &sv, &emode);
2607
+				if(sv.s==NULL) {
2608
+					LM_ERR("failed to escape the value\n");
2609
+					return -1;
2610
+				}
2611
+				if(emode==0) {
2612
+					/* no escape was needed */
2613
+					return 0;
2614
+				}
2615
+				if(sv.len >= TR_BUFFER_SIZE - 3) {
2616
+					LM_ERR("escaped value is too long\n");
2617
+					return -1;
2618
+				}
2619
+				tr_set_crt_buffer();
2620
+				_tr_buffer[0] = '"';
2621
+				memcpy(_tr_buffer + 1, sv.s, sv.len);
2622
+				_tr_buffer[sv.len + 1] = '"';
2623
+				_tr_buffer[sv.len + 2] = '\0';
2624
+				val->rs.s = _tr_buffer;
2625
+				val->rs.len = sv.len + 2;
2626
+			}
2627
+			break;
2593 2628
 
2594 2629
 		default:
2595 2630
 			LM_ERR("unknown subtype %d\n",
... ...
@@ -3847,6 +3882,9 @@ char* tr_parse_val(str* in, trans_t *t)
3847 3882
 	} else if(name.len==4 && strncasecmp(name.s, "json", 4)==0) {
3848 3883
 		t->subtype = TR_VAL_JSON;
3849 3884
 		goto done;
3885
+	} else if(name.len==6 && strncasecmp(name.s, "jsonqe", 6)==0) {
3886
+		t->subtype = TR_VAL_JSONQE;
3887
+		goto done;
3850 3888
 	}
3851 3889
 
3852 3890
 
... ...
@@ -73,7 +73,7 @@ enum _tr_urialias_subtype {
73 73
 };
74 74
 
75 75
 enum _tr_val_subtype {
76
-	TR_VAL_NONE=0, TR_VAL_N0, TR_VAL_NE, TR_VAL_JSON
76
+	TR_VAL_NONE=0, TR_VAL_N0, TR_VAL_NE, TR_VAL_JSON, TR_VAL_JSONQE
77 77
 };
78 78
 
79 79