2 * Copyright 2016 IoT.bzh
3 * Author: José Bollo <jose.bollo@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
27 #include <microhttpd.h>
29 #include "afb-method.h"
32 #include "afb-req-itf.h"
34 #include "utils-upoll.h"
37 #define JSON_CONTENT "application/json"
38 #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
42 struct hsrv_handler *next;
45 int (*handler) (struct afb_hreq *, void *);
52 const char *directory;
65 enum afb_hsrv_state state;
66 struct hsrv_handler *handlers;
67 struct MHD_Daemon *httpd;
73 static void reply_error(struct MHD_Connection *connection, unsigned int status)
75 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
76 MHD_queue_response(connection, status, response);
77 MHD_destroy_response(response);
80 static int postproc(void *cls,
81 enum MHD_ValueKind kind,
84 const char *content_type,
85 const char *transfer_encoding,
90 struct afb_hreq *hreq = cls;
92 return afb_hreq_post_add_file(hreq, key, filename, data, size);
94 return afb_hreq_post_add(hreq, key, data, size);
97 static int access_handler(
99 struct MHD_Connection *connection,
101 const char *methodstr,
103 const char *upload_data,
104 size_t *upload_data_size,
108 struct afb_hreq *hreq;
109 enum afb_method method;
110 struct afb_hsrv *hsrv;
111 struct hsrv_handler *iter;
117 /* create the request */
118 hreq = calloc(1, sizeof *hreq);
124 method = get_method(methodstr);
125 method &= afb_method_get | afb_method_post;
126 if (method == afb_method_none)
129 /* init the request */
130 hreq->cacheTimeout = hsrv->cache_to;
131 hreq->connection = connection;
132 hreq->method = method;
133 hreq->version = version;
134 hreq->tail = hreq->url = url;
135 hreq->lentail = hreq->lenurl = strlen(url);
137 /* init the post processing */
138 if (method == afb_method_post) {
139 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
141 /* an empty post, let's process it as a get */
142 hreq->method = afb_method_get;
143 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
144 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
145 if (hreq->postform == NULL)
148 } else if (strcasestr(type, JSON_CONTENT) == NULL) {
149 reply_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
155 /* process further data */
156 if (*upload_data_size) {
157 if (hreq->postform != NULL) {
158 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size))
161 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size))
164 *upload_data_size = 0;
169 if (hreq->postform != NULL) {
170 rc = MHD_destroy_post_processor(hreq->postform);
171 hreq->postform = NULL;
176 /* search an handler for the request */
177 iter = hsrv->handlers;
179 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
180 if (iter->handler(hreq, iter->data))
182 hreq->tail = hreq->url;
183 hreq->lentail = hreq->lenurl;
189 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
193 reply_error(connection, MHD_HTTP_BAD_REQUEST);
197 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
201 /* Because of POST call multiple time requestApi we need to free POST handle here */
202 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
203 enum MHD_RequestTerminationCode toe)
205 struct afb_hreq *hreq;
209 MHD_suspend_connection (connection);
213 static void handle_epoll_readable(struct afb_hsrv *hsrv)
215 if (hsrv->state != hsrv_idle)
216 hsrv->state = hsrv_rerun;
219 hsrv->state = hsrv_run;
220 MHD_run(hsrv->httpd);
221 } while (hsrv->state == hsrv_rerun);
222 hsrv->state = hsrv_idle;
226 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
231 static struct hsrv_handler *new_handler(
232 struct hsrv_handler *head,
234 int (*handler) (struct afb_hreq *, void *),
238 struct hsrv_handler *link, *iter, *previous;
241 /* get the length of the prefix without its leading / */
242 length = strlen(prefix);
243 while (length && prefix[length - 1] == '/')
246 /* allocates the new link */
247 link = malloc(sizeof *link);
252 link->prefix = prefix;
253 link->length = length;
254 link->handler = handler;
256 link->priority = priority;
261 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
266 if (previous == NULL)
268 previous->next = link;
272 static int handle_alias(struct afb_hreq *hreq, void *data)
274 struct hsrv_alias *da = data;
276 if (hreq->method != afb_method_get) {
277 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
281 if (!afb_hreq_valid_tail(hreq)) {
282 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
286 return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
289 int afb_hsrv_add_handler(
290 struct afb_hsrv *hsrv,
292 int (*handler) (struct afb_hreq *, void *),
296 struct hsrv_handler *head;
298 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
301 hsrv->handlers = head;
305 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority)
307 struct hsrv_alias *da;
310 dirfd = open(alias, O_PATH|O_DIRECTORY);
315 da = malloc(sizeof *da);
318 da->directory = alias;
319 da->lendir = strlen(da->directory);
321 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
329 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
334 rc = asprintf(&dur, "%d", duration);
338 free(hsrv->cache_to);
339 hsrv->cache_to = dur;
343 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
346 struct MHD_Daemon *httpd;
347 const union MHD_DaemonInfo *info;
349 httpd = MHD_start_daemon(
350 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
352 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
353 access_handler, hsrv, /* Http Request Call back + extra attribute */
354 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
355 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
356 MHD_OPTION_END); /* options-end */
359 fprintf(stderr, "Error: httpStart invalid httpd port: %d", (int)port);
363 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
365 MHD_stop_daemon(httpd);
366 fprintf(stderr, "Error: httpStart no pollfd");
370 upoll = upoll_open(info->listen_fd, hsrv);
372 MHD_stop_daemon(httpd);
373 fprintf(stderr, "Error: connection to upoll of httpd failed");
376 upoll_on_readable(upoll, (void*)handle_epoll_readable);
383 void afb_hsrv_stop(struct afb_hsrv *hsrv)
386 upoll_close(hsrv->upoll);
388 if (hsrv->httpd != NULL)
389 MHD_stop_daemon(hsrv->httpd);
393 struct afb_hsrv *afb_hsrv_create()
395 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
397 result->refcount = 1;
401 void afb_hsrv_put(struct afb_hsrv *hsrv)
403 assert(hsrv->refcount != 0);
404 if (!--hsrv->refcount) {