2 * Copyright (C) 2016, 2017, 2018 "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>
32 #include "afb-method.h"
33 #include "afb-context.h"
40 #include "locale-root.h"
42 #include "afb-systemd.h"
45 #define JSON_CONTENT "application/json"
46 #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
50 struct hsrv_handler *next;
53 int (*handler) (struct afb_hreq *, void *);
59 struct locale_root *root;
65 struct hsrv_handler *handlers;
66 struct MHD_Daemon *httpd;
71 static void reply_error(struct MHD_Connection *connection, unsigned int status)
73 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
74 MHD_queue_response(connection, status, response);
75 MHD_destroy_response(response);
78 static int postproc(void *cls,
79 enum MHD_ValueKind kind,
82 const char *content_type,
83 const char *transfer_encoding,
88 struct afb_hreq *hreq = cls;
90 return afb_hreq_post_add_file(hreq, key, filename, data, size);
92 return afb_hreq_post_add(hreq, key, data, size);
95 static int access_handler(
97 struct MHD_Connection *connection,
99 const char *methodstr,
101 const char *upload_data,
102 size_t *upload_data_size,
106 struct afb_hreq *hreq;
107 enum afb_method method;
108 struct afb_hsrv *hsrv;
109 struct hsrv_handler *iter;
116 method = get_method(methodstr);
117 method &= afb_method_get | afb_method_post;
118 if (method == afb_method_none) {
119 WARNING("Unsupported HTTP operation %s", methodstr);
120 reply_error(connection, MHD_HTTP_BAD_REQUEST);
124 /* create the request */
125 hreq = afb_hreq_create();
127 ERROR("Can't allocate 'hreq'");
128 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
132 /* init the request */
134 hreq->cacheTimeout = hsrv->cache_to;
135 hreq->connection = connection;
136 hreq->method = method;
137 hreq->version = version;
138 hreq->lang = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_ACCEPT_LANGUAGE);
139 hreq->tail = hreq->url = url;
140 hreq->lentail = hreq->lenurl = strlen(url);
143 /* init the post processing */
144 if (method == afb_method_post) {
145 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
147 /* an empty post, let's process it as a get */
148 hreq->method = afb_method_get;
149 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
150 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
151 if (hreq->postform == NULL) {
152 ERROR("Can't create POST processor");
153 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
156 } else if (strcasestr(type, JSON_CONTENT) != NULL) {
159 WARNING("Unsupported media type %s", type);
160 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
166 /* process further data */
167 if (*upload_data_size) {
168 if (hreq->postform != NULL) {
169 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
170 ERROR("error in POST processor");
171 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
175 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
176 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
180 *upload_data_size = 0;
185 if (hreq->postform != NULL) {
186 rc = MHD_destroy_post_processor(hreq->postform);
187 hreq->postform = NULL;
189 ERROR("error detected in POST processing");
190 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
195 if (hreq->scanned != 0) {
196 if (hreq->replied == 0 && hreq->suspended == 0) {
197 MHD_suspend_connection (connection);
203 /* search an handler for the request */
205 iter = hsrv->handlers;
207 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
208 if (iter->handler(hreq, iter->data)) {
209 if (hreq->replied == 0 && hreq->suspended == 0) {
210 MHD_suspend_connection (connection);
215 hreq->tail = hreq->url;
216 hreq->lentail = hreq->lenurl;
222 WARNING("Unhandled request to %s", hreq->url);
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 afb_hreq_unref(hreq);
239 static void do_run(int signum, void *arg)
241 MHD_UNSIGNED_LONG_LONG to;
242 struct afb_hsrv *hsrv = arg;
245 do { MHD_run(hsrv->httpd); } while(MHD_get_timeout(hsrv->httpd, &to) == MHD_YES && !to);
247 fdev_set_events(hsrv->fdev, EPOLLIN);
250 void run_micro_httpd(struct afb_hsrv *hsrv)
252 fdev_set_events(hsrv->fdev, 0);
253 if (jobs_queue(hsrv, 0, do_run, hsrv) < 0)
257 static void listen_callback(void *hsrv, uint32_t revents, struct fdev *fdev)
259 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)
398 struct MHD_Daemon *httpd;
399 const union MHD_DaemonInfo *info;
401 httpd = MHD_start_daemon(
402 MHD_USE_EPOLL | MHD_ALLOW_UPGRADE | 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 fdev = afb_fdev_create(info->listen_fd);
424 MHD_stop_daemon(httpd);
425 ERROR("connection to events for httpd failed");
428 fdev_set_autoclose(fdev, 0);
429 fdev_set_events(fdev, EPOLLIN);
430 fdev_set_callback(fdev, listen_callback, hsrv);
437 void afb_hsrv_stop(struct afb_hsrv *hsrv)
439 if (hsrv->fdev != NULL) {
440 fdev_unref(hsrv->fdev);
443 if (hsrv->httpd != NULL)
444 MHD_stop_daemon(hsrv->httpd);
448 struct afb_hsrv *afb_hsrv_create()
450 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
452 result->refcount = 1;
456 void afb_hsrv_put(struct afb_hsrv *hsrv)
458 assert(hsrv->refcount != 0);
459 if (!--hsrv->refcount) {