@@ -50,6 +50,8 @@ static const str MI_HTTP_U_ERROR = str_init("{\"error\":"
5050"\"Internal server error\"}" );
5151static 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