Browse code

Avoid parsing the Request URI when it is not necessary.

When @ruri.user was executed, the first select call set
the value of the URI in str format regardless of whether
or not it had already been parsed. The second function call
took the str value, and parsed the URI to get its user part.

This patch introduces a global pointer variable that can be set
to the already parsed URI being processed. Any nested select
function can check this pointer and access the parsed URI if
possible.

Miklos Tirpak authored on 15/05/2009 07:42:29
Showing 3 changed files
... ...
@@ -60,6 +60,13 @@ static select_table_t *select_list = &select_core_table;
60 60
  */
61 61
 int select_level = 0;
62 62
 
63
+/* pointer to the SIP uri beeing processed.
64
+ * Nested function calls can pass information to each
65
+ * other using this pointer. Only for performace reasons.
66
+ * (Miklos)
67
+ */
68
+struct sip_uri	*select_uri_p = NULL;
69
+
63 70
 /** parse a select identifier (internal version)
64 71
  * Parse select string into select structure s
65 72
  * moves pointer p to the first unused char.
... ...
@@ -399,6 +406,9 @@ int run_select(str* res, select_t* s, struct sip_msg* msg)
399 406
 	}
400 407
 	DBG("Calling SELECT %p \n", s->f);
401 408
 
409
+	/* reset the uri pointer */
410
+	select_uri_p = NULL;
411
+
402 412
 	/* save and restore the original select_level
403 413
 	 * because of the nested selects */
404 414
 	orig_level = select_level;
... ...
@@ -149,6 +149,13 @@ typedef struct select_table {
149 149
  */
150 150
 extern int select_level;
151 151
 
152
+/* pointer to the SIP uri beeing processed.
153
+ * Nested function calls can pass information to each
154
+ * other using this pointer. Only for performace reasons.
155
+ * (Miklos)
156
+ */
157
+extern struct sip_uri	*select_uri_p;
158
+
152 159
 /*
153 160
  * Lookup corresponding select function based on
154 161
  * the select parameters
... ...
@@ -69,6 +69,9 @@
69 69
 
70 70
 int select_ruri(str* res, select_t* s, struct sip_msg* msg)
71 71
 {
72
+	if (msg->parsed_uri_ok)
73
+		select_uri_p = &msg->parsed_uri;
74
+
72 75
 	if (msg->first_line.type==SIP_REQUEST) {
73 76
 		if(msg->new_uri.s) {
74 77
 			RETURN0_res(msg->new_uri);
... ...
@@ -94,9 +97,15 @@ int select_next_hop(str* res, select_t* s, struct sip_msg* msg)
94 97
 			RETURN0_res(msg->dst_uri);
95 98
 		}
96 99
 		else if(msg->new_uri.s) {
100
+			if (msg->parsed_uri_ok)
101
+				select_uri_p = &msg->parsed_uri;
97 102
 			RETURN0_res(msg->new_uri);
98 103
 		}
99 104
 		else {
105
+			if (msg->parsed_uri_ok)
106
+				select_uri_p = &msg->parsed_uri;
107
+			else if (msg->parsed_orig_ruri_ok)
108
+				select_uri_p = &msg->parsed_orig_ruri;
100 109
 			RETURN0_res(msg->first_line.u.request.uri);
101 110
 		}
102 111
 	}
... ...
@@ -691,30 +700,36 @@ static struct sip_uri uri;
691 700
 
692 701
 int select_uri_type(str* res, select_t* s, struct sip_msg* msg)
693 702
 {
694
-	trim(res);
695
-	if (parse_uri(res->s, res->len, &uri)<0)
696
-		return -1;
703
+	if (select_uri_p == NULL) {
704
+		trim(res);
705
+		if (parse_uri(res->s, res->len, &uri)<0)
706
+			return -1;
707
+		select_uri_p = &uri;
708
+	}
697 709
 
698
-	if (uri.type==ERROR_URI_T)
710
+	if (select_uri_p->type==ERROR_URI_T)
699 711
 		return -1;
700 712
 
701
-	uri_type_to_str(uri.type, res);
713
+	uri_type_to_str(select_uri_p->type, res);
702 714
 	return 0;
703 715
 }
704 716
 
705 717
 int select_uri_user(str* res, select_t* s, struct sip_msg* msg)
706 718
 {
707
-	if (parse_uri(res->s, res->len, &uri)<0)
708
-		return -1;
719
+	if (select_uri_p == NULL) {
720
+		if (parse_uri(res->s, res->len, &uri)<0)
721
+			return -1;
722
+		select_uri_p = &uri;
723
+	}
709 724
 
710
-	if (uri.flags & URI_USER_NORMALIZE) {
711
-		if (!(res->s=get_static_buffer(uri.user.len)))
725
+	if (select_uri_p->flags & URI_USER_NORMALIZE) {
726
+		if (!(res->s=get_static_buffer(select_uri_p->user.len)))
712 727
 			return -1;
713
-		if ((res->len=normalize_tel_user(res->s, (&uri.user)))==0)
728
+		if ((res->len=normalize_tel_user(res->s, (&select_uri_p->user)))==0)
714 729
 			return 1;
715 730
 		return 0;
716 731
 	}
717
-	RETURN0_res(uri.user);
732
+	RETURN0_res(select_uri_p->user);
718 733
 }
719 734
 
720 735
 /* search for a parameter with "name"
... ...
@@ -757,23 +772,26 @@ int select_uri_rn_user(str* res, select_t* s, struct sip_msg* msg)
757 772
 	int	ret;
758 773
 	str	val;
759 774
 
760
-	if (parse_uri(res->s, res->len, &uri)<0)
761
-		return -1;
775
+	if (select_uri_p == NULL) {
776
+		if (parse_uri(res->s, res->len, &uri)<0)
777
+			return -1;
778
+		select_uri_p = &uri;
779
+	}
762 780
 
763 781
 	/* search for the "rn" parameter */
764
-	if ((ret = search_param(&uri.params, "rn", 2, &val)) != 0)
782
+	if ((ret = search_param(&select_uri_p->params, "rn", 2, &val)) != 0)
765 783
 		goto done;
766 784
 
767
-	if (uri.sip_params.s != uri.params.s) {
785
+	if (select_uri_p->sip_params.s != select_uri_p->params.s) {
768 786
 		/* check also the original sip: URI parameters */
769
-		if ((ret = search_param(&uri.sip_params, "rn", 2, &val)) != 0)
787
+		if ((ret = search_param(&select_uri_p->sip_params, "rn", 2, &val)) != 0)
770 788
 			goto done;
771 789
 	}
772 790
 
773
-	if ((uri.flags & URI_USER_NORMALIZE) == 0)
774
-		RETURN0_res(uri.user);
791
+	if ((select_uri_p->flags & URI_USER_NORMALIZE) == 0)
792
+		RETURN0_res(select_uri_p->user);
775 793
 	/* else normalize the user name */
776
-	val = uri.user;
794
+	val = select_uri_p->user;
777 795
 done:
778 796
 	if (ret < 0)
779 797
 		return -1; /* error */
... ...
@@ -787,26 +805,35 @@ done:
787 805
 
788 806
 int select_uri_pwd(str* res, select_t* s, struct sip_msg* msg)
789 807
 {
790
-	if (parse_uri(res->s, res->len, &uri)<0)
791
-		return -1;
808
+	if (select_uri_p == NULL) {
809
+		if (parse_uri(res->s, res->len, &uri)<0)
810
+			return -1;
811
+		select_uri_p = &uri;
812
+	}
792 813
 
793
-	RETURN0_res(uri.passwd);
814
+	RETURN0_res(select_uri_p->passwd);
794 815
 }
795 816
 
796 817
 int select_uri_host(str* res, select_t* s, struct sip_msg* msg)
797 818
 {
798
-	if (parse_uri(res->s, res->len, &uri)<0)
799
-		return -1;
819
+	if (select_uri_p == NULL) {
820
+		if (parse_uri(res->s, res->len, &uri)<0)
821
+			return -1;
822
+		select_uri_p = &uri;
823
+	}
800 824
 
801
-	RETURN0_res(uri.host);
825
+	RETURN0_res(select_uri_p->host);
802 826
 }
803 827
 
804 828
 int select_uri_port(str* res, select_t* s, struct sip_msg* msg)
805 829
 {
806
-	if (parse_uri(res->s, res->len, &uri)<0)
807
-		return -1;
830
+	if (select_uri_p == NULL) {
831
+		if (parse_uri(res->s, res->len, &uri)<0)
832
+			return -1;
833
+		select_uri_p = &uri;
834
+	}
808 835
 
809
-	RETURN0_res(uri.port);
836
+	RETURN0_res(select_uri_p->port);
810 837
 }
811 838
 
812 839
 int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
... ...
@@ -814,31 +841,34 @@ int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
814 841
 	char* p;
815 842
 	int size;
816 843
 	
817
-	if (parse_uri(res->s,res->len, &uri)<0)
818
-		return -1;
844
+	if (select_uri_p == NULL) {
845
+		if (parse_uri(res->s, res->len, &uri)<0)
846
+			return -1;
847
+		select_uri_p = &uri;
848
+	}
819 849
 
820
-	if (!uri.host.len)
850
+	if (!select_uri_p->host.len)
821 851
 		return -1;
822 852
 	
823
-	if (uri.port.len) {
824
-		res->s=uri.host.s;
825
-		res->len=uri.host.len+uri.port.len+1;
853
+	if (select_uri_p->port.len) {
854
+		res->s=select_uri_p->host.s;
855
+		res->len=select_uri_p->host.len+select_uri_p->port.len+1;
826 856
 		return 0;
827 857
 	}
828 858
 	
829
-	size=uri.host.len+5;
859
+	size=select_uri_p->host.len+5;
830 860
 	if (!(p = get_static_buffer(size)))
831 861
 		return -1;
832 862
 			
833
-	strncpy(p, uri.host.s, uri.host.len);
834
-	switch (uri.type) {
863
+	strncpy(p, select_uri_p->host.s, select_uri_p->host.len);
864
+	switch (select_uri_p->type) {
835 865
 		case SIPS_URI_T:
836 866
 		case TELS_URI_T:
837
-			strncpy(p+uri.host.len, ":5061", 5); 
867
+			strncpy(p+select_uri_p->host.len, ":5061", 5); 
838 868
 			break;
839 869
 		case SIP_URI_T:
840 870
 		case TEL_URI_T:
841
-			strncpy(p+uri.host.len, ":5060", 5);
871
+			strncpy(p+select_uri_p->host.len, ":5060", 5);
842 872
 			break;
843 873
 		case ERROR_URI_T:
844 874
 			return -1;
... ...
@@ -850,13 +880,16 @@ int select_uri_hostport(str* res, select_t* s, struct sip_msg* msg)
850 880
 
851 881
 int select_uri_proto(str* res, select_t* s, struct sip_msg* msg)
852 882
 {
853
-	if (parse_uri(res->s, res->len, &uri)<0)
854
-		return -1;
883
+	if (select_uri_p == NULL) {
884
+		if (parse_uri(res->s, res->len, &uri)<0)
885
+			return -1;
886
+		select_uri_p = &uri;
887
+	}
855 888
 
856
-	if (uri.proto != PROTO_NONE) {
857
-		proto_type_to_str(uri.proto, res);
889
+	if (select_uri_p->proto != PROTO_NONE) {
890
+		proto_type_to_str(select_uri_p->proto, res);
858 891
 	} else {
859
-		switch (uri.type) {
892
+		switch (select_uri_p->type) {
860 893
 			case SIPS_URI_T:
861 894
 			case TELS_URI_T:
862 895
 				proto_type_to_str(PROTO_TLS, res);
... ...
@@ -879,20 +912,23 @@ int select_uri_params(str* res, select_t* s, struct sip_msg* msg)
879 912
 		return select_any_params(res, s, msg);
880 913
 	}
881 914
 
882
-	if (parse_uri(res->s, res->len, &uri)<0)
883
-		return -1;
915
+	if (select_uri_p == NULL) {
916
+		if (parse_uri(res->s, res->len, &uri)<0)
917
+			return -1;
918
+		select_uri_p = &uri;
919
+	}
884 920
 	
885 921
 	if (s->param_offset[select_level+1]-s->param_offset[select_level]==1)
886
-		RETURN0_res(uri.params);
922
+		RETURN0_res(select_uri_p->params);
887 923
 
888
-	*res=uri.params;
924
+	*res=select_uri_p->params;
889 925
 	ret = select_any_params(res, s, msg);
890 926
 	if ((ret < 0)
891
-		&& (uri.sip_params.s != NULL)
892
-		&& (uri.sip_params.s != uri.params.s)
927
+		&& (select_uri_p->sip_params.s != NULL)
928
+		&& (select_uri_p->sip_params.s != select_uri_p->params.s)
893 929
 	) {
894 930
 		/* Search also in the original sip: uri parameters. */
895
-		*res = uri.sip_params;
931
+		*res = select_uri_p->sip_params;
896 932
 		ret = select_any_params(res, s, msg);
897 933
 	}
898 934
 	return ret;