Browse code

lost: error response fix in lost_function(), lost_held_dereference() and lost_held_function()

- error variable was set to 0 (clean up) before return. Lost functions now returns 500
in case of LOST/HELD server error, or 400 in case of internal error, otherwise 2xx.

Wolfgang Kampichler authored on 02/05/2021 15:28:44
Showing 3 changed files
... ...
@@ -211,6 +211,7 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url,
211 211
 	int flag = 0;
212 212
 	int naptr = 0;
213 213
 	int presence = 0;
214
+	int res_error = 0;
214 215
 
215 216
 	if(_pidf == NULL || _url == NULL || _err == NULL) {
216 217
 		LM_ERR("invalid parameter\n");
... ...
@@ -505,6 +506,10 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url,
505 506
 	psurl->setf(_m, &psurl->pvp, (int)EQ_T, &pvurl);
506 507
 	lost_free_string(&geo); /* clean up */
507 508
  
509
+ 	/* return error code in case of response error */
510
+	if(err.len > 0) {
511
+		res_error = 1;
512
+	}
508 513
 	pverr.rs = err;
509 514
 	pverr.rs.s = err.s;
510 515
 	pverr.rs.len = err.len;
... ...
@@ -514,7 +519,7 @@ int lost_held_function(struct sip_msg *_m, char *_con, char *_pidf, char *_url,
514 519
 	pserr->setf(_m, &pserr->pvp, (int)EQ_T, &pverr);
515 520
 	lost_free_string(&err); /* clean up */
516 521
 
517
-	return (err.len > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
522
+	return (res_error > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
518 523
 
519 524
 err:
520 525
 	/* clean up pointer */
... ...
@@ -574,6 +579,7 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf,
574 579
 	int len = 0;
575 580
 	int curl = 0;
576 581
 	int exact = 0;
582
+	int ret = LOST_SUCCESS;
577 583
 
578 584
 	if(_url == NULL || _rtime == NULL || _pidf == NULL || _rtype == NULL
579 585
 			|| _err == NULL) {
... ...
@@ -709,12 +715,20 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf,
709 715
 		goto err;
710 716
 	}
711 717
 
712
-	/* check the root element ... shall be locationResponse, or error */
713
-	if(xmlStrcmp(root->name, (const xmlChar *)"presence") == 0) {
718
+	/* check root element ... shall be presence|locationResponse, or error */
719
+	if((!xmlStrcmp(root->name, (const xmlChar *)"presence"))
720
+			|| (!xmlStrcmp(root->name, (const xmlChar *)"locationResponse"))) {
714 721
 
715 722
 		LM_DBG("HELD location response [%.*s]\n", res.len, res.s);
716 723
 
717
-		/* error received */
724
+		/* check content and set response code
725
+		 * + 0 nothing found: return 200
726
+		 * + 1 value found: return 201
727
+		 * + 2 reference found: return 202
728
+		 * + 3 value and reference found: return 203
729
+		 */
730
+		ret += lost_check_HeldResponse(root);
731
+	/* error received */
718 732
 	} else if(xmlStrcmp(root->name, (const xmlChar *)"error") == 0) {
719 733
 
720 734
 		LM_DBG("HELD error response [%.*s]\n", res.len, res.s);
... ...
@@ -745,6 +759,10 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf,
745 759
 	pspidf->setf(_m, &pspidf->pvp, (int)EQ_T, &pvpidf);
746 760
 	lost_free_string(&res); /* clean up */
747 761
 
762
+	/* return error code in case of response error */
763
+	if(err.len > 0) {
764
+		ret = LOST_SERVER_ERROR;
765
+	}
748 766
 	pverr.rs = err;
749 767
 	pverr.rs.s = err.s;
750 768
 	pverr.rs.len = err.len;
... ...
@@ -754,7 +772,7 @@ int lost_held_dereference(struct sip_msg *_m, char *_url, char *_pidf,
754 772
 	pserr->setf(_m, &pserr->pvp, (int)EQ_T, &pverr);
755 773
 	lost_free_string(&err); /* clean up */
756 774
 
757
-	return (err.len > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
775
+	return ret;
758 776
 
759 777
 err:
760 778
 	/* clean up xml */
... ...
@@ -822,6 +840,7 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name,
822 840
 	int len = 0;
823 841
 	int naptr = 0;
824 842
 	int geoitems = 0;
843
+	int res_error = 0;
825 844
 
826 845
 	if(_con == NULL || _uri == NULL || _name == NULL || _err == NULL) {
827 846
 		LM_ERR("invalid parameter\n");
... ...
@@ -1253,6 +1272,10 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name,
1253 1272
 	psuri->setf(_m, &psuri->pvp, (int)EQ_T, &pvuri);
1254 1273
 	lost_free_string(&uri); /* clean up */
1255 1274
 
1275
+	/* return error code in case of response error */
1276
+	if(err.len > 0) {
1277
+		res_error = 1;
1278
+	}
1256 1279
 	pverr.rs = err;
1257 1280
 	pverr.rs.s = err.s;
1258 1281
 	pverr.rs.len = err.len;
... ...
@@ -1262,7 +1285,7 @@ int lost_function(struct sip_msg *_m, char *_con, char *_uri, char *_name,
1262 1285
 	pserr->setf(_m, &pserr->pvp, (int)EQ_T, &pverr);
1263 1286
 	lost_free_string(&err); /* clean up */
1264 1287
 
1265
-	return (err.len > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
1288
+	return (res_error > 0) ? LOST_SERVER_ERROR : LOST_SUCCESS;
1266 1289
 
1267 1290
 err:
1268 1291
 	/* clean up */
... ...
@@ -1000,4 +1000,34 @@ p_lost_fsr_t lost_parse_findServiceResponse(str ret)
1000 1000
 	xmlFreeDoc(doc); /* clean up */
1001 1001
 
1002 1002
 	return res;
1003
+}
1004
+
1005
+/*
1006
+ * lost_check_HeldResponse(node)
1007
+ * does a quick check of HELD dereference response and returns ...
1008
+ * 0: neither location value nor reference found
1009
+ * 1: location value found
1010
+ * 2: location reference found
1011
+ * 3: location value and reference found
1012
+ * multiple occurences are ignored
1013
+ */
1014
+int lost_check_HeldResponse(xmlNodePtr node)
1015
+{
1016
+	char *tmp = NULL;
1017
+
1018
+	int ret = 0; /* response error */
1019
+
1020
+	tmp = xmlNodeGetNodeContentByName(node, "location-info", NULL);
1021
+	if(tmp != NULL) {
1022
+		ret += HELD_RESPONSE_VALUE; /* LocByVal: civic or geodetic */
1023
+	}
1024
+	xmlFree(tmp); /* clean up */
1025
+
1026
+	tmp = xmlNodeGetNodeContentByName(node, "locationURI", NULL);
1027
+	if(tmp != NULL) {
1028
+		ret += HELD_RESPONSE_REFERENCE; /* LocByRef: reference */
1029
+	}
1030
+	xmlFree(tmp); /* clean up */
1031
+
1032
+	return ret;
1003 1033
 }
1004 1034
\ No newline at end of file
... ...
@@ -56,6 +56,9 @@
56 56
 
57 57
 #define ERRORS_NODE (const char *)"errors"
58 58
 
59
+#define HELD_RESPONSE_VALUE 1
60
+#define HELD_RESPONSE_REFERENCE 2
61
+
59 62
 typedef struct lost_list
60 63
 {
61 64
 	char *value;
... ...
@@ -114,6 +117,8 @@ typedef struct lost_fsr
114 117
 
115 118
 /* read and parse response data */
116 119
 p_lost_fsr_t lost_parse_findServiceResponse(str);
120
+/* check response to dereferece request */
121
+int lost_check_HeldResponse(xmlNodePtr); 
117 122
 /* print the response */
118 123
 void lost_print_findServiceResponse(p_lost_fsr_t);
119 124
 /* remove response data from memory */