2 * Copyright (C) 2016, 2017 "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>
38 #include "locale-root.h"
40 #include "afb-common.h"
44 #define JSON_CONTENT "application/json"
45 #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
49 struct hsrv_handler *next;
52 int (*handler) (struct afb_hreq *, void *);
58 struct locale_root *root;
64 struct hsrv_handler *handlers;
65 struct MHD_Daemon *httpd;
66 sd_event_source *evsrc;
71 static int global_reqids = 0;
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;
118 method = get_method(methodstr);
119 method &= afb_method_get | afb_method_post;
120 if (method == afb_method_none) {
121 WARNING("Unsupported HTTP operation %s", methodstr);
122 reply_error(connection, MHD_HTTP_BAD_REQUEST);
126 /* create the request */
127 hreq = calloc(1, sizeof *hreq);
129 ERROR("Can't allocate '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 = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, 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 ERROR("Can't create POST processor");
160 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
163 } else if (strcasestr(type, JSON_CONTENT) != NULL) {
166 WARNING("Unsupported media type %s", type);
167 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
173 /* process further data */
174 if (*upload_data_size) {
175 if (hreq->postform != NULL) {
176 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
177 ERROR("error in POST processor");
178 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
182 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
183 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
187 *upload_data_size = 0;
192 if (hreq->postform != NULL) {
193 rc = MHD_destroy_post_processor(hreq->postform);
194 hreq->postform = NULL;
196 ERROR("error detected in POST processing");
197 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
202 if (hreq->scanned != 0) {
203 if (hreq->replied == 0 && hreq->suspended == 0) {
204 MHD_suspend_connection (connection);
210 /* search an handler for the request */
212 iter = hsrv->handlers;
214 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
215 if (iter->handler(hreq, iter->data)) {
216 if (hreq->replied == 0 && hreq->suspended == 0) {
217 MHD_suspend_connection (connection);
222 hreq->tail = hreq->url;
223 hreq->lentail = hreq->lenurl;
229 WARNING("Unhandled request to %s", hreq->url);
230 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
234 /* Because of POST call multiple time requestApi we need to free POST handle here */
235 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
236 enum MHD_RequestTerminationCode toe)
238 struct afb_hreq *hreq;
243 MHD_suspend_connection (connection);
244 afb_hreq_unref(hreq);
248 void run_micro_httpd(struct afb_hsrv *hsrv)
250 if (hsrv->in_run != 0)
253 sd_event_source_set_io_events(hsrv->evsrc, 0);
256 MHD_run(hsrv->httpd);
257 } while(hsrv->in_run == 2);
259 sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
263 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsrv)
265 run_micro_httpd(hsrv);
269 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
274 static struct hsrv_handler *new_handler(
275 struct hsrv_handler *head,
277 int (*handler) (struct afb_hreq *, void *),
281 struct hsrv_handler *link, *iter, *previous;
284 /* get the length of the prefix without its leading / */
285 length = strlen(prefix);
286 while (length && prefix[length - 1] == '/')
289 /* allocates the new link */
290 link = malloc(sizeof *link);
295 link->prefix = prefix;
296 link->length = length;
297 link->handler = handler;
299 link->priority = priority;
304 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
309 if (previous == NULL)
311 previous->next = link;
315 static int handle_alias(struct afb_hreq *hreq, void *data)
318 struct hsrv_alias *da = data;
319 struct locale_search *search;
321 if (hreq->method != afb_method_get) {
324 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
328 search = locale_root_search(da->root, hreq->lang, 0);
329 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, &hreq->tail[1]);
330 locale_search_unref(search);
334 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
339 int afb_hsrv_add_handler(
340 struct afb_hsrv *hsrv,
342 int (*handler) (struct afb_hreq *, void *),
346 struct hsrv_handler *head;
348 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
351 hsrv->handlers = head;
355 int afb_hsrv_add_alias_root(struct afb_hsrv *hsrv, const char *prefix, struct locale_root *root, int priority, int relax)
357 struct hsrv_alias *da;
359 da = malloc(sizeof *da);
363 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority)) {
364 locale_root_addref(root);
372 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, int dirfd, const char *alias, int priority, int relax)
374 struct locale_root *root;
377 root = locale_root_create_at(dirfd, alias);
379 ERROR("can't connect to directory %s: %m", alias);
382 rc = afb_hsrv_add_alias_root(hsrv, prefix, root, priority, relax);
383 locale_root_unref(root);
388 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
393 rc = asprintf(&dur, "%d", duration);
397 free(hsrv->cache_to);
398 hsrv->cache_to = dur;
402 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
404 sd_event_source *evsrc;
406 struct MHD_Daemon *httpd;
407 const union MHD_DaemonInfo *info;
409 httpd = MHD_start_daemon(
410 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
412 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
413 access_handler, hsrv, /* Http Request Call back + extra attribute */
414 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
415 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
416 MHD_OPTION_END); /* options-end */
419 ERROR("httpStart invalid httpd port: %d", (int)port);
423 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
425 MHD_stop_daemon(httpd);
426 ERROR("httpStart no pollfd");
430 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
432 MHD_stop_daemon(httpd);
434 ERROR("connection to events for httpd failed");
443 void afb_hsrv_stop(struct afb_hsrv *hsrv)
445 if (hsrv->evsrc != NULL) {
446 sd_event_source_unref(hsrv->evsrc);
449 if (hsrv->httpd != NULL)
450 MHD_stop_daemon(hsrv->httpd);
454 struct afb_hsrv *afb_hsrv_create()
456 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
458 result->refcount = 1;
462 void afb_hsrv_put(struct afb_hsrv *hsrv)
464 assert(hsrv->refcount != 0);
465 if (!--hsrv->refcount) {