2 * Copyright 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"
35 #include "afb-req-itf.h"
38 #include "afb-common.h"
42 #define JSON_CONTENT "application/json"
43 #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
47 struct hsrv_handler *next;
50 int (*handler) (struct afb_hreq *, void *);
57 const char *directory;
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 reply_error(connection, MHD_HTTP_BAD_REQUEST);
125 /* create the request */
126 hreq = calloc(1, sizeof *hreq);
128 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
132 /* init the request */
134 hreq->cacheTimeout = hsrv->cache_to;
135 hreq->reqid = ++global_reqids;
139 hreq->connection = connection;
140 hreq->method = method;
141 hreq->version = version;
142 hreq->tail = hreq->url = url;
143 hreq->lentail = hreq->lenurl = strlen(url);
146 /* init the post processing */
147 if (method == afb_method_post) {
148 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
150 /* an empty post, let's process it as a get */
151 hreq->method = afb_method_get;
152 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
153 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
154 if (hreq->postform == NULL)
155 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
157 } else if (strcasestr(type, JSON_CONTENT) == NULL) {
158 afb_hreq_reply_error(hreq, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
164 /* process further data */
165 if (*upload_data_size) {
166 if (hreq->postform != NULL) {
167 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size)) {
168 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
172 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
173 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
177 *upload_data_size = 0;
182 if (hreq->postform != NULL) {
183 rc = MHD_destroy_post_processor(hreq->postform);
184 hreq->postform = NULL;
186 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
191 if (hreq->scanned != 0) {
192 if (hreq->replied == 0 && hreq->suspended == 0) {
193 MHD_suspend_connection (connection);
199 /* search an handler for the request */
201 iter = hsrv->handlers;
203 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
204 if (iter->handler(hreq, iter->data)) {
205 if (hreq->replied == 0 && hreq->suspended == 0) {
206 MHD_suspend_connection (connection);
211 hreq->tail = hreq->url;
212 hreq->lentail = hreq->lenurl;
218 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
222 /* Because of POST call multiple time requestApi we need to free POST handle here */
223 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
224 enum MHD_RequestTerminationCode toe)
226 struct afb_hreq *hreq;
230 MHD_suspend_connection (connection);
234 void run_micro_httpd(struct afb_hsrv *hsrv)
236 if (hsrv->in_run != 0)
239 sd_event_source_set_io_events(hsrv->evsrc, 0);
242 MHD_run(hsrv->httpd);
243 } while(hsrv->in_run == 2);
245 sd_event_source_set_io_events(hsrv->evsrc, EPOLLIN);
249 static int io_event_callback(sd_event_source *src, int fd, uint32_t revents, void *hsrv)
251 run_micro_httpd(hsrv);
255 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
260 static struct hsrv_handler *new_handler(
261 struct hsrv_handler *head,
263 int (*handler) (struct afb_hreq *, void *),
267 struct hsrv_handler *link, *iter, *previous;
270 /* get the length of the prefix without its leading / */
271 length = strlen(prefix);
272 while (length && prefix[length - 1] == '/')
275 /* allocates the new link */
276 link = malloc(sizeof *link);
281 link->prefix = prefix;
282 link->length = length;
283 link->handler = handler;
285 link->priority = priority;
290 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
295 if (previous == NULL)
297 previous->next = link;
301 static int handle_alias(struct afb_hreq *hreq, void *data)
303 struct hsrv_alias *da = data;
305 if (hreq->method != afb_method_get) {
306 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
310 if (!afb_hreq_valid_tail(hreq)) {
311 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
315 return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
318 int afb_hsrv_add_handler(
319 struct afb_hsrv *hsrv,
321 int (*handler) (struct afb_hreq *, void *),
325 struct hsrv_handler *head;
327 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
330 hsrv->handlers = head;
334 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority)
336 struct hsrv_alias *da;
339 dirfd = open(alias, O_PATH|O_DIRECTORY);
344 da = malloc(sizeof *da);
347 da->directory = alias;
348 da->lendir = strlen(da->directory);
350 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
358 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
363 rc = asprintf(&dur, "%d", duration);
367 free(hsrv->cache_to);
368 hsrv->cache_to = dur;
372 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
374 sd_event_source *evsrc;
376 struct MHD_Daemon *httpd;
377 const union MHD_DaemonInfo *info;
379 httpd = MHD_start_daemon(
380 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
382 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
383 access_handler, hsrv, /* Http Request Call back + extra attribute */
384 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
385 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
386 MHD_OPTION_END); /* options-end */
389 fprintf(stderr, "Error: httpStart invalid httpd port: %d", (int)port);
393 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
395 MHD_stop_daemon(httpd);
396 fprintf(stderr, "Error: httpStart no pollfd");
400 rc = sd_event_add_io(afb_common_get_event_loop(), &evsrc, info->listen_fd, EPOLLIN, io_event_callback, hsrv);
402 MHD_stop_daemon(httpd);
404 fprintf(stderr, "Error: connection to events for httpd failed");
413 void afb_hsrv_stop(struct afb_hsrv *hsrv)
415 if (hsrv->evsrc != NULL) {
416 sd_event_source_unref(hsrv->evsrc);
419 if (hsrv->httpd != NULL)
420 MHD_stop_daemon(hsrv->httpd);
424 struct afb_hsrv *afb_hsrv_create()
426 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
428 result->refcount = 1;
432 void afb_hsrv_put(struct afb_hsrv *hsrv)
434 assert(hsrv->refcount != 0);
435 if (!--hsrv->refcount) {