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"
37 #include <afb/afb-req-itf.h>
39 #include "locale-root.h"
41 #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 *);
57 struct locale_root *root;
63 struct hsrv_handler *handlers;
64 struct MHD_Daemon *httpd;
65 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;
235 MHD_suspend_connection (connection);
236 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;
311 struct locale_search *search;
313 if (hreq->method != afb_method_get) {
316 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
320 search = locale_root_search(da->root, hreq->lang, 0);
321 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, &hreq->tail[1]);
322 locale_search_unref(search);
326 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
331 int afb_hsrv_add_handler(
332 struct afb_hsrv *hsrv,
334 int (*handler) (struct afb_hreq *, void *),
338 struct hsrv_handler *head;
340 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
343 hsrv->handlers = head;
347 int afb_hsrv_add_alias_root(struct afb_hsrv *hsrv, const char *prefix, struct locale_root *root, int priority, int relax)
349 struct hsrv_alias *da;
351 da = malloc(sizeof *da);
355 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority)) {
356 locale_root_addref(root);
364 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, int dirfd, const char *alias, int priority, int relax)
366 struct locale_root *root;
369 root = locale_root_create_at(dirfd, alias);
371 ERROR("can't connect to directory %s: %m", alias);
374 rc = afb_hsrv_add_alias_root(hsrv, prefix, root, priority, relax);
375 locale_root_unref(root);
380 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
385 rc = asprintf(&dur, "%d", duration);
389 free(hsrv->cache_to);
390 hsrv->cache_to = dur;
394 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
396 sd_event_source *evsrc;
398 struct MHD_Daemon *httpd;
399 const union MHD_DaemonInfo *info;
401 httpd = MHD_start_daemon(
402 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
404 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
405 access_handler, hsrv, /* Http Request Call back + extra attribute */
406 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
407 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
408 MHD_OPTION_END); /* options-end */
411 ERROR("httpStart invalid httpd port: %d", (int)port);
415 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
417 MHD_stop_daemon(httpd);
418 ERROR("httpStart no pollfd");
422 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
424 MHD_stop_daemon(httpd);
426 ERROR("connection to events for httpd failed");
435 void afb_hsrv_stop(struct afb_hsrv *hsrv)
437 if (hsrv->evsrc != NULL) {
438 sd_event_source_unref(hsrv->evsrc);
441 if (hsrv->httpd != NULL)
442 MHD_stop_daemon(hsrv->httpd);
446 struct afb_hsrv *afb_hsrv_create()
448 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
450 result->refcount = 1;
454 void afb_hsrv_put(struct afb_hsrv *hsrv)
456 assert(hsrv->refcount != 0);
457 if (!--hsrv->refcount) {