From c95f72616f59a317f72c58c0e5664992504a48e5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Thu, 31 Mar 2016 23:15:45 +0200 Subject: [PATCH] refactoring (in progress, tbf) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Id9a98da85bb838b9401dad48a6652207ab4db191 Signed-off-by: José Bollo --- plugins/CMakeLists.txt | 8 +++--- plugins/samples/CMakeLists.txt | 10 +++---- src/afb-apis.c | 5 +++- src/afb-hreq.c | 62 ++++++++++++++++++++++++++++++++++++++++-- src/afb-req-itf.h | 29 ++++++++++++-------- src/helper-api.c | 6 ++-- src/http-svc.c | 10 +++---- test/client-ctx.html | 9 ++++++ test/hello-world.html | 12 ++++++++ test/index.html | 10 +++++++ test/sample-post.html | 10 +++++++ test/websock.html | 47 ++++++++++++++++++++++++++++++++ 12 files changed, 186 insertions(+), 32 deletions(-) create mode 100644 test/client-ctx.html create mode 100644 test/hello-world.html create mode 100644 test/index.html create mode 100644 test/sample-post.html create mode 100644 test/websock.html diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index ba3432b4..5d8b26ac 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,6 +1,6 @@ -ADD_SUBDIRECTORY(afm-main-plugin) +#ADD_SUBDIRECTORY(afm-main-plugin) ADD_SUBDIRECTORY(session) ADD_SUBDIRECTORY(samples) -ADD_SUBDIRECTORY(audio) -ADD_SUBDIRECTORY(radio) -ADD_SUBDIRECTORY(media) +#ADD_SUBDIRECTORY(audio) +#ADD_SUBDIRECTORY(radio) +#ADD_SUBDIRECTORY(media) diff --git a/plugins/samples/CMakeLists.txt b/plugins/samples/CMakeLists.txt index c3c305ca..24ffd91b 100644 --- a/plugins/samples/CMakeLists.txt +++ b/plugins/samples/CMakeLists.txt @@ -6,11 +6,11 @@ TARGET_LINK_LIBRARIES(helloWorld-api ${link_libraries}) INSTALL(TARGETS helloWorld-api LIBRARY DESTINATION ${plugin_install_dir}) -ADD_LIBRARY(samplePost-api MODULE SamplePost.c) -SET_TARGET_PROPERTIES(samplePost-api PROPERTIES PREFIX "") -TARGET_LINK_LIBRARIES(samplePost-api ${link_libraries}) -INSTALL(TARGETS samplePost-api - LIBRARY DESTINATION ${plugin_install_dir}) +#ADD_LIBRARY(samplePost-api MODULE SamplePost.c) +#SET_TARGET_PROPERTIES(samplePost-api PROPERTIES PREFIX "") +#TARGET_LINK_LIBRARIES(samplePost-api ${link_libraries}) +#INSTALL(TARGETS samplePost-api +# LIBRARY DESTINATION ${plugin_install_dir}) ADD_LIBRARY(clientCtx-api MODULE ClientCtx.c) SET_TARGET_PROPERTIES(clientCtx-api PROPERTIES PREFIX "") 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; diff --git a/test/client-ctx.html b/test/client-ctx.html new file mode 100644 index 00000000..26f4bcfb --- /dev/null +++ b/test/client-ctx.html @@ -0,0 +1,9 @@ + + + Client Ctx test + +

Client Ctx test

+
    +
  1. create +
  2. check +
  3. close diff --git a/test/hello-world.html b/test/hello-world.html new file mode 100644 index 00000000..88f2c95b --- /dev/null +++ b/test/hello-world.html @@ -0,0 +1,12 @@ + + + Hello world test + +

    Hello world test

    +
      +
    1. ping +
    2. ping null +
    3. ping bug +
    4. ping json +
    5. not a verb +
    6. not an api diff --git a/test/index.html b/test/index.html new file mode 100644 index 00000000..66b36190 --- /dev/null +++ b/test/index.html @@ -0,0 +1,10 @@ + + + afb-daemon test + +

      afb-daemon test

      +
        +
      1. Hello World! +
      2. client context +
      3. Sample post +
      4. websockets diff --git a/test/sample-post.html b/test/sample-post.html new file mode 100644 index 00000000..c9decdcb --- /dev/null +++ b/test/sample-post.html @@ -0,0 +1,10 @@ + + + Sample Post test + +

        Sample Post test

        +
          +
        1. upload json +
        2. upload json +
        3. upload json +
        4. upload json diff --git a/test/websock.html b/test/websock.html new file mode 100644 index 00000000..1db33b2a --- /dev/null +++ b/test/websock.html @@ -0,0 +1,47 @@ + + + + WebSocket Echo + + + +

          WebSocket Echo

          +
          Not Connected
          + + + -- 2.16.6