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.
27 #include <microhttpd.h>
29 #include "afb-method.h"
32 #include "afb-req-itf.h"
34 #include "utils-upoll.h"
37 #define JSON_CONTENT "application/json"
38 #define FORM_CONTENT MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA
42 struct hsrv_handler *next;
45 int (*handler) (struct afb_hreq *, void *);
52 const char *directory;
59 struct hsrv_handler *handlers;
60 struct MHD_Daemon *httpd;
66 static void reply_error(struct MHD_Connection *connection, unsigned int status)
70 struct MHD_Response *response;
72 length = asprintf(&buffer, "<html><body>error %u</body></html>", status);
74 response = MHD_create_response_from_buffer((unsigned)length, buffer, MHD_RESPMEM_MUST_FREE);
76 buffer = "<html><body>error</body></html>";
77 response = MHD_create_response_from_buffer(strlen(buffer), buffer, MHD_RESPMEM_PERSISTENT);
79 if (!MHD_queue_response(connection, status, response))
80 fprintf(stderr, "Failed to reply error code %u", status);
81 MHD_destroy_response(response);
84 static int postproc(void *cls,
85 enum MHD_ValueKind kind,
88 const char *content_type,
89 const char *transfer_encoding,
94 struct afb_hreq *hreq = cls;
96 return afb_hreq_post_add_file(hreq, key, filename, data, size);
98 return afb_hreq_post_add(hreq, key, data, size);
101 static int access_handler(
103 struct MHD_Connection *connection,
105 const char *methodstr,
107 const char *upload_data,
108 size_t *upload_data_size,
112 struct afb_hreq *hreq;
113 enum afb_method method;
114 struct afb_hsrv *hsrv;
115 struct hsrv_handler *iter;
121 /* create the request */
122 hreq = calloc(1, sizeof *hreq);
128 method = get_method(methodstr);
129 method &= afb_method_get | afb_method_post;
130 if (method == afb_method_none)
133 /* init the request */
134 hreq->cacheTimeout = hsrv->cache_to;
135 hreq->connection = connection;
136 hreq->method = method;
137 hreq->version = version;
138 hreq->tail = hreq->url = url;
139 hreq->lentail = hreq->lenurl = strlen(url);
141 /* init the post processing */
142 if (method == afb_method_post) {
143 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
145 /* an empty post, let's process it as a get */
146 hreq->method = afb_method_get;
147 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
148 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
149 if (hreq->postform == NULL)
152 } else if (strcasestr(type, JSON_CONTENT) == NULL) {
153 reply_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
159 /* process further data */
160 if (*upload_data_size) {
161 if (hreq->postform != NULL) {
162 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size))
165 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size))
168 *upload_data_size = 0;
173 if (hreq->postform != NULL) {
174 rc = MHD_destroy_post_processor(hreq->postform);
175 hreq->postform = NULL;
180 /* search an handler for the request */
181 iter = hsrv->handlers;
183 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
184 if (iter->handler(hreq, iter->data))
186 hreq->tail = hreq->url;
187 hreq->lentail = hreq->lenurl;
193 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
197 reply_error(connection, MHD_HTTP_BAD_REQUEST);
201 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
205 /* Because of POST call multiple time requestApi we need to free POST handle here */
206 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
207 enum MHD_RequestTerminationCode toe)
209 struct afb_hreq *hreq;
213 MHD_suspend_connection (connection);
217 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
222 static struct hsrv_handler *new_handler(
223 struct hsrv_handler *head,
225 int (*handler) (struct afb_hreq *, void *),
229 struct hsrv_handler *link, *iter, *previous;
232 /* get the length of the prefix without its leading / */
233 length = strlen(prefix);
234 while (length && prefix[length - 1] == '/')
237 /* allocates the new link */
238 link = malloc(sizeof *link);
243 link->prefix = prefix;
244 link->length = length;
245 link->handler = handler;
247 link->priority = priority;
252 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
257 if (previous == NULL)
259 previous->next = link;
263 static int handle_alias(struct afb_hreq *hreq, void *data)
265 struct hsrv_alias *da = data;
267 if (hreq->method != afb_method_get) {
268 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
272 if (!afb_hreq_valid_tail(hreq)) {
273 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
277 return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
280 int afb_hsrv_add_handler(
281 struct afb_hsrv *hsrv,
283 int (*handler) (struct afb_hreq *, void *),
287 struct hsrv_handler *head;
289 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
292 hsrv->handlers = head;
296 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority)
298 struct hsrv_alias *da;
301 dirfd = open(alias, O_PATH|O_DIRECTORY);
306 da = malloc(sizeof *da);
309 da->directory = alias;
310 da->lendir = strlen(da->directory);
312 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
320 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
325 rc = asprintf(&dur, "%d", duration);
329 free(hsrv->cache_to);
330 hsrv->cache_to = dur;
334 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
337 struct MHD_Daemon *httpd;
338 const union MHD_DaemonInfo *info;
340 httpd = MHD_start_daemon(
341 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
343 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
344 access_handler, hsrv, /* Http Request Call back + extra attribute */
345 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
346 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
347 MHD_OPTION_END); /* options-end */
350 printf("Error: httpStart invalid httpd port: %d", (int)port);
354 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
356 MHD_stop_daemon(httpd);
357 fprintf(stderr, "Error: httpStart no pollfd");
361 upoll = upoll_open(info->listen_fd, httpd);
363 MHD_stop_daemon(httpd);
364 fprintf(stderr, "Error: connection to upoll of httpd failed");
367 upoll_on_readable(upoll, (void*)MHD_run);
374 void afb_hsrv_stop(struct afb_hsrv *hsrv)
377 upoll_close(hsrv->upoll);
379 if (hsrv->httpd != NULL)
380 MHD_stop_daemon(hsrv->httpd);
384 struct afb_hsrv *afb_hsrv_create()
386 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
388 result->refcount = 1;
392 void afb_hsrv_put(struct afb_hsrv *hsrv)
394 assert(hsrv->refcount != 0);
395 if (!--hsrv->refcount) {