Browse code

- new function: is_present_hf_re(regexp) - return true if the regexp matches a header name in the sip message

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

Daniel-Constantin Mierla authored on 09/01/2009 11:14:46
Showing 3 changed files
... ...
@@ -60,14 +60,15 @@ Daniel-Constantin Mierla
60 60
               1.3.20. insert_hf(txt, hdr)
61 61
               1.3.21. append_urihf(prefix, suffix)
62 62
               1.3.22. is_present_hf(hf_name)
63
-              1.3.23. append_time()
64
-              1.3.24. is_method(name)
65
-              1.3.25. remove_hf(hname)
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)
63
+              1.3.23. is_present_hf_re(hf_name_re)
64
+              1.3.24. append_time()
65
+              1.3.25. is_method(name)
66
+              1.3.26. remove_hf(hname)
67
+              1.3.27. remove_hf_re(re)
68
+              1.3.28. has_body(), has_body(mime)
69
+              1.3.29. is_privacy(privacy_type)
70
+              1.3.30. cmp_str(str1, str2)
71
+              1.3.31. cmp_istr(str1, str2)
71 72
 
72 73
         1.4. Known Limitations
73 74
 
... ...
@@ -101,14 +102,15 @@ Daniel-Constantin Mierla
101 102
    1.20. insert_hf usage
102 103
    1.21. append_urihf usage
103 104
    1.22. is_present_hf usage
104
-   1.23. append_time usage
105
-   1.24. is_method usage
106
-   1.25. remove_hf usage
107
-   1.26. remove_hf_re usage
108
-   1.27. has_body usage
109
-   1.28. is_privacy usage
110
-   1.29. cmp_str usage
105
+   1.23. is_present_hf_re usage
106
+   1.24. append_time usage
107
+   1.25. is_method usage
108
+   1.26. remove_hf usage
109
+   1.27. remove_hf_re usage
110
+   1.28. has_body usage
111
+   1.29. is_privacy usage
111 112
    1.30. cmp_str usage
113
+   1.31. cmp_str usage
112 114
 
113 115
 Chapter 1. Admin Guide
114 116
 
... ...
@@ -570,7 +572,24 @@ Note
570 572
 if (is_present_hf("From")) log(1, "From HF Present");
571 573
 ...
572 574
 
573
-1.3.23.  append_time()
575
+1.3.23.  is_present_hf_re(hf_name_re)
576
+
577
+   Return true if a header field whose name matches regular
578
+   expression 'hf_name_re' is present in message.
579
+
580
+   Meaning of the parameters is as follows:
581
+     * hf_name_re - Regular expression to match header field name.
582
+
583
+   This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
584
+   FAILURE_ROUTE, BRANCH_ROUTE.
585
+
586
+   Example 1.23. is_present_hf_re usage
587
+...
588
+if (is_present_hf_re("^P-")) log(1, "There are headers starting with P-\
589
+n");
590
+...
591
+
592
+1.3.24.  append_time()
574 593
 
575 594
    Adds a time header to the reply of the request. You must use it
576 595
    before functions that are likely to send a reply, e.g., save()
... ...
@@ -589,12 +608,12 @@ if (is_present_hf("From")) log(1, "From HF Present");
589 608
    This function can be used from REQUEST_ROUTE, FAILURE_ROUTE,
590 609
    BRANCH_ROUTE.
591 610
 
592
-   Example 1.23. append_time usage
611
+   Example 1.24. append_time usage
593 612
 ...
594 613
 append_time();
595 614
 ...
596 615
 
597
-1.3.24.  is_method(name)
616
+1.3.25.  is_method(name)
598 617
 
599 618
    Check if the method of the message matches the name. If name is
600 619
    a known method (invite, cancel, ack, bye, options, info,
... ...
@@ -619,7 +638,7 @@ append_time();
619 638
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
620 639
    FAILURE_ROUTE, and BRANCH_ROUTE.
621 640
 
622
-   Example 1.24. is_method usage
641
+   Example 1.25. is_method usage
623 642
 ...
624 643
 if(is_method("INVITE"))
625 644
 {
... ...
@@ -631,7 +650,7 @@ if(is_method("OPTION|UPDATE"))
631 650
 }
632 651
 ...
633 652
 
634
-1.3.25.  remove_hf(hname)
653
+1.3.26.  remove_hf(hname)
635 654
 
636 655
    Remove from message all headers with name "hname"
637 656
 
... ...
@@ -643,7 +662,7 @@ if(is_method("OPTION|UPDATE"))
643 662
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
644 663
    FAILURE_ROUTE and BRANCH_ROUTE.
645 664
 
646
-   Example 1.25. remove_hf usage
665
+   Example 1.26. remove_hf usage
647 666
 ...
648 667
 if(remove_hf("User-Agent"))
649 668
 {
... ...
@@ -651,7 +670,7 @@ if(remove_hf("User-Agent"))
651 670
 }
652 671
 ...
653 672
 
654
-1.3.26.  remove_hf_re(re)
673
+1.3.27.  remove_hf_re(re)
655 674
 
656 675
    Remove from message all headers with name matching regular
657 676
    expression "re"
... ...
@@ -665,7 +684,7 @@ if(remove_hf("User-Agent"))
665 684
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
666 685
    FAILURE_ROUTE and BRANCH_ROUTE.
667 686
 
668
-   Example 1.26. remove_hf_re usage
687
+   Example 1.27. remove_hf_re usage
669 688
 ...
670 689
 if(remove_hf_re("^P-"))
671 690
 {
... ...
@@ -673,7 +692,7 @@ if(remove_hf_re("^P-"))
673 692
 }
674 693
 ...
675 694
 
676
-1.3.27.  has_body(), has_body(mime)
695
+1.3.28.  has_body(), has_body(mime)
677 696
 
678 697
    The function returns true if the SIP message has a body
679 698
    attached. The checked includes also the "Content-Lenght" header
... ...
@@ -689,7 +708,7 @@ if(remove_hf_re("^P-"))
689 708
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
690 709
    FAILURE_ROUTE and BRANCH_ROUTE.
691 710
 
692
-   Example 1.27. has_body usage
711
+   Example 1.28. has_body usage
693 712
 ...
694 713
 if(has_body("application/sdp"))
695 714
 {
... ...
@@ -697,7 +716,7 @@ if(has_body("application/sdp"))
697 716
 }
698 717
 ...
699 718
 
700
-1.3.28.  is_privacy(privacy_type)
719
+1.3.29.  is_privacy(privacy_type)
701 720
 
702 721
    The function returns true if the SIP message has a Privacy
703 722
    header field that includes the given privacy_type among its
... ...
@@ -708,7 +727,7 @@ if(has_body("application/sdp"))
708 727
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
709 728
    FAILURE_ROUTE and BRANCH_ROUTE.
710 729
 
711
-   Example 1.28. is_privacy usage
730
+   Example 1.29. is_privacy usage
712 731
 ...
713 732
 if(is_privacy("id"))
714 733
 {
... ...
@@ -716,7 +735,7 @@ if(is_privacy("id"))
716 735
 }
717 736
 ...
718 737
 
719
-1.3.29.  cmp_str(str1, str2)
738
+1.3.30.  cmp_str(str1, str2)
720 739
 
721 740
    The function returns true if the two parameters matches as
722 741
    string case sensitive comparison.
... ...
@@ -724,7 +743,7 @@ if(is_privacy("id"))
724 743
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
725 744
    FAILURE_ROUTE and BRANCH_ROUTE.
726 745
 
727
-   Example 1.29. cmp_str usage
746
+   Example 1.30. cmp_str usage
728 747
 ...
729 748
 if(cmp_str("$rU", "kamailio"))
730 749
 {
... ...
@@ -732,7 +751,7 @@ if(cmp_str("$rU", "kamailio"))
732 751
 }
733 752
 ...
734 753
 
735
-1.3.30.  cmp_istr(str1, str2)
754
+1.3.31.  cmp_istr(str1, str2)
736 755
 
737 756
    The function returns true if the two parameters matches as
738 757
    string case insensitive comparison.
... ...
@@ -740,7 +759,7 @@ if(cmp_str("$rU", "kamailio"))
740 759
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
741 760
    FAILURE_ROUTE and BRANCH_ROUTE.
742 761
 
743
-   Example 1.30. cmp_str usage
762
+   Example 1.31. cmp_str usage
744 763
 ...
745 764
 if(cmp_istr("$rU@you", "kamailio@YOU"))
746 765
 {
... ...
@@ -837,6 +837,36 @@ if (is_present_hf("From")) log(1, "From HF Present");
837 837
 		</example>
838 838
 	</section>
839 839
 
840
+	<section>
841
+		<title>
842
+		<function moreinfo="none">is_present_hf_re(hf_name_re)</function>
843
+		</title>
844
+		<para>
845
+		Return true if a header field whose name matches regular expression
846
+		'hf_name_re' is present in message.
847
+		</para>
848
+		<para>Meaning of the parameters is as follows:</para>
849
+		<itemizedlist>
850
+		<listitem>
851
+			<para><emphasis>hf_name_re</emphasis> - Regular expression to
852
+				match header field name.
853
+			</para>
854
+		</listitem>
855
+		</itemizedlist>
856
+		<para>
857
+		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE, 
858
+		FAILURE_ROUTE, BRANCH_ROUTE.
859
+		</para>
860
+		<example>
861
+		<title><function>is_present_hf_re</function> usage</title>
862
+		<programlisting format="linespecific">
863
+...
864
+if (is_present_hf_re("^P-")) log(1, "There are headers starting with P-\n");
865
+...
866
+</programlisting>
867
+		</example>
868
+	</section>
869
+
840 870
 	<section>
841 871
 		<title>
842 872
 		<function moreinfo="none">append_time()</function>
... ...
@@ -119,6 +119,7 @@ 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 121
 static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
122
+static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
122 123
 
123 124
 static int fixup_substre(void**, int);
124 125
 static int hname_fixup(void** param, int param_no);
... ...
@@ -192,6 +193,9 @@ static cmd_export_t cmds[]={
192 193
 	{"is_present_hf",    (cmd_function)is_present_hf_f,   1,
193 194
 		hname_fixup, free_hname_fixup,
194 195
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
196
+	{"is_present_hf_re", (cmd_function)is_present_hf_re_f,1,
197
+		fixup_regexp_null, fixup_free_regexp_null,
198
+		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
195 199
 	{"subst",            (cmd_function)subst_f,           1,
196 200
 		fixup_substre, 0,
197 201
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
... ...
@@ -985,6 +989,33 @@ static int is_present_hf_f(struct sip_msg* msg, char* str_hf, char* foo)
985 989
 	return -1;
986 990
 }
987 991
 
992
+static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo)
993
+{
994
+	struct hdr_field *hf;
995
+	regex_t *re;
996
+	regmatch_t pmatch;
997
+	char c;
998
+
999
+	re = (regex_t*)key;
1000
+
1001
+	/* we need to be sure we have seen all HFs */
1002
+	parse_headers(msg, HDR_EOH_F, 0);
1003
+	for (hf=msg->headers; hf; hf=hf->next)
1004
+	{
1005
+		c = hf->name.s[hf->name.len];
1006
+		hf->name.s[hf->name.len] = '\0';
1007
+		if (regexec(re, hf->name.s, 1, &pmatch, 0)!=0)
1008
+		{
1009
+			hf->name.s[hf->name.len] = c;
1010
+			continue;
1011
+		}
1012
+		hf->name.s[hf->name.len] = c;
1013
+		return 1;
1014
+	}
1015
+
1016
+	return -1;
1017
+}
1018
+
988 1019
 
989 1020
 static int fixup_substre(void** param, int param_no)
990 1021
 {