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->tail = hreq->url = url;
146 hreq->lentail = hreq->lenurl = strlen(url);
149 /* init the post processing */
150 if (method == afb_method_post) {
151 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
153 /* an empty post, let's process it as a get */
154 hreq->method = afb_method_get;
155 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
156 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
157 if (hreq->postform == NULL)
158 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
160 } else if (strcasestr(type, JSON_CONTENT) != NULL) {
163 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
169 /* process further data */
170 if (*upload_data_size) {
171 if (hreq->postform != NULL) {
172 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
173 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
177 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
178 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
182 *upload_data_size = 0;
187 if (hreq->postform != NULL) {
188 rc = MHD_destroy_post_processor(hreq->postform);
189 hreq->postform = NULL;
191 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
196 if (hreq->scanned != 0) {
197 if (hreq->replied == 0 && hreq->suspended == 0) {
198 MHD_suspend_connection (connection);
204 /* search an handler for the request */
206 iter = hsrv->handlers;
208 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
209 if (iter->handler(hreq, iter->data)) {
210 if (hreq->replied == 0 && hreq->suspended == 0) {
211 MHD_suspend_connection (connection);
216 hreq->tail = hreq->url;
217 hreq->lentail = hreq->lenurl;
223 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
227 /* Because of POST call multiple time requestApi we need to free POST handle here */
228 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
229 enum MHD_RequestTerminationCode toe)
231 struct afb_hreq *hreq;
235 MHD_suspend_connection (connection);
236 afb_hreq_unref(hreq);
239 void run_micro_httpd(struct afb_hsrv *hsrv)
241 if (hsrv->in_run != 0)
244 sd_event_source_set_io_events(hsrv->evsrc, 0);
247 MHD_run(hsrv->httpd);
248 } while(hsrv->in_run == 2);
250 sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
254 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsrv)
256 run_micro_httpd(hsrv);
260 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
265 static struct hsrv_handler *new_handler(
266 struct hsrv_handler *head,
268 int (*handler) (struct afb_hreq *, void *),
272 struct hsrv_handler *link, *iter, *previous;
275 /* get the length of the prefix without its leading / */
276 length = strlen(prefix);
277 while (length && prefix[length - 1] == '/')
280 /* allocates the new link */
281 link = malloc(sizeof *link);
286 link->prefix = prefix;
287 link->length = length;
288 link->handler = handler;
290 link->priority = priority;
295 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
300 if (previous == NULL)
302 previous->next = link;
306 static int handle_alias(struct afb_hreq *hreq, void *data)
309 struct hsrv_alias *da = data;
311 if (hreq->method != afb_method_get) {
314 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
318 if (!afb_hreq_valid_tail(hreq)) {
321 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
325 rc = afb_hreq_reply_file_if_exist(hreq, da->dirfd, &hreq->tail[1]);
329 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
334 int afb_hsrv_add_handler(
335 struct afb_hsrv *hsrv,
337 int (*handler) (struct afb_hreq *, void *),
341 struct hsrv_handler *head;
343 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
346 hsrv->handlers = head;
350 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority, int relax)
352 struct hsrv_alias *da;
355 dirfd = open(alias, O_PATH|O_DIRECTORY);
360 da = malloc(sizeof *da);
363 da->directory = alias;
364 da->lendir = strlen(da->directory);
367 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
375 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
380 rc = asprintf(&dur, "%d", duration);
384 free(hsrv->cache_to);
385 hsrv->cache_to = dur;
389 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
391 sd_event_source *evsrc;
393 struct MHD_Daemon *httpd;
394 const union MHD_DaemonInfo *info;
396 httpd = MHD_start_daemon(
397 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
399 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
400 access_handler, hsrv, /* Http Request Call back + extra attribute */
401 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
402 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
403 MHD_OPTION_END); /* options-end */
406 ERROR("httpStart invalid httpd port: %d", (int)port);
410 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
412 MHD_stop_daemon(httpd);
413 ERROR("httpStart no pollfd");
417 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
419 MHD_stop_daemon(httpd);
421 ERROR("connection to events for httpd failed");
430 void afb_hsrv_stop(struct afb_hsrv *hsrv)
432 if (hsrv->evsrc != NULL) {
433 sd_event_source_unref(hsrv->evsrc);
436 if (hsrv->httpd != NULL)
437 MHD_stop_daemon(hsrv->httpd);
441 struct afb_hsrv *afb_hsrv_create()
443 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
445 result->refcount = 1;
449 void afb_hsrv_put(struct afb_hsrv *hsrv)
451 assert(hsrv->refcount != 0);
452 if (!--hsrv->refcount) {