2 * Copyright (C) 2016 "IoT.bzh"
3 * Author "Fulup Ar Foll"
4 * Author José Bollo <jose.bollo@iot.bzh>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Contain all generic part to handle REST/API
21 * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
33 #include <sys/types.h>
37 #include <sys/syscall.h>
40 #include "afb-plugin.h"
41 #include "afb-req-itf.h"
42 #include "afb-poll-itf.h"
47 #include "utils-upoll.h"
55 static struct api_desc *apis_array = NULL;
56 static int apis_count = 0;
63 void afb_apis_free_context(int apiidx, void *context)
65 const struct afb_api *api;
66 api = &apis_array[apiidx].api;
67 api->free_context(api->closure, context);
70 int afb_apis_add(const char *name, struct afb_api api)
72 struct api_desc *apis;
76 /* check existing or not */
79 fprintf(stderr, "empty api name forbidden\n");
83 /* check previously existing plugin */
84 for (i = 0 ; i < apis_count ; i++) {
85 if (!strcasecmp(apis_array[i].name, name)) {
86 fprintf(stderr, "ERROR: api of name %s already exists\n", name);
91 /* allocates enough memory */
92 apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
94 fprintf(stderr, "out of memory\n");
99 /* record the plugin */
100 apis = &apis_array[apis_count];
112 void afb_apis_call(struct afb_req req, struct AFB_clientCtx *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
115 const struct api_desc *a;
118 for (i = 0 ; i < apis_count ; i++, a++) {
119 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
120 req.context = &context->contexts[i];
121 a->api.call(a->api.closure, req, verb, lenverb);
125 afb_req_fail(req, "fail", "api not found");