Skip to content

Commit

Permalink
mi_json: send 'Command not found' if mi cmd does not exist
Browse files Browse the repository at this point in the history
Closes issue #282
  • Loading branch information
razvancrainea committed Jul 31, 2014
1 parent 7396231 commit aaaf85c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 58 deletions.
11 changes: 2 additions & 9 deletions modules/mi_json/http_fnc.c
Expand Up @@ -227,10 +227,9 @@ static inline struct mi_handler* mi_json_build_async_handler(void)
return hdl;
}

struct mi_root* mi_json_run_mi_cmd(const str* miCmd, const str* params,
str *page, str *buffer, struct mi_handler **async_hdl)
struct mi_root* mi_json_run_mi_cmd(struct mi_cmd *f, const str* miCmd,
const str* params, str *page, str *buffer, struct mi_handler **async_hdl)
{
struct mi_cmd *f;
struct mi_node *node;
struct mi_root *mi_cmd;
struct mi_root *mi_rpl;
Expand All @@ -240,12 +239,6 @@ struct mi_root* mi_json_run_mi_cmd(const str* miCmd, const str* params,

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

f = lookup_mi_cmd(miCmd->s, miCmd->len);
if (f == NULL) {
LM_ERR("unable to find mi command [%.*s]\n", miCmd->len, miCmd->s);
goto error;
}

if (f->flags&MI_ASYNC_RPL_FLAG) {
LM_DBG("command=%.*s is async\n", miCmd->len, miCmd->s);
/* We need to build an async handler */
Expand Down
4 changes: 2 additions & 2 deletions modules/mi_json/http_fnc.h
Expand Up @@ -39,8 +39,8 @@ typedef struct mi_json_async_resp_data_ {
int mi_json_init_async_lock(void);
void mi_json_destroy_async_lock(void);

struct mi_root* mi_json_run_mi_cmd(const str* command, const str* params,
str *page, str *buffer, struct mi_handler **async_hdl);
struct mi_root* mi_json_run_mi_cmd(struct mi_cmd *f, const str* command,
const str* params, str *page, str *buffer, struct mi_handler **async_hdl);
int mi_json_build_page(str* page, int max_page_len,
struct mi_root* tree);

Expand Down
105 changes: 58 additions & 47 deletions modules/mi_json/mi_json.c
Expand Up @@ -50,6 +50,8 @@ static const str MI_HTTP_U_ERROR = str_init("{\"error\":"
"\"Internal server error\"}");
static const str MI_HTTP_U_METHOD = str_init("{\"error\":"
"\"Unexpected method\"}");
static const str MI_HTTP_U_NOT_FOUND = str_init("{\"error\":"
"\"Command not found\"}");


/* module parameters */
Expand Down Expand Up @@ -178,51 +180,60 @@ void mi_json_answer_to_connection (void *cls, void *connection,
size_t *upload_data_size, void **con_cls,
str *buffer, str *page)
{
str command = {NULL, 0};
str params = {NULL, 0};
struct mi_root *tree = NULL;
struct mi_handler *async_hdl;

LM_DBG("START *** cls=%p, connection=%p, url=%s, method=%s, "
"versio=%s, upload_data[%d]=%p, *con_cls=%p\n",
cls, connection, url, method, version,
(int)*upload_data_size, upload_data, *con_cls);
if (strncmp(method, "GET", 3)==0) {
if(url && url[0] == '/' && url[1] != '\0') {
command.s = (char*)url+1;
command.len = strlen(command.s);
}
httpd_api.lookup_arg(connection, "params", *con_cls, &params);
if (command.s) {
tree = mi_json_run_mi_cmd(&command,&params,
page, buffer, &async_hdl);
if (tree == NULL) {
LM_ERR("no reply\n");
*page = MI_HTTP_U_ERROR;
} else if (tree == MI_ROOT_ASYNC_RPL) {
LM_DBG("got an async reply\n");
tree = NULL;
} else {
LM_DBG("building on page [%p:%d]\n",
page->s, page->len);
if(0!=mi_json_build_page(page, buffer->len, tree)){
LM_ERR("unable to build response\n");
*page = MI_HTTP_U_ERROR;
}
}
} else {
page->s = buffer->s;
LM_ERR("unable to build response for empty request\n");
*page = MI_HTTP_U_ERROR;
}
if (tree) {
free_mi_tree(tree);
tree = NULL;
}
} else {
LM_ERR("unexpected method [%s]\n", method);
*page = MI_HTTP_U_METHOD;
}

return;
str command = {NULL, 0};
str params = {NULL, 0};
struct mi_cmd *f = NULL;
struct mi_root *tree = NULL;
struct mi_handler *async_hdl;

LM_DBG("START *** cls=%p, connection=%p, url=%s, method=%s, "
"versio=%s, upload_data[%d]=%p, *con_cls=%p\n",
cls, connection, url, method, version,
(int)*upload_data_size, upload_data, *con_cls);
if (strncmp(method, "GET", 3)==0) {
if(url && url[0] == '/' && url[1] != '\0') {
command.s = (char*)url+1;
command.len = strlen(command.s);
}
httpd_api.lookup_arg(connection, "params", *con_cls, &params);
if (command.s) {

f = lookup_mi_cmd(command.s, command.len);
if (f == NULL) {
LM_ERR("unable to find mi command [%.*s]\n", command.len, command.s);
*page = MI_HTTP_U_NOT_FOUND;
} else {

tree = mi_json_run_mi_cmd(f, &command,&params,
page, buffer, &async_hdl);
if (tree == NULL) {
LM_ERR("no reply\n");
*page = MI_HTTP_U_ERROR;
} else if (tree == MI_ROOT_ASYNC_RPL) {
LM_DBG("got an async reply\n");
tree = NULL;
} else {
LM_DBG("building on page [%p:%d]\n",
page->s, page->len);
if(0!=mi_json_build_page(page, buffer->len, tree)){
LM_ERR("unable to build response\n");
*page = MI_HTTP_U_ERROR;
}
}
}
} else {
page->s = buffer->s;
LM_ERR("unable to build response for empty request\n");
*page = MI_HTTP_U_ERROR;
}
if (tree) {
free_mi_tree(tree);
tree = NULL;
}
} else {
LM_ERR("unexpected method [%s]\n", method);
*page = MI_HTTP_U_METHOD;
}

return;
}

0 comments on commit aaaf85c

Please sign in to comment.