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.
30 #include <microhttpd.h>
31 #include <systemd/sd-event.h>
33 #include "afb-method.h"
34 #include "afb-context.h"
39 #include "locale-root.h"
41 #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;
70 static void reply_error(struct MHD_Connection *connection, unsigned int status)
72 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
73 MHD_queue_response(connection, status, response);
74 MHD_destroy_response(response);
77 static int postproc(void *cls,
78 enum MHD_ValueKind kind,
81 const char *content_type,
82 const char *transfer_encoding,
87 struct afb_hreq *hreq = cls;
89 return afb_hreq_post_add_file(hreq, key, filename, data, size);
91 return afb_hreq_post_add(hreq, key, data, size);
94 static int access_handler(
96 struct MHD_Connection *connection,
98 const char *methodstr,
100 const char *upload_data,
101 size_t *upload_data_size,
105 struct afb_hreq *hreq;
106 enum afb_method method;
107 struct afb_hsrv *hsrv;
108 struct hsrv_handler *iter;
115 method = get_method(methodstr);
116 method &= afb_method_get | afb_method_post;
117 if (method == afb_method_none) {
118 WARNING("Unsupported HTTP operation %s", methodstr);
119 reply_error(connection, MHD_HTTP_BAD_REQUEST);
123 /* create the request */
124 hreq = afb_hreq_create();
126 ERROR("Can't allocate 'hreq'");
127 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
131 /* init the request */
133 hreq->cacheTimeout = hsrv->cache_to;
134 hreq->connection = connection;
135 hreq->method = method;
136 hreq->version = version;
137 hreq->lang = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
138 hreq->tail = hreq->url = url;
139 hreq->lentail = hreq->lenurl = strlen(url);
142 /* init the post processing */
143 if (method == afb_method_post) {
144 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
146 /* an empty post, let's process it as a get */
147 hreq->method = afb_method_get;
148 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
149 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
150 if (hreq->postform == NULL) {
151 ERROR("Can't create POST processor");
152 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
155 } else if (strcasestr(type, JSON_CONTENT) != NULL) {
158 WARNING("Unsupported media type %s", type);
159 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
165 /* process further data */
166 if (*upload_data_size) {
167 if (hreq->postform != NULL) {
168 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
169 ERROR("error in POST processor");
170 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
174 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
175 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
179 *upload_data_size = 0;
184 if (hreq->postform != NULL) {
185 rc = MHD_destroy_post_processor(hreq->postform);
186 hreq->postform = NULL;
188 ERROR("error detected in POST processing");
189 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
194 if (hreq->scanned != 0) {
195 if (hreq->replied == 0 && hreq->suspended == 0) {
196 MHD_suspend_connection (connection);
202 /* search an handler for the request */
204 iter = hsrv->handlers;
206 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
207 if (iter->handler(hreq, iter->data)) {
208 if (hreq->replied == 0 && hreq->suspended == 0) {
209 MHD_suspend_connection (connection);
214 hreq->tail = hreq->url;
215 hreq->lentail = hreq->lenurl;
221 WARNING("Unhandled request to %s", hreq->url);
222 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
226 /* Because of POST call multiple time requestApi we need to free POST handle here */
227 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
228 enum MHD_RequestTerminationCode toe)
230 struct afb_hreq *hreq;
234 afb_hreq_unref(hreq);
238 static void do_run(int signum, void *arg)
240 MHD_UNSIGNED_LONG_LONG to;
241 struct afb_hsrv *hsrv = arg;
244 do { MHD_run(hsrv->httpd); } while(MHD_get_timeout(hsrv->httpd, &to) == MHD_YES && !to);
246 sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
249 void run_micro_httpd(struct afb_hsrv *hsrv)
251 sd_event_source_set_io_events(hsrv->evsrc, 0);
252 if (jobs_queue(hsrv, 0, do_run, hsrv) < 0)
256 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsrv)
258 run_micro_httpd(hsrv);
262 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
267 static struct hsrv_handler *new_handler(
268 struct hsrv_handler *head,
270 int (*handler) (struct afb_hreq *, void *),
274 struct hsrv_handler *link, *iter, *previous;
277 /* get the length of the prefix without its leading / */
278 length = strlen(prefix);
279 while (length && prefix[length - 1] == '/')
282 /* allocates the new link */
283 link = malloc(sizeof *link);
288 link->prefix = prefix;
289 link->length = length;
290 link->handler = handler;
292 link->priority = priority;
297 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
302 if (previous == NULL)
304 previous->next = link;
308 static int handle_alias(struct afb_hreq *hreq, void *data)
311 struct hsrv_alias *da = data;
312 struct locale_search *search;
314 if (hreq->method != afb_method_get) {
317 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
321 search = locale_root_search(da->root, hreq->lang, 0);
322 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, &hreq->tail[1]);
323 locale_search_unref(search);
327 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
332 int afb_hsrv_add_handler(
333 struct afb_hsrv *hsrv,
335 int (*handler) (struct afb_hreq *, void *),
339 struct hsrv_handler *head;
341 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
344 hsrv->handlers = head;
348 int afb_hsrv_add_alias_root(struct afb_hsrv *hsrv, const char *prefix, struct locale_root *root, int priority, int relax)
350 struct hsrv_alias *da;
352 da = malloc(sizeof *da);
356 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority)) {
357 locale_root_addref(root);
365 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, int dirfd, const char *alias, int priority, int relax)
367 struct locale_root *root;
370 root = locale_root_create_at(dirfd, alias);
372 ERROR("can't connect to directory %s: %m", alias);
375 rc = afb_hsrv_add_alias_root(hsrv, prefix, root, priority, relax);
376 locale_root_unref(root);
381 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
386 rc = asprintf(&dur, "%d", duration);
390 free(hsrv->cache_to);
391 hsrv->cache_to = dur;
395 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
397 sd_event_source *evsrc;
399 struct MHD_Daemon *httpd;
400 const union MHD_DaemonInfo *info;
402 httpd = MHD_start_daemon(
403 MHD_USE_EPOLL | MHD_ALLOW_UPGRADE | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
405 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
406 access_handler, hsrv, /* Http Request Call back + extra attribute */
407 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
408 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
409 MHD_OPTION_END); /* options-end */
412 ERROR("httpStart invalid httpd port: %d", (int)port);
416 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
418 MHD_stop_daemon(httpd);
419 ERROR("httpStart no pollfd");
423 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
425 MHD_stop_daemon(httpd);
427 ERROR("connection to events for httpd failed");
436 void afb_hsrv_stop(struct afb_hsrv *hsrv)
438 if (hsrv->evsrc != NULL) {
439 sd_event_source_unref(hsrv->evsrc);
442 if (hsrv->httpd != NULL)
443 MHD_stop_daemon(hsrv->httpd);
447 struct afb_hsrv *afb_hsrv_create()
449 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
451 result->refcount = 1;
455 void afb_hsrv_put(struct afb_hsrv *hsrv)
457 assert(hsrv->refcount != 0);
458 if (!--hsrv->refcount) {