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;
65 struct hsrv_handler *handlers;
66 struct MHD_Daemon *httpd;
67 sd_event_source *evsrc;
72 static int global_reqids = 0;
74 static void reply_error(struct MHD_Connection *connection, unsigned int status)
76 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
77 MHD_queue_response(connection, status, response);
78 MHD_destroy_response(response);
81 static int postproc(void *cls,
82 enum MHD_ValueKind kind,
85 const char *content_type,
86 const char *transfer_encoding,
91 struct afb_hreq *hreq = cls;
93 return afb_hreq_post_add_file(hreq, key, filename, data, size);
95 return afb_hreq_post_add(hreq, key, data, size);
98 static int access_handler(
100 struct MHD_Connection *connection,
102 const char *methodstr,
104 const char *upload_data,
105 size_t *upload_data_size,
109 struct afb_hreq *hreq;
110 enum afb_method method;
111 struct afb_hsrv *hsrv;
112 struct hsrv_handler *iter;
119 method = get_method(methodstr);
120 method &= afb_method_get | afb_method_post;
121 if (method == afb_method_none) {
122 reply_error(connection, MHD_HTTP_BAD_REQUEST);
126 /* create the request */
127 hreq = calloc(1, sizeof *hreq);
129 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
133 /* init the request */
136 hreq->cacheTimeout = hsrv->cache_to;
137 hreq->reqid = ++global_reqids;
141 hreq->connection = connection;
142 hreq->method = method;
143 hreq->version = version;
144 hreq->tail = hreq->url = url;
145 hreq->lentail = hreq->lenurl = strlen(url);
148 /* init the post processing */
149 if (method == afb_method_post) {
150 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
152 /* an empty post, let's process it as a get */
153 hreq->method = afb_method_get;
154 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
155 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
156 if (hreq->postform == NULL)
157 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
159 } else if (strcasestr(type, JSON_CONTENT) != NULL) {
162 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
168 /* process further data */
169 if (*upload_data_size) {
170 if (hreq->postform != NULL) {
171 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
172 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
176 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
177 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
181 *upload_data_size = 0;
186 if (hreq->postform != NULL) {
187 rc = MHD_destroy_post_processor(hreq->postform);
188 hreq->postform = NULL;
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 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 MHD_suspend_connection (connection);
235 afb_hreq_unref(hreq);
238 void run_micro_httpd(struct afb_hsrv *hsrv)
240 if (hsrv->in_run != 0)
243 sd_event_source_set_io_events(hsrv->evsrc, 0);
246 MHD_run(hsrv->httpd);
247 } while(hsrv->in_run == 2);
249 sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
253 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsrv)
255 run_micro_httpd(hsrv);
259 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
264 static struct hsrv_handler *new_handler(
265 struct hsrv_handler *head,
267 int (*handler) (struct afb_hreq *, void *),
271 struct hsrv_handler *link, *iter, *previous;
274 /* get the length of the prefix without its leading / */
275 length = strlen(prefix);
276 while (length && prefix[length - 1] == '/')
279 /* allocates the new link */
280 link = malloc(sizeof *link);
285 link->prefix = prefix;
286 link->length = length;
287 link->handler = handler;
289 link->priority = priority;
294 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
299 if (previous == NULL)
301 previous->next = link;
305 static int handle_alias(struct afb_hreq *hreq, void *data)
307 struct hsrv_alias *da = data;
309 if (hreq->method != afb_method_get) {
310 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
314 if (!afb_hreq_valid_tail(hreq)) {
315 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
319 return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
322 int afb_hsrv_add_handler(
323 struct afb_hsrv *hsrv,
325 int (*handler) (struct afb_hreq *, void *),
329 struct hsrv_handler *head;
331 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
334 hsrv->handlers = head;
338 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority)
340 struct hsrv_alias *da;
343 dirfd = open(alias, O_PATH|O_DIRECTORY);
348 da = malloc(sizeof *da);
351 da->directory = alias;
352 da->lendir = strlen(da->directory);
354 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
362 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
367 rc = asprintf(&dur, "%d", duration);
371 free(hsrv->cache_to);
372 hsrv->cache_to = dur;
376 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
378 sd_event_source *evsrc;
380 struct MHD_Daemon *httpd;
381 const union MHD_DaemonInfo *info;
383 httpd = MHD_start_daemon(
384 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
386 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
387 access_handler, hsrv, /* Http Request Call back + extra attribute */
388 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
389 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
390 MHD_OPTION_END); /* options-end */
393 ERROR("httpStart invalid httpd port: %d", (int)port);
397 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
399 MHD_stop_daemon(httpd);
400 ERROR("httpStart no pollfd");
404 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
406 MHD_stop_daemon(httpd);
408 ERROR("connection to events for httpd failed");
417 void afb_hsrv_stop(struct afb_hsrv *hsrv)
419 if (hsrv->evsrc != NULL) {
420 sd_event_source_unref(hsrv->evsrc);
423 if (hsrv->httpd != NULL)
424 MHD_stop_daemon(hsrv->httpd);
428 struct afb_hsrv *afb_hsrv_create()
430 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
432 result->refcount = 1;
436 void afb_hsrv_put(struct afb_hsrv *hsrv)
438 assert(hsrv->refcount != 0);
439 if (!--hsrv->refcount) {