Browse Source

modules/websocket: tidied up MI commands

- Fixed leak in error situations
- Improved error responses
Peter Dunkley 12 years ago
parent
commit
6c7a0f3cda
2 changed files with 32 additions and 8 deletions
  1. 17 4
      modules/websocket/ws_conn.c
  2. 15 4
      modules/websocket/ws_frame.c

+ 17 - 4
modules/websocket/ws_conn.c

@@ -440,10 +440,7 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 	int h, connections = 0, truncated = 0, order = 0, found = 0;
 	ws_connection_t *wsc;
 	struct mi_node *node = NULL;
-	struct mi_root *rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
-	
-	if (!rpl_tree)
-		return 0;
+	struct mi_root *rpl_tree;
 
 	node = cmd->node.kids;
 	if (node != NULL)
@@ -476,6 +473,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 		}
 	}
 
+	rpl_tree = init_mi_tree(200, MI_OK_S, MI_OK_LEN);
+	if (rpl_tree == NULL)
+		return 0;
+
 	WSCONN_LOCK;
 	if (order == 0)
 	{
@@ -485,7 +486,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 			while(wsc)
 			{
 				if ((found = add_node(rpl_tree, wsc)) < 0)
+				{
+					free_mi_tree(rpl_tree);
 					return 0;
+				}
 
 
 				connections += found;
@@ -508,7 +512,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 		while (wsc)
 		{
 			if ((found = add_node(rpl_tree, wsc)) < 0)
+			{
+				free_mi_tree(rpl_tree);
 				return 0;
+			}
 
 			connections += found;
 			if (connections >= MAX_WS_CONNS_DUMP)
@@ -526,7 +533,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 		while (wsc)
 		{
 			if ((found = add_node(rpl_tree, wsc)) < 0)
+			{
+				free_mi_tree(rpl_tree);
 				return 0;
+			}
 
 			connections += found;
 			if (connections >= MAX_WS_CONNS_DUMP)
@@ -544,7 +554,10 @@ struct mi_root *ws_mi_dump(struct mi_root *cmd, void *param)
 				"%d WebSocket connection%s found%s",
 				connections, connections == 1 ? "" : "s",
 				truncated == 1 ? "(truncated)" : "") == 0)
+	{
+		free_mi_tree(rpl_tree);
 		return 0;
+	}
 
 	return rpl_tree;
 }

+ 15 - 4
modules/websocket/ws_frame.c

@@ -125,6 +125,7 @@ static str str_status_too_many_params = str_init("Too many parameters");
 static str str_status_bad_param = str_init("Bad connection ID parameter");
 static str str_status_error_closing = str_init("Error closing connection");
 static str str_status_error_sending = str_init("Error sending frame");
+static str str_status_string_error = str_init("Error converting string to int");
 
 static int encode_and_send_ws_frame(ws_frame_t *frame, conn_close_t conn_close)
 {
@@ -726,7 +727,11 @@ struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
 
 	node = cmd->node.kids;
 	if (node == NULL)
-		return 0;
+	{
+		LM_WARN("no connection ID parameter\n");
+		return init_mi_tree(400, str_status_empty_param.s,
+					str_status_empty_param.len);
+	}
 	if (node->value.s == NULL || node->value.len == 0)
 	{
 		LM_WARN("empty connection ID parameter\n");
@@ -736,7 +741,8 @@ struct mi_root *ws_mi_close(struct mi_root *cmd, void *param)
 	if (str2int(&node->value, &id) < 0)
 	{
 		LM_ERR("converting string to int\n");
-		return 0;
+		return init_mi_tree(400, str_status_string_error.s,
+					str_status_string_error.len);
 	}
 	if (node->next != NULL)
 	{
@@ -772,7 +778,11 @@ static struct mi_root *mi_ping_pong(struct mi_root *cmd, void *param,
 
 	node = cmd->node.kids;
 	if (node == NULL)
-		return 0;
+	{
+		LM_WARN("no connection ID parameter\n");
+		return init_mi_tree(400, str_status_empty_param.s,
+					str_status_empty_param.len);
+	}
 	if (node->value.s == NULL || node->value.len == 0)
 	{
 		LM_WARN("empty connection ID parameter\n");
@@ -782,7 +792,8 @@ static struct mi_root *mi_ping_pong(struct mi_root *cmd, void *param,
 	if (str2int(&node->value, &id) < 0)
 	{
 		LM_ERR("converting string to int\n");
-		return 0;
+		return init_mi_tree(400, str_status_string_error.s,
+					str_status_string_error.len);
 	}
 	if (node->next != NULL)
 	{