2 * Copyright (C) 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.
29 #include <microhttpd.h>
30 #include <systemd/sd-event.h>
32 #include "afb-method.h"
33 #include "afb-context.h"
36 #include <afb/afb-req-itf.h>
39 #include "afb-common.h"
43 #define JSON_CONTENT "application/json"
44 #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
48 struct hsrv_handler *next;
51 int (*handler) (struct afb_hreq *, void *);
58 const char *directory;
66 struct hsrv_handler *handlers;
67 struct MHD_Daemon *httpd;
68 sd_event_source *evsrc;
73 static int global_reqids = 0;
75 static void reply_error(struct MHD_Connection *connection, unsigned int status)
77 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
78 MHD_queue_response(connection, status, response);
79 MHD_destroy_response(response);
82 static int postproc(void *cls,
83 enum MHD_ValueKind kind,
86 const char *content_type,
87 const char *transfer_encoding,
92 struct afb_hreq *hreq = cls;
94 return afb_hreq_post_add_file(hreq, key, filename, data, size);
96 return afb_hreq_post_add(hreq, key, data, size);
99 static int access_handler(
101 struct MHD_Connection *connection,
103 const char *methodstr,
105 const char *upload_data,
106 size_t *upload_data_size,
110 struct afb_hreq *hreq;
111 enum afb_method method;
112 struct afb_hsrv *hsrv;
113 struct hsrv_handler *iter;
120 method = get_method(methodstr);
121 method &= afb_method_get | afb_method_post;
122 if (method == afb_method_none) {
123 reply_error(connection, MHD_HTTP_BAD_REQUEST);
127 /* create the request */
128 hreq = calloc(1, sizeof *hreq);
130 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
134 /* init the request */
137 hreq->cacheTimeout = hsrv->cache_to;
138 hreq->reqid = ++global_reqids;
142 hreq->connection = connection;
143 hreq->method = method;
144 hreq->version = version;
145 hreq->lang = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
146 hreq->tail = hreq->url = url;
147 hreq->lentail = hreq->lenurl = strlen(url);
150 /* init the post processing */
151 if (method == afb_method_post) {
152 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
154 /* an empty post, let's process it as a get */
155 hreq->method = afb_method_get;
156 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
157 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
158 if (hreq->postform == NULL)
159 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
161 } else if (strcasestr(type, JSON_CONTENT) != NULL) {
164 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
170 /* process further data */
171 if (*upload_data_size) {
172 if (hreq->postform != NULL) {
173 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
174 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
178 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
179 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
183 *upload_data_size = 0;
188 if (hreq->postform != NULL) {
189 rc = MHD_destroy_post_processor(hreq->postform);
190 hreq->postform = NULL;
192 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
197 if (hreq->scanned != 0) {
198 if (hreq->replied == 0 && hreq->suspended == 0) {
199 MHD_suspend_connection (connection);
205 /* search an handler for the request */
207 iter = hsrv->handlers;
209 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
210 if (iter->handler(hreq, iter->data)) {
211 if (hreq->replied == 0 && hreq->suspended == 0) {
212 MHD_suspend_connection (connection);
217 hreq->tail = hreq->url;
218 hreq->lentail = hreq->lenurl;
224 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
228 /* Because of POST call multiple time requestApi we need to free POST handle here */
229 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
230 enum MHD_RequestTerminationCode toe)
232 struct afb_hreq *hreq;
236 MHD_suspend_connection (connection);
237 afb_hreq_unref(hreq);
240 void run_micro_httpd(struct afb_hsrv *hsrv)
242 if (hsrv->in_run != 0)
245 sd_event_source_set_io_events(hsrv->evsrc, 0);
248 MHD_run(hsrv->httpd);
249 } while(hsrv->in_run == 2);
251 sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
255 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsrv)
257 run_micro_httpd(hsrv);
261 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
266 static struct hsrv_handler *new_handler(
267 struct hsrv_handler *head,
269 int (*handler) (struct afb_hreq *, void *),
273 struct hsrv_handler *link, *iter, *previous;
276 /* get the length of the prefix without its leading / */
277 length = strlen(prefix);
278 while (length && prefix[length - 1] == '/')
281 /* allocates the new link */
282 link = malloc(sizeof *link);
287 link->prefix = prefix;
288 link->length = length;
289 link->handler = handler;
291 link->priority = priority;
296 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
301 if (previous == NULL)
303 previous->next = link;
307 static int handle_alias(struct afb_hreq *hreq, void *data)
310 struct hsrv_alias *da = data;
312 if (hreq->method != afb_method_get) {
315 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
319 if (!afb_hreq_valid_tail(hreq)) {
322 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
326 rc = afb_hreq_reply_file_if_exist(hreq, da->dirfd, &hreq->tail[1]);
330 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
335 int afb_hsrv_add_handler(
336 struct afb_hsrv *hsrv,
338 int (*handler) (struct afb_hreq *, void *),
342 struct hsrv_handler *head;
344 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
347 hsrv->handlers = head;
351 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority, int relax)
353 struct hsrv_alias *da;
356 dirfd = open(alias, O_PATH|O_DIRECTORY);
361 da = malloc(sizeof *da);
364 da->directory = alias;
365 da->lendir = strlen(da->directory);
368 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
376 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
381 rc = asprintf(&dur, "%d", duration);
385 free(hsrv->cache_to);
386 hsrv->cache_to = dur;
390 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
392 sd_event_source *evsrc;
394 struct MHD_Daemon *httpd;
395 const union MHD_DaemonInfo *info;
397 httpd = MHD_start_daemon(
398 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
400 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
401 access_handler, hsrv, /* Http Request Call back + extra attribute */
402 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
403 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
404 MHD_OPTION_END); /* options-end */
407 ERROR("httpStart invalid httpd port: %d", (int)port);
411 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
413 MHD_stop_daemon(httpd);
414 ERROR("httpStart no pollfd");
418 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
420 MHD_stop_daemon(httpd);
422 ERROR("connection to events for httpd failed");
431 void afb_hsrv_stop(struct afb_hsrv *hsrv)
433 if (hsrv->evsrc != NULL) {
434 sd_event_source_unref(hsrv->evsrc);
437 if (hsrv->httpd != NULL)
438 MHD_stop_daemon(hsrv->httpd);
442 struct afb_hsrv *afb_hsrv_create()
444 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
446 result->refcount = 1;
450 void afb_hsrv_put(struct afb_hsrv *hsrv)
452 assert(hsrv->refcount != 0);
453 if (!--hsrv->refcount) {