Browse code

- new function: remove_hf_re(regexp) - remove headers from the SIP messages based on regular expression - all headers matching name with the parameter regexp are removed - example: remove headers starting with "P-" - remove_hf_re("^P-");

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5403 689a6050-402a-0410-94f2-e92a70836424

Daniel-Constantin Mierla authored on 30/12/2008 19:47:13
Showing 3 changed files
... ...
@@ -63,10 +63,11 @@ Daniel-Constantin Mierla
63 63
               1.3.23. append_time()
64 64
               1.3.24. is_method(name)
65 65
               1.3.25. remove_hf(hname)
66
-              1.3.26. has_body(), has_body(mime)
67
-              1.3.27. is_privacy(privacy_type)
68
-              1.3.28. cmp_str(str1, str2)
69
-              1.3.29. cmp_istr(str1, str2)
66
+              1.3.26. remove_hf_re(re)
67
+              1.3.27. has_body(), has_body(mime)
68
+              1.3.28. is_privacy(privacy_type)
69
+              1.3.29. cmp_str(str1, str2)
70
+              1.3.30. cmp_istr(str1, str2)
70 71
 
71 72
         1.4. Known Limitations
72 73
 
... ...
@@ -103,10 +104,11 @@ Daniel-Constantin Mierla
103 104
    1.23. append_time usage
104 105
    1.24. is_method usage
105 106
    1.25. remove_hf usage
106
-   1.26. has_body usage
107
-   1.27. is_privacy usage
108
-   1.28. cmp_str usage
107
+   1.26. remove_hf_re usage
108
+   1.27. has_body usage
109
+   1.28. is_privacy usage
109 110
    1.29. cmp_str usage
111
+   1.30. cmp_str usage
110 112
 
111 113
 Chapter 1. Admin Guide
112 114
 
... ...
@@ -649,7 +651,29 @@ if(remove_hf("User-Agent"))
649 651
 }
650 652
 ...
651 653
 
652
-1.3.26.  has_body(), has_body(mime)
654
+1.3.26.  remove_hf_re(re)
655
+
656
+   Remove from message all headers with name matching regular
657
+   expression "re"
658
+
659
+   Returns true if at least one header is found and removed.
660
+
661
+   Meaning of the parameters is as follows:
662
+     * re - regular expression to match the header name to be
663
+       removed.
664
+
665
+   This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
666
+   FAILURE_ROUTE and BRANCH_ROUTE.
667
+
668
+   Example 1.26. remove_hf_re usage
669
+...
670
+if(remove_hf_re("^P-"))
671
+{
672
+    # All headers starting with "P-" removed
673
+}
674
+...
675
+
676
+1.3.27.  has_body(), has_body(mime)
653 677
 
654 678
    The function returns true if the SIP message has a body
655 679
    attached. The checked includes also the "Content-Lenght" header
... ...
@@ -665,7 +689,7 @@ if(remove_hf("User-Agent"))
665 689
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
666 690
    FAILURE_ROUTE and BRANCH_ROUTE.
667 691
 
668
-   Example 1.26. has_body usage
692
+   Example 1.27. has_body usage
669 693
 ...
670 694
 if(has_body("application/sdp"))
671 695
 {
... ...
@@ -673,7 +697,7 @@ if(has_body("application/sdp"))
673 697
 }
674 698
 ...
675 699
 
676
-1.3.27.  is_privacy(privacy_type)
700
+1.3.28.  is_privacy(privacy_type)
677 701
 
678 702
    The function returns true if the SIP message has a Privacy
679 703
    header field that includes the given privacy_type among its
... ...
@@ -684,7 +708,7 @@ if(has_body("application/sdp"))
684 708
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
685 709
    FAILURE_ROUTE and BRANCH_ROUTE.
686 710
 
687
-   Example 1.27. is_privacy usage
711
+   Example 1.28. is_privacy usage
688 712
 ...
689 713
 if(is_privacy("id"))
690 714
 {
... ...
@@ -692,7 +716,7 @@ if(is_privacy("id"))
692 716
 }
693 717
 ...
694 718
 
695
-1.3.28.  cmp_str(str1, str2)
719
+1.3.29.  cmp_str(str1, str2)
696 720
 
697 721
    The function returns true if the two parameters matches as
698 722
    string case sensitive comparison.
... ...
@@ -700,7 +724,7 @@ if(is_privacy("id"))
700 724
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
701 725
    FAILURE_ROUTE and BRANCH_ROUTE.
702 726
 
703
-   Example 1.28. cmp_str usage
727
+   Example 1.29. cmp_str usage
704 728
 ...
705 729
 if(cmp_str("$rU", "kamailio"))
706 730
 {
... ...
@@ -708,7 +732,7 @@ if(cmp_str("$rU", "kamailio"))
708 732
 }
709 733
 ...
710 734
 
711
-1.3.29.  cmp_istr(str1, str2)
735
+1.3.30.  cmp_istr(str1, str2)
712 736
 
713 737
    The function returns true if the two parameters matches as
714 738
    string case insensitive comparison.
... ...
@@ -716,7 +740,7 @@ if(cmp_str("$rU", "kamailio"))
716 740
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
717 741
    FAILURE_ROUTE and BRANCH_ROUTE.
718 742
 
719
-   Example 1.29. cmp_str usage
743
+   Example 1.30. cmp_str usage
720 744
 ...
721 745
 if(cmp_istr("$rU@you", "kamailio@YOU"))
722 746
 {
... ...
@@ -981,6 +981,42 @@ if(remove_hf("User-Agent"))
981 981
 		</example>
982 982
 	</section>
983 983
 
984
+	<section>
985
+		<title>
986
+		<function moreinfo="none">remove_hf_re(re)</function>
987
+		</title>
988
+		<para>
989
+			Remove from message all headers with name matching regular
990
+			expression <quote>re</quote>
991
+		</para>
992
+		<para>
993
+		Returns true if at least one header is found and removed.
994
+		</para>
995
+		<para>Meaning of the parameters is as follows:</para>
996
+		<itemizedlist>
997
+		<listitem>
998
+			<para><emphasis>re</emphasis> - regular expression to match
999
+				the header name to be removed.
1000
+			</para>
1001
+		</listitem>
1002
+		</itemizedlist>
1003
+		<para>
1004
+		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE, 
1005
+		FAILURE_ROUTE and BRANCH_ROUTE.
1006
+		</para>
1007
+		<example>
1008
+		<title><function>remove_hf_re</function> usage</title>
1009
+		<programlisting format="linespecific">
1010
+...
1011
+if(remove_hf_re("^P-"))
1012
+{
1013
+    # All headers starting with "P-" removed
1014
+}
1015
+...
1016
+</programlisting>
1017
+		</example>
1018
+	</section>
1019
+
984 1020
 	<section>
985 1021
 		<title>
986 1022
 		<function moreinfo="none">has_body()</function>,
... ...
@@ -118,6 +118,7 @@ static int has_body_f(struct sip_msg *msg, char *type, char *str2 );
118 118
 static int is_privacy_f(struct sip_msg *msg, char *privacy, char *str2 );
119 119
 static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
120 120
 static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
121
+static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
121 122
 
122 123
 static int fixup_substre(void**, int);
123 124
 static int hname_fixup(void** param, int param_no);
... ...
@@ -185,6 +186,9 @@ static cmd_export_t cmds[]={
185 186
 	{"remove_hf",        (cmd_function)remove_hf_f,       1,
186 187
 		hname_fixup, free_hname_fixup,
187 188
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
189
+	{"remove_hf_re",     (cmd_function)remove_hf_re_f,    1,
190
+		fixup_regexp_null, fixup_free_regexp_null,
191
+		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
188 192
 	{"is_present_hf",    (cmd_function)is_present_hf_f,   1,
189 193
 		hname_fixup, free_hname_fixup,
190 194
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
... ...
@@ -920,6 +924,42 @@ int remove_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
920 924
 	return cnt==0 ? -1 : 1;
921 925
 }
922 926
 
927
+static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo)
928
+{
929
+	struct hdr_field *hf;
930
+	struct lump* l;
931
+	int cnt;
932
+	regex_t *re;
933
+	char c;
934
+	regmatch_t pmatch;
935
+
936
+	re = (regex_t*)key;
937
+	cnt=0;
938
+
939
+	/* we need to be sure we have seen all HFs */
940
+	parse_headers(msg, HDR_EOH_F, 0);
941
+	for (hf=msg->headers; hf; hf=hf->next)
942
+	{
943
+		c = hf->name.s[hf->name.len];
944
+		hf->name.s[hf->name.len] = '\0';
945
+		if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
946
+		{
947
+			hf->name.s[hf->name.len] = c;
948
+			continue;
949
+		}
950
+		hf->name.s[hf->name.len] = c;
951
+		l=del_lump(msg, hf->name.s-msg->buf, hf->len, 0);
952
+		if (l==0)
953
+		{
954
+			LM_ERR("cannot remove header\n");
955
+			return -1;
956
+		}
957
+		cnt++;
958
+	}
959
+
960
+	return cnt==0 ? -1 : 1;
961
+}
962
+
923 963
 static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
924 964
 {
925 965
 	struct hdr_field *hf;