From 26c9c3ee6077adcf46bcb29cfa25ef17b06aae2d Mon Sep 17 00:00:00 2001
From: Sourabh Chandak <sourabh3934@gmail.com>
Date: Fri, 23 Mar 2012 11:47:33 +0530
Subject: [PATCH] Duda: fixed extra parameters issue
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------1.7.5.4"

This is a multi-part message in MIME format.
--------------1.7.5.4
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


[Updated] Duda: fixed extra parameters issue
---
 plugins/duda/duda.c          |   50 ++++++++++++++++++++++++++++++++++++++---
 plugins/duda/duda_api.c      |    5 +--
 plugins/duda/example/hello.c |   47 ++++++++++++++++++++++++---------------
 3 files changed, 77 insertions(+), 25 deletions(-)


--------------1.7.5.4
Content-Type: text/x-patch; name="0001-Duda-fixed-extra-parameters-issue.patch"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0001-Duda-fixed-extra-parameters-issue.patch"

diff --git a/plugins/duda/duda.c b/plugins/duda/duda.c
index 5906989..71ac8d5 100644
--- a/plugins/duda/duda.c
+++ b/plugins/duda/duda.c
@@ -207,12 +207,48 @@ int _mkp_init(void **api, char *confdir)
     return 0;
 }
 
+/* Retrieves the num_params field of the method invoked
+ * 
+ * Finds out the duda_method structure corresponding to the method invoked
+ * and returns the num_params field;
+ */
+short int duda_get_param_from_method(struct duda_request *dr)
+{
+    struct mk_list *head_iface, *head_method;
+    struct duda_interface *entry_iface;
+    struct duda_method *entry_method;
+    short int num_params=-1;
+
+    /* Finds the corresponding duda_method structure */
+    mk_list_foreach(head_iface, dr->web_service->map) {
+        entry_iface = mk_list_entry(head_iface, struct duda_interface, _head);
+
+        if (entry_iface->uid_len == dr->interface.len &&
+            strncmp(entry_iface->uid, dr->interface.data, dr->interface.len) == 0) {
+
+            mk_list_foreach(head_method, &entry_iface->methods) {
+                entry_method = mk_list_entry(head_method, struct duda_method, _head);
+                if (entry_method->uid_len == dr->method.len &&
+                    strncmp(entry_method->uid, dr->method.data, dr->method.len) == 0) {
+                    num_params = entry_method->num_params;
+                    break;
+                }
+            }
+            if(num_params != -1) {
+                break;
+            }
+        }
+    }
+    return num_params;    
+}
+
 int duda_request_parse(struct session_request *sr,
                        struct duda_request *dr)
 {
     short int last_field = MAP_WS_APP_NAME;
     unsigned int i = 0, len, val_len;
     int end;
+    short int method_num_params;
 
     len = sr->uri_processed.len;
 
@@ -250,10 +286,14 @@ int duda_request_parse(struct session_request *sr,
             dr->method.data    = sr->uri_processed.data + i;
             dr->method.len     = val_len;
             last_field = MAP_WS_PARAM;
+            method_num_params = duda_get_param_from_method(dr);           
             break;
         case MAP_WS_PARAM:
-            if (dr->n_params >= MAP_WS_MAX_PARAMS) {
-                PLUGIN_TRACE("too much parameters (max=%i)", MAP_WS_MAX_PARAMS);
+            if (dr->n_params >= method_num_params 
+                || dr->n_params >= MAP_WS_MAX_PARAMS) {
+                PLUGIN_TRACE("too much parameters (max=%i)", 
+                             (dr->n_params >= MAP_WS_MAX_PARAMS)?
+                             MAP_WS_MAX_PARAMS:method_num_params);
                 return -1;
             }
             dr->params[dr->n_params].data = sr->uri_processed.data + i;
@@ -346,9 +386,12 @@ int duda_service_run(struct client_session *cs,
                     break;
                 }
             }
+            if(callback) {
+                break;				
+		    }
         }
     }
-
+    
     if (!callback) {
         PLUGIN_TRACE("callback not found: '%s'", callback);
         return -1;
@@ -395,7 +438,6 @@ struct web_service *duda_get_service_from_uri(struct session_request *sr,
     return NULL;
 }
 
-
 void _mkp_exit()
 {
 
diff --git a/plugins/duda/duda_api.c b/plugins/duda/duda_api.c
index efe86f0..90745f4 100644
--- a/plugins/duda/duda_api.c
+++ b/plugins/duda/duda_api.c
@@ -126,17 +126,16 @@ int _sendfile_enqueue(duda_request_t *dr, char *path)
 /* Return ith parameter */
 char * duda_param_get(duda_request_t *dr, short int i)
 {
-    if(i >= dr->n_params){
+    if(i >= dr->n_params) {
         return NULL;
     }
-
     return mk_api->str_copy_substr(dr->params[i].data,0,(int)dr->params[i].len);
 }
 
 /* Return the total no of parameters */
 short int duda_param_count(duda_request_t *dr)
 {
-    if(!dr){
+    if(!dr) {
         return -1;
     }
     return dr->n_params;
diff --git a/plugins/duda/example/hello.c b/plugins/duda/example/hello.c
index eaaea6c..bb5b19e 100644
--- a/plugins/duda/example/hello.c
+++ b/plugins/duda/example/hello.c
@@ -4,8 +4,14 @@
 #include "packages/json/json.h"
 
 #define INCORRECT_PARAMETERS "Incorrect Parameters\n"
-#define FORMATTED "==Formatted JSON output==\n"
-#define UNFORMATTED "\n\n==Unformatted JSON output==\n"
+#define FORMATTED_OUT "==Formatted JSON output==\n"
+#define UNFORMATTED_OUT "\n\n==Unformatted JSON output==\n"
+
+/* Allowed parameter values */
+#define CREATE "create"
+#define PARSE "parse"
+#define FORMATTED "formatted"
+#define UNFORMATTED "unformatted"
 
 DUDA_REGISTER("Service Example", "service");
 
@@ -107,13 +113,13 @@ void cb_json_first(duda_request_t *dr)
     json->add_to_array(jphone, jphone2);
     json->add_to_object(jroot, "phoneNumber", jphone);
 
-    response->body_write(dr, FORMATTED, sizeof(FORMATTED)-1);
+    response->body_write(dr, FORMATTED_OUT, sizeof(FORMATTED_OUT) - 1);
     resp = json->print(jroot);
     response->body_write(dr, resp, strlen(resp));
 
     resp = NULL;
     jparse = json->parse(strparse);
-    response->body_write(dr, UNFORMATTED, sizeof(UNFORMATTED)-1);
+    response->body_write(dr, UNFORMATTED_OUT, sizeof(UNFORMATTED_OUT) - 1);
     resp = json->print_unformatted(jparse);
     json->delete(jparse);
     response->body_write(dr, resp, strlen(resp));
@@ -152,8 +158,10 @@ void cb_json_second(duda_request_t *dr){
     pvalue1 = params->get(dr, pnumber);
     pnumber = 1;
     pvalue2 = params->get(dr, pnumber);
-
-    if(strncmp(pvalue1, "create", strlen("create")) == 0 && strlen("create") == strlen(pvalue1)){
+    
+    if(!pvalue1 || !pvalue2) {
+        response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS) - 1);
+    }else if(strncmp(pvalue1, CREATE, sizeof(CREATE) - 1) == 0 && (sizeof(CREATE) - 1) == strlen(pvalue1)) {
         jroot = json->create_object();
         json->add_to_object(jroot, "name", json->create_string("Michel Perez"));
         json->add_to_object(jroot, "age", json->create_number(22.0));
@@ -175,29 +183,29 @@ void cb_json_second(duda_request_t *dr){
         json->add_to_array(jphone, jphone2);
         json->add_to_object(jroot, "phoneNumber", jphone);
 
-        if(strncmp(pvalue2, "formatted", strlen("formatted")) == 0 && strlen("formatted") == strlen(pvalue2)){
+        if(strncmp(pvalue2, FORMATTED, sizeof(FORMATTED) - 1) == 0 && (sizeof(FORMATTED) - 1) == strlen(pvalue2)) {
             resp = json->print(jroot);
             response->body_write(dr, resp, strlen(resp));
-        }else if(strncmp(pvalue2, "unformatted", strlen("unformatted")) == 0 && strlen("unformatted") == strlen(pvalue2)){
+        }else if(strncmp(pvalue2, UNFORMATTED, sizeof(UNFORMATTED) - 1) == 0 && (sizeof(UNFORMATTED) - 1) == strlen(pvalue2)) {
             resp = json->print_unformatted(jroot);
             response->body_write(dr, resp, strlen(resp));
-        }else{
-            response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS)-1);
+        }else  {
+            response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS) - 1);
         }
-    }else if(strncmp(pvalue1, "parse", strlen("parse")) == 0 && strlen("parse") == strlen(pvalue1)){
+    }else if(strncmp(pvalue1, PARSE, sizeof(PARSE) - 1) == 0 && (sizeof(PARSE) - 1) == strlen(pvalue1)) {
         jparse = json->parse(strparse);
-        if(strncmp(pvalue2, "formatted", strlen("formatted")) == 0 && strlen("formatted") == strlen(pvalue2)){
+        if(strncmp(pvalue2, FORMATTED, sizeof(FORMATTED) - 1) == 0 && (sizeof(FORMATTED) - 1) == strlen(pvalue2)) {
             resp = json->print(jparse);
             response->body_write(dr, resp, strlen(resp));
-        }else if(strncmp(pvalue2, "unformatted", strlen("unformatted")) == 0 && strlen("unformatted") == strlen(pvalue2)){
+        }else if(strncmp(pvalue2, UNFORMATTED, sizeof(UNFORMATTED) - 1) == 0 && (sizeof(UNFORMATTED) - 1) == strlen(pvalue2)) {
             resp = json->print_unformatted(jparse);
             response->body_write(dr, resp, strlen(resp));
-        }else{
-            response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS)-1);
+        }else {
+            response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS) - 1);
         }
         json->delete(jparse);
-    }else{
-        response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS)-1);
+    }else {
+        response->body_write(dr, INCORRECT_PARAMETERS, sizeof(INCORRECT_PARAMETERS) - 1);
     }
         response->end(dr, cb_end);
 }
@@ -238,7 +246,7 @@ int duda_init(struct duda_api_objects *api)
      * action: create/parse
      * format: formatted/unformatted
      */
-    method = map->method_new("json_second", "cb_json_second", 1);
+    method = map->method_new("json_second", "cb_json_second", 2);
     param = map->param_new("action", strlen("create"));
     map->method_add_param(param, method);
     param = map->param_new("format", strlen("unformatted")); 
@@ -252,5 +260,8 @@ int duda_init(struct duda_api_objects *api)
     /* Add interface to map */
     duda_service_add_interface(if_system);
 
+    if_system = map->interface_new("test");
+    duda_service_add_interface(if_system);
+    
     duda_service_ready();
 }

--------------1.7.5.4--


