diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-03-31 23:15:45 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-03-31 23:15:45 +0200 |
commit | c95f72616f59a317f72c58c0e5664992504a48e5 (patch) | |
tree | 9ca3029b00b5fd67434f769b7cb621b4c6741bcf /src | |
parent | 1205c90cccd3144bab24b4b5fd8dcbf0d0e6b570 (diff) |
refactoring (in progress, tbf)
Change-Id: Id9a98da85bb838b9401dad48a6652207ab4db191
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r-- | src/afb-apis.c | 5 | ||||
-rw-r--r-- | src/afb-hreq.c | 62 | ||||
-rw-r--r-- | src/afb-req-itf.h | 29 | ||||
-rw-r--r-- | src/helper-api.c | 6 | ||||
-rw-r--r-- | src/http-svc.c | 10 |
5 files changed, 89 insertions, 23 deletions
diff --git a/src/afb-apis.c b/src/afb-apis.c index ca427437..856e3f5d 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -345,12 +345,15 @@ int afb_apis_handle(struct afb_req req, const char *api, size_t lenapi, const ch const struct api_desc *a; const struct AFB_restapi *v; +//fprintf(stderr,"afb_apis_handle prefix:%.*s verb:%.*s\n",(int)lenapi,api,(int)lenverb,verb); a = apis_array; for (i = 0 ; i < apis_count ; i++, a++) { - if (a->prefixlen == lenapi && !strcasecmp(a->prefix, api)) { + if (a->prefixlen == lenapi && !strncasecmp(a->prefix, api, lenapi)) { +//fprintf(stderr,"afb_apis_handle found prefix:%.*s -> %s\n",(int)lenapi,api,a->prefix); v = a->plugin->apis; for (j = 0 ; v->name ; j++, v++) { if (!strncasecmp(v->name, verb, lenverb) && !v->name[lenverb]) { +//fprintf(stderr,"afb_apis_handle found prefix:%.*s verb:%.*s -> %s/%s\n",(int)lenapi,api,(int)lenverb,verb,a->prefix,v->name); handle(req, a, v); return 1; } diff --git a/src/afb-hreq.c b/src/afb-hreq.c index 16114675..506091cb 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -36,10 +36,12 @@ struct hreq_data { char *value; }; +static struct afb_arg getarg(struct afb_hreq *hreq, const char *name); +static void iterargs(struct afb_hreq *hreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure); + static const struct afb_req_itf afb_hreq_itf = { - .argument = (void*)afb_hreq_get_argument, - .is_argument_file = (void*)afb_hreq_is_argument_a_file, - .iterate_arguments = (void*)afb_hreq_iterate_arguments + .get = (void*)getarg, + .iterate = (void*)iterargs }; static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create) @@ -378,6 +380,60 @@ void afb_hreq_iterate_arguments(struct afb_hreq *hreq, int (*iterator)(void *clo MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)itargs, &id); } +static struct afb_arg getarg(struct afb_hreq *hreq, const char *name) +{ + struct hreq_data *hdat = get_data(hreq, name, 0); + if (hdat) + return (struct afb_arg){ + .name = hdat->key, + .value = hdat->value, + .size = hdat->length, + .is_file = (hdat->file != 0) + }; + + return (struct afb_arg){ + .name = name, + .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name), + .size = 0, + .is_file = 0 + }; +} + +struct iterdata +{ + struct afb_hreq *hreq; + int (*iterator)(void *closure, struct afb_arg arg); + void *closure; +}; + +static int _iterargs_(struct iterdata *id, enum MHD_ValueKind kind, const char *key, const char *value) +{ + if (get_data(id->hreq, key, 0)) + return 1; + return id->iterator(id->closure, (struct afb_arg){ + .name = key, + .value = value, + .size = 0, + .is_file = 0 + }); +} + +static void iterargs(struct afb_hreq *hreq, int (*iterator)(void *closure, struct afb_arg arg), void *closure) +{ + struct iterdata id = { .hreq = hreq, .iterator = iterator, .closure = closure }; + struct hreq_data *hdat = hreq->data; + while (hdat) { + if (!iterator(closure, (struct afb_arg){ + .name = hdat->key, + .value = hdat->value, + .size = hdat->length, + .is_file = (hdat->file != 0)})) + return; + hdat = hdat->next; + } + MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, &id); +} + void afb_hreq_drop_data(struct afb_hreq *hreq) { struct hreq_data *data = hreq->data; diff --git a/src/afb-req-itf.h b/src/afb-req-itf.h index d747d0b8..ab72a5ce 100644 --- a/src/afb-req-itf.h +++ b/src/afb-req-itf.h @@ -15,11 +15,16 @@ * limitations under the License. */ +struct afb_arg { + const char *name; + const char *value; + size_t size; + int is_file; +}; struct afb_req_itf { - const char *(*argument)(void *data, const char *name); - int (*is_argument_file)(void *data, const char *name); - int (*iterate_arguments)(void *data, int (*iterator)(void *closure, const char *key, const char *value, int isfile), void *closure); + struct afb_arg (*get)(void *data, const char *name); + void (*iterate)(void *data, int (*iterator)(void *closure, struct afb_arg arg), void *closure); }; struct afb_req { @@ -27,21 +32,23 @@ struct afb_req { void *data; }; -static inline const char *afb_req_argument(struct afb_req req, const char *name) +static inline struct afb_arg afb_req_get(struct afb_req req, const char *name) { - return req.itf->argument(req.data, name); + return req.itf->get(req.data, name); } -static inline int afb_req_argument_file(struct afb_req req, const char *name) +static inline const char *afb_req_argument(struct afb_req req, const char *name) { - return req.itf->is_argument_file(req.data, name); + return afb_req_get(req, name).value; } -static inline int afb_req_iterate_arguments(struct afb_req req, int (*iterator)(void *closure, const char *key, const char *value, int isfile), void *closure) +static inline int afb_req_is_argument_file(struct afb_req req, const char *name) { - return req.itf->iterate_arguments(req.data, iterator, closure); + return afb_req_get(req, name).is_file; } - - +static inline void afb_req_iterate(struct afb_req req, int (*iterator)(void *closure, struct afb_arg arg), void *closure) +{ + req.itf->iterate(req.data, iterator, closure); +} diff --git a/src/helper-api.c b/src/helper-api.c index 58015d12..fd42c482 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -53,10 +53,10 @@ const char* getQueryValue(const AFB_request * request, const char *name) { return afb_req_argument(*request->areq, name); } -static int getQueryCB (queryHandleT *query, const char *key, const char *value, int isfile) { +static int getQueryCB (queryHandleT *query, struct afb_arg arg) { if (query->idx >= query->len) return 0; - query->idx += snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", key, isfile?"FILE=":"", value); + query->idx += snprintf (&query->msg[query->idx], query->len-query->idx, " %s: %s\'%s\',", arg.name, arg.is_file?"FILE=":"", arg.value); return 1; /* continue to iterate */ } @@ -68,7 +68,7 @@ int getQueryAll(AFB_request * request, char *buffer, size_t len) { query.len = len; query.idx = 0; - afb_req_iterate_arguments(*request->areq, getQueryCB, &query); + afb_req_iterate(*request->areq, getQueryCB, &query); buffer[len-1] = 0; return query.idx >= len ? len - 1 : query.idx; } diff --git a/src/http-svc.c b/src/http-svc.c index 9fa6e84c..706abbc5 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -175,11 +175,11 @@ static int afb_hreq_rest_api(struct afb_hreq *hreq, void *data) const char *api, *verb; size_t lenapi, lenverb; - api = hreq->tail; - lenapi = strspn(api, "/"); - verb = &hreq->tail[lenapi]; - verb = &verb[strcspn(verb, "/")]; - lenverb = strspn(verb, "/"); + api = &hreq->tail[strspn(hreq->tail, "/")]; + lenapi = strcspn(api, "/"); + verb = &api[lenapi]; + verb = &verb[strspn(verb, "/")]; + lenverb = strcspn(verb, "/"); if (!(*api && *verb && lenapi && lenverb)) return 0; |