Hi,
txt version of the last patch attached.
On Sun, Mar 25, 2012 at 11:31 AM, Sourabh Chandak <[email protected]>wrote:
> Hi,
>
> Have made the modifications suggested by Eduardo, now there exists a
> variable of the type duda_method in duda_request which is used to compare
> the parameters count in the URL with the allowed count.
>
> On Fri, Mar 23, 2012 at 3:13 PM, Sourabh Chandak <[email protected]>wrote:
>
>> Hi,
>>
>> The webservice URL supported specifying more parameters than permissible
>> for a particular method. The restriction was laid on the number of
>> parameters globally(i.e it can't be more than MAP_WS_MAX_PARAMS). Have
>> fixed that, now the callback method will display results only when correct
>> number of parameters are specified.
>>
>> In the function *duda_service_run* in *duda.c* the outer loop iterated
>> for all the interfaces even though the corresponding callback method was
>> found. Have fixed that, possibly it will decrease time when a large number
>> of interfaces are present in a webservice.
>>
>> Further I am working on changing the way parameters are retrieved from
>> integer to char *.
>>
>> Please review the attached patch and send your comments.
>>
>> Regards,
>>
>> --
>> Sourabh Chandak
>>
>>
>>
>>
>
>
> --
> Sourabh Chandak
>
>
>
>
--
Sourabh Chandak
diff --git a/plugins/duda/duda.c b/plugins/duda/duda.c
index 5906989..60423d9 100644
--- a/plugins/duda/duda.c
+++ b/plugins/duda/duda.c
@@ -207,12 +207,52 @@ int _mkp_init(void **api, char *confdir)
return 0;
}
+/* Sets the duda_method structure variable in duda_request */
+int duda_set_invoked_method(struct duda_request *dr)
+{
+ struct mk_list *head_iface, *head_method;
+ struct duda_interface *entry_iface;
+ struct duda_method *entry_method, *invoked=NULL;
+
+ /* 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) {
+ invoked = entry_method;
+ dr->_invoked_method = entry_method;
+ break;
+ }
+ }
+ if(invoked) {
+ break;
+ }
+ }
+ }
+
+ if(!invoked) {
+ PLUGIN_TRACE("Invoked method not found");
+ return -1;
+ }
+
+ PLUGIN_TRACE("Method %s invoked", entry_method->uid);
+ return 0;
+}
+
+
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 allowed_params;
len = sr->uri_processed.len;
@@ -250,10 +290,16 @@ 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;
+ if(duda_set_invoked_method(dr) == -1) {
+ return -1;
+ }
+ allowed_params = dr->_invoked_method->num_params;
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 >= MAP_WS_MAX_PARAMS || dr->n_params >=
allowed_params) {
+ PLUGIN_TRACE("too much parameters (max=%i)",
+ (dr->n_params >= MAP_WS_MAX_PARAMS)?
+ MAP_WS_MAX_PARAMS:allowed_params);
return -1;
}
dr->params[dr->n_params].data = sr->uri_processed.data + i;
@@ -269,6 +315,11 @@ int duda_request_parse(struct session_request *sr,
if (last_field < MAP_WS_METHOD) {
return -1;
}
+
+ if ((dr->n_params) != allowed_params) {
+ PLUGIN_TRACE("%i parameters required", allowed_params);
+ return -1;
+ }
return 0;
}
@@ -297,9 +348,6 @@ int duda_service_run(struct client_session *cs,
struct web_service *web_service)
{
struct duda_request *dr;
- struct mk_list *head_iface, *head_method;
- struct duda_interface *entry_iface;
- struct duda_method *entry_method;
void *(*callback) (duda_request_t *) = NULL;
dr = mk_api->mem_alloc(sizeof(struct duda_request));
@@ -314,6 +362,9 @@ int duda_service_run(struct client_session *cs,
dr->cs = cs;
dr->sr = sr;
+ /* method invoked */
+ dr->_invoked_method = NULL;
+
/* callbacks */
dr->end_callback = NULL;
@@ -330,32 +381,15 @@ int duda_service_run(struct client_session *cs,
return -1;
}
- /* Invoke the web service callback */
- 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) {
-
- /* try to match method */
- 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) {
- callback = entry_method->func_cb;
- break;
- }
- }
- }
- }
+ callback = dr->_invoked_method->func_cb;
if (!callback) {
- PLUGIN_TRACE("callback not found: '%s'", callback);
+ PLUGIN_TRACE("callback not found");
return -1;
}
- PLUGIN_TRACE("CB %s()", entry_method->callback);
- entry_method->func_cb(dr);
+ PLUGIN_TRACE("CB %s()", dr->_invoked_method->callback);
+ dr->_invoked_method->func_cb(dr);
return 0;
}
diff --git a/plugins/duda/duda.h b/plugins/duda/duda.h
index b257fd1..7761dba 100644
--- a/plugins/duda/duda.h
+++ b/plugins/duda/duda.h
@@ -53,6 +53,9 @@ struct duda_request {
struct client_session *cs;
struct session_request *sr;
+ /* Method structure */
+ struct duda_method *_invoked_method;
+
/* Callback functions */
void (*end_callback)(duda_request_t *);
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();
}
_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey