e8e7ced355aa283735a7f9618bde805f34b3eaae
[src/app-framework-binder.git] / src / afb-apis.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  * Author José Bollo <jose.bollo@iot.bzh>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24
25 #include "session.h"
26 #include "verbose.h"
27 #include "afb-apis.h"
28 #include "afb-context.h"
29 #include <afb/afb-req-itf.h>
30
31 struct api_desc {
32         struct afb_api api;
33         const char *name;
34         size_t namelen;
35 };
36
37 static struct api_desc *apis_array = NULL;
38 static int apis_count = 0;
39
40 int afb_apis_count()
41 {
42         return apis_count;
43 }
44
45 int afb_apis_is_valid_api_name(const char *name)
46 {
47         unsigned char c;
48
49         c = (unsigned char)*name;
50         if (c == 0)
51                 return 0;
52         do {
53                 if (c < (unsigned char)'\x80') {
54                         switch(c) {
55                         default:
56                                 if (c > ' ')
57                                         break;
58                         case '"':
59                         case '#':
60                         case '%':
61                         case '&':
62                         case '\'':
63                         case '/':
64                         case '?':
65                         case '`':
66                         case '\\':
67                         case '\x7f':
68                                 return 0;
69                         }
70                 }
71                 c = (unsigned char)*++name;
72         } while(c != 0);
73         return 1;
74 }
75
76 int afb_apis_add(const char *name, struct afb_api api)
77 {
78         struct api_desc *apis;
79         int i;
80
81         /* Checks the api name */
82         if (!afb_apis_is_valid_api_name(name)) {
83                 ERROR("invalid api name forbidden (name is '%s')", name);
84                 goto error;
85         }
86
87         /* check previously existing plugin */
88         for (i = 0 ; i < apis_count ; i++) {
89                 if (!strcasecmp(apis_array[i].name, name)) {
90                         ERROR("api of name %s already exists", name);
91                         goto error;
92                 }
93         }
94
95         /* allocates enough memory */
96         apis = realloc(apis_array, ((unsigned)apis_count + 1) * sizeof * apis);
97         if (apis == NULL) {
98                 ERROR("out of memory");
99                 goto error;
100         }
101         apis_array = apis;
102
103         /* record the plugin */
104         apis = &apis_array[apis_count];
105         apis->api = api;
106         apis->namelen = strlen(name);
107         apis->name = name;
108         apis_count++;
109
110         return 0;
111
112 error:
113         return -1;
114 }
115
116 void afb_apis_call_(struct afb_req req, struct afb_context *context, const char *api, const char *verb)
117 {
118         afb_apis_call(req, context, api, strlen(api), verb, strlen(verb));
119 }
120
121 void afb_apis_call(struct afb_req req, struct afb_context *context, const char *api, size_t lenapi, const char *verb, size_t lenverb)
122 {
123         int i;
124         const struct api_desc *a;
125
126         a = apis_array;
127         for (i = 0 ; i < apis_count ; i++, a++) {
128                 if (a->namelen == lenapi && !strncasecmp(a->name, api, lenapi)) {
129                         context->api_index = i;
130                         a->api.call(a->api.closure, req, context, verb, lenverb);
131                         return;
132                 }
133         }
134         afb_req_fail(req, "fail", "api not found");
135 }
136