Skip to content

Commit aaaf85c

Browse files
committed
mi_json: send 'Command not found' if mi cmd does not exist
Closes issue #282
1 parent 7396231 commit aaaf85c

File tree

3 files changed

+62
-58
lines changed

3 files changed

+62
-58
lines changed

modules/mi_json/http_fnc.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,9 @@ static inline struct mi_handler* mi_json_build_async_handler(void)
227227
return hdl;
228228
}
229229

230-
struct mi_root* mi_json_run_mi_cmd(const str* miCmd, const str* params,
231-
str *page, str *buffer, struct mi_handler **async_hdl)
230+
struct mi_root* mi_json_run_mi_cmd(struct mi_cmd *f, const str* miCmd,
231+
const str* params, str *page, str *buffer, struct mi_handler **async_hdl)
232232
{
233-
struct mi_cmd *f;
234233
struct mi_node *node;
235234
struct mi_root *mi_cmd;
236235
struct mi_root *mi_rpl;
@@ -240,12 +239,6 @@ struct mi_root* mi_json_run_mi_cmd(const str* miCmd, const str* params,
240239

241240
LM_DBG("got command=%.*s\n", miCmd->len, miCmd->s);
242241

243-
f = lookup_mi_cmd(miCmd->s, miCmd->len);
244-
if (f == NULL) {
245-
LM_ERR("unable to find mi command [%.*s]\n", miCmd->len, miCmd->s);
246-
goto error;
247-
}
248-
249242
if (f->flags&MI_ASYNC_RPL_FLAG) {
250243
LM_DBG("command=%.*s is async\n", miCmd->len, miCmd->s);
251244
/* We need to build an async handler */

modules/mi_json/http_fnc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ typedef struct mi_json_async_resp_data_ {
3939
int mi_json_init_async_lock(void);
4040
void mi_json_destroy_async_lock(void);
4141

42-
struct mi_root* mi_json_run_mi_cmd(const str* command, const str* params,
43-
str *page, str *buffer, struct mi_handler **async_hdl);
42+
struct mi_root* mi_json_run_mi_cmd(struct mi_cmd *f, const str* command,
43+
const str* params, str *page, str *buffer, struct mi_handler **async_hdl);
4444
int mi_json_build_page(str* page, int max_page_len,
4545
struct mi_root* tree);
4646

modules/mi_json/mi_json.c

Lines changed: 58 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ static const str MI_HTTP_U_ERROR = str_init("{\"error\":"
5050
"\"Internal server error\"}");
5151
static const str MI_HTTP_U_METHOD = str_init("{\"error\":"
5252
"\"Unexpected method\"}");
53+
static const str MI_HTTP_U_NOT_FOUND = str_init("{\"error\":"
54+
"\"Command not found\"}");
5355

5456

5557
/* module parameters */
@@ -178,51 +180,60 @@ void mi_json_answer_to_connection (void *cls, void *connection,
178180
size_t *upload_data_size, void **con_cls,
179181
str *buffer, str *page)
180182
{
181-
str command = {NULL, 0};
182-
str params = {NULL, 0};
183-
struct mi_root *tree = NULL;
184-
struct mi_handler *async_hdl;
185-
186-
LM_DBG("START *** cls=%p, connection=%p, url=%s, method=%s, "
187-
"versio=%s, upload_data[%d]=%p, *con_cls=%p\n",
188-
cls, connection, url, method, version,
189-
(int)*upload_data_size, upload_data, *con_cls);
190-
if (strncmp(method, "GET", 3)==0) {
191-
if(url && url[0] == '/' && url[1] != '\0') {
192-
command.s = (char*)url+1;
193-
command.len = strlen(command.s);
194-
}
195-
httpd_api.lookup_arg(connection, "params", *con_cls, &params);
196-
if (command.s) {
197-
tree = mi_json_run_mi_cmd(&command,&params,
198-
page, buffer, &async_hdl);
199-
if (tree == NULL) {
200-
LM_ERR("no reply\n");
201-
*page = MI_HTTP_U_ERROR;
202-
} else if (tree == MI_ROOT_ASYNC_RPL) {
203-
LM_DBG("got an async reply\n");
204-
tree = NULL;
205-
} else {
206-
LM_DBG("building on page [%p:%d]\n",
207-
page->s, page->len);
208-
if(0!=mi_json_build_page(page, buffer->len, tree)){
209-
LM_ERR("unable to build response\n");
210-
*page = MI_HTTP_U_ERROR;
211-
}
212-
}
213-
} else {
214-
page->s = buffer->s;
215-
LM_ERR("unable to build response for empty request\n");
216-
*page = MI_HTTP_U_ERROR;
217-
}
218-
if (tree) {
219-
free_mi_tree(tree);
220-
tree = NULL;
221-
}
222-
} else {
223-
LM_ERR("unexpected method [%s]\n", method);
224-
*page = MI_HTTP_U_METHOD;
225-
}
226-
227-
return;
183+
str command = {NULL, 0};
184+
str params = {NULL, 0};
185+
struct mi_cmd *f = NULL;
186+
struct mi_root *tree = NULL;
187+
struct mi_handler *async_hdl;
188+
189+
LM_DBG("START *** cls=%p, connection=%p, url=%s, method=%s, "
190+
"versio=%s, upload_data[%d]=%p, *con_cls=%p\n",
191+
cls, connection, url, method, version,
192+
(int)*upload_data_size, upload_data, *con_cls);
193+
if (strncmp(method, "GET", 3)==0) {
194+
if(url && url[0] == '/' && url[1] != '\0') {
195+
command.s = (char*)url+1;
196+
command.len = strlen(command.s);
197+
}
198+
httpd_api.lookup_arg(connection, "params", *con_cls, &params);
199+
if (command.s) {
200+
201+
f = lookup_mi_cmd(command.s, command.len);
202+
if (f == NULL) {
203+
LM_ERR("unable to find mi command [%.*s]\n", command.len, command.s);
204+
*page = MI_HTTP_U_NOT_FOUND;
205+
} else {
206+
207+
tree = mi_json_run_mi_cmd(f, &command,&params,
208+
page, buffer, &async_hdl);
209+
if (tree == NULL) {
210+
LM_ERR("no reply\n");
211+
*page = MI_HTTP_U_ERROR;
212+
} else if (tree == MI_ROOT_ASYNC_RPL) {
213+
LM_DBG("got an async reply\n");
214+
tree = NULL;
215+
} else {
216+
LM_DBG("building on page [%p:%d]\n",
217+
page->s, page->len);
218+
if(0!=mi_json_build_page(page, buffer->len, tree)){
219+
LM_ERR("unable to build response\n");
220+
*page = MI_HTTP_U_ERROR;
221+
}
222+
}
223+
}
224+
} else {
225+
page->s = buffer->s;
226+
LM_ERR("unable to build response for empty request\n");
227+
*page = MI_HTTP_U_ERROR;
228+
}
229+
if (tree) {
230+
free_mi_tree(tree);
231+
tree = NULL;
232+
}
233+
} else {
234+
LM_ERR("unexpected method [%s]\n", method);
235+
*page = MI_HTTP_U_METHOD;
236+
}
237+
238+
return;
228239
}

0 commit comments

Comments
 (0)