diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-04-01 17:00:47 +0200 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-04-01 17:00:47 +0200 |
commit | dde70b62b09f49ad672c104a3f81714bf11047be (patch) | |
tree | de0af0b927b30dd612cb95f2d6095379a65ff38e /src | |
parent | bbe18a624f4961165cf52d7f4c25de6f3a7ec012 (diff) |
work in progress (tbf)
Change-Id: I01f72892530bb4ef14a7216a112812026a367bfa
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src')
-rw-r--r-- | src/afb-apis.c | 36 | ||||
-rw-r--r-- | src/afb-hreq.c | 4 | ||||
-rw-r--r-- | src/afb-req-itf.h | 104 | ||||
-rw-r--r-- | src/helper-api.c | 1 |
4 files changed, 18 insertions, 127 deletions
diff --git a/src/afb-apis.c b/src/afb-apis.c index effef8a9..8fbd9fc9 100644 --- a/src/afb-apis.c +++ b/src/afb-apis.c @@ -37,7 +37,7 @@ #include <sys/syscall.h> #include <setjmp.h> -#include "../include/local-def.h" +#include "local-def.h" #include "afb-req-itf.h" #include "afb-apis.h" @@ -49,6 +49,7 @@ struct api_desc { void *handle; /* context of dlopen */ }; +static int api_timeout = 15; static struct api_desc *apis_array = NULL; static int apis_count = 0; @@ -285,9 +286,9 @@ int afb_apis_add_pathset(const char *pathset) // Check of apiurl is declare in this plugin and call it extern __thread sigjmp_buf *error_handler; -static int callPluginApi(AFB_request * request) +static void trapping_handle(AFB_request * request, struct json_object *(*cb)(AFB_request *,void*)) { - volatile int status, timerset; + volatile int signum, timerset; timer_t timerid; sigjmp_buf jmpbuf, *older; struct sigevent sevp; @@ -296,43 +297,38 @@ static int callPluginApi(AFB_request * request) // save context before calling the API timerset = 0; older = error_handler; - status = setjmp(jmpbuf); - if (status != 0) { - status = 0; + signum = setjmp(jmpbuf); + if (signum != 0) { + afb_req_fail_f(*request->areq, "aborted", "signal %d caught", signum); } else { error_handler = &jmpbuf; - if (request->config->apiTimeout > 0) { + if (api_timeout > 0) { timerset = 1; /* TODO: check statuses */ sevp.sigev_notify = SIGEV_THREAD_ID; sevp.sigev_signo = SIGALRM; #if defined(sigev_notify_thread_id) - sevp.sigev_notify_thread_id = syscall(SYS_gettid); + sevp.sigev_notify_thread_id = (pid_t)syscall(SYS_gettid); #else - sevp._sigev_un._tid = syscall(SYS_gettid); + sevp._sigev_un._tid = (pid_t)syscall(SYS_gettid); #endif timer_create(CLOCK_THREAD_CPUTIME_ID, &sevp, &timerid); its.it_interval.tv_sec = 0; its.it_interval.tv_nsec = 0; - its.it_value.tv_sec = 15; + its.it_value.tv_sec = api_timeout; its.it_value.tv_nsec = 0; timer_settime(timerid, 0, &its, NULL); } - //doCallPluginApi(request, apiidx, verbidx, context); - status = 1; + cb(request, NULL); } if (timerset) timer_delete(timerid); error_handler = older; - - return status; } static void handle(struct afb_req req, const struct api_desc *api, const struct AFB_restapi *verb) { - json_object *jresp, *jcall, *jreqt; - AFB_request request; request.uuid = request.url = "fake"; @@ -357,7 +353,7 @@ static void handle(struct afb_req req, const struct api_desc *api, const struct default: break; } - verb->callback(&request, NULL); + trapping_handle(&request, verb->callback); if (verb->session == AFB_SESSION_CLOSE) /*close*/; @@ -369,20 +365,18 @@ 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 && !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; } } - break; + afb_req_fail_f(req, "unknown-verb", "verb %.*s unknown within api %s", (int)lenverb, verb, a->prefix); + return 1; } } return 0; diff --git a/src/afb-hreq.c b/src/afb-hreq.c index 58dbb04e..e146bcb4 100644 --- a/src/afb-hreq.c +++ b/src/afb-hreq.c @@ -32,6 +32,8 @@ #include "afb-req-itf.h" #include "afb-hreq.h" +#define SIZE_RESPONSE_BUFFER 8000 + static char empty_string[] = ""; struct hreq_data { @@ -460,7 +462,7 @@ static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *statu if (resp) json_object_object_add(root, "response", resp); - response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, 65500, (void*)send_json_cb, root, (void*)json_object_put); + response = MHD_create_response_from_callback(MHD_SIZE_UNKNOWN, SIZE_RESPONSE_BUFFER, (void*)send_json_cb, root, (void*)json_object_put); MHD_queue_response(hreq->connection, retcode, response); MHD_destroy_response(response); } diff --git a/src/afb-req-itf.h b/src/afb-req-itf.h deleted file mode 100644 index eea78317..00000000 --- a/src/afb-req-itf.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2016 IoT.bzh - * Author: José Bollo <jose.bollo@iot.bzh> - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -struct afb_arg { - const char *name; - const char *value; - size_t size; - int is_file; -}; - -struct afb_req_itf { - struct afb_arg (*get)(void *data, const char *name); - void (*iterate)(void *data, int (*iterator)(void *closure, struct afb_arg arg), void *closure); - void (*fail)(void *data, const char *status, const char *info); - void (*success)(void *data, json_object *obj, const char *info); -}; - -struct afb_req { - const struct afb_req_itf *itf; - void *data; -}; - -static inline struct afb_arg afb_req_get(struct afb_req req, const char *name) -{ - return req.itf->get(req.data, name); -} - -static inline const char *afb_req_argument(struct afb_req req, const char *name) -{ - return afb_req_get(req, name).value; -} - -static inline int afb_req_is_argument_file(struct afb_req req, const char *name) -{ - 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); -} - -#include <stdarg.h> -#include <stdlib.h> -#include <stdio.h> - -static inline void afb_req_fail(struct afb_req req, const char *status, const char *info) -{ - req.itf->fail(req.data, status, info); -} - -static inline void afb_req_fail_v(struct afb_req req, const char *status, const char *info, va_list args) -{ - char *message; - if (info == NULL || vasprintf(&message, info, args) < 0) - message = NULL; - afb_req_fail(req, status, message); - free(message); -} - -static inline void afb_req_fail_f(struct afb_req req, const char *status, const char *info, ...) -{ - va_list args; - va_start(args, info); - afb_req_fail_v(req, status, info, args); - va_end(args); -} - -static inline void afb_req_success(struct afb_req req, json_object *obj, const char *info) -{ - req.itf->success(req.data, obj, info); -} - -static inline void afb_req_success_v(struct afb_req req, json_object *obj, const char *info, va_list args) -{ - char *message; - if (info == NULL || vasprintf(&message, info, args) < 0) - message = NULL; - afb_req_success(req, obj, message); - free(message); -} - -static inline void afb_req_success_f(struct afb_req req, json_object *obj, const char *info, ...) -{ - va_list args; - va_start(args, info); - afb_req_success_v(req, obj, info, args); - va_end(args); -} - diff --git a/src/helper-api.c b/src/helper-api.c index 1e3d8820..9d1ec0b7 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -54,7 +54,6 @@ PUBLIC int verbose; static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success", "done", "unauth"}; - // Helper to retrieve argument from connection const char* getQueryValue(const AFB_request * request, const char *name) { return afb_req_argument(*request->areq, name); |