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)
68 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
69 MHD_queue_response(connection, status, response);
70 MHD_destroy_response(response);
73 static int postproc(void *cls,
74 enum MHD_ValueKind kind,
77 const char *content_type,
78 const char *transfer_encoding,
83 struct afb_hreq *hreq = cls;
85 return afb_hreq_post_add_file(hreq, key, filename, data, size);
87 return afb_hreq_post_add(hreq, key, data, size);
90 static int access_handler(
92 struct MHD_Connection *connection,
94 const char *methodstr,
96 const char *upload_data,
97 size_t *upload_data_size,
101 struct afb_hreq *hreq;
102 enum afb_method method;
103 struct afb_hsrv *hsrv;
104 struct hsrv_handler *iter;
110 /* create the request */
111 hreq = calloc(1, sizeof *hreq);
117 method = get_method(methodstr);
118 method &= afb_method_get | afb_method_post;
119 if (method == afb_method_none)
122 /* init the request */
123 hreq->cacheTimeout = hsrv->cache_to;
124 hreq->connection = connection;
125 hreq->method = method;
126 hreq->version = version;
127 hreq->tail = hreq->url = url;
128 hreq->lentail = hreq->lenurl = strlen(url);
130 /* init the post processing */
131 if (method == afb_method_post) {
132 type = afb_hreq_get_header(hreq, MHD_HTTP_HEADER_CONTENT_TYPE);
134 /* an empty post, let's process it as a get */
135 hreq->method = afb_method_get;
136 } else if (strcasestr(type, FORM_CONTENT) != NULL) {
137 hreq->postform = MHD_create_post_processor (connection, 65500, postproc, hreq);
138 if (hreq->postform == NULL)
141 } else if (strcasestr(type, JSON_CONTENT) == NULL) {
142 reply_error(connection, MHD_HTTP_UNSUPPORTED_MEDIA_TYPE);
148 /* process further data */
149 if (*upload_data_size) {
150 if (hreq->postform != NULL) {
151 if (!MHD_post_process (hreq->postform, upload_data, *upload_data_size))
154 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size))
157 *upload_data_size = 0;
162 if (hreq->postform != NULL) {
163 rc = MHD_destroy_post_processor(hreq->postform);
164 hreq->postform = NULL;
169 /* search an handler for the request */
170 iter = hsrv->handlers;
172 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
173 if (iter->handler(hreq, iter->data))
175 hreq->tail = hreq->url;
176 hreq->lentail = hreq->lenurl;
182 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
186 reply_error(connection, MHD_HTTP_BAD_REQUEST);
190 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
194 /* Because of POST call multiple time requestApi we need to free POST handle here */
195 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
196 enum MHD_RequestTerminationCode toe)
198 struct afb_hreq *hreq;
202 MHD_suspend_connection (connection);
206 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
211 static struct hsrv_handler *new_handler(
212 struct hsrv_handler *head,
214 int (*handler) (struct afb_hreq *, void *),
218 struct hsrv_handler *link, *iter, *previous;
221 /* get the length of the prefix without its leading / */
222 length = strlen(prefix);
223 while (length && prefix[length - 1] == '/')
226 /* allocates the new link */
227 link = malloc(sizeof *link);
232 link->prefix = prefix;
233 link->length = length;
234 link->handler = handler;
236 link->priority = priority;
241 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
246 if (previous == NULL)
248 previous->next = link;
252 static int handle_alias(struct afb_hreq *hreq, void *data)
254 struct hsrv_alias *da = data;
256 if (hreq->method != afb_method_get) {
257 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
261 if (!afb_hreq_valid_tail(hreq)) {
262 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
266 return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
269 int afb_hsrv_add_handler(
270 struct afb_hsrv *hsrv,
272 int (*handler) (struct afb_hreq *, void *),
276 struct hsrv_handler *head;
278 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
281 hsrv->handlers = head;
285 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority)
287 struct hsrv_alias *da;
290 dirfd = open(alias, O_PATH|O_DIRECTORY);
295 da = malloc(sizeof *da);
298 da->directory = alias;
299 da->lendir = strlen(da->directory);
301 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
309 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
314 rc = asprintf(&dur, "%d", duration);
318 free(hsrv->cache_to);
319 hsrv->cache_to = dur;
323 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
326 struct MHD_Daemon *httpd;
327 const union MHD_DaemonInfo *info;
329 httpd = MHD_start_daemon(
330 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
332 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
333 access_handler, hsrv, /* Http Request Call back + extra attribute */
334 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
335 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
336 MHD_OPTION_END); /* options-end */
339 fprintf(stderr, "Error: httpStart invalid httpd port: %d", (int)port);
343 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
345 MHD_stop_daemon(httpd);
346 fprintf(stderr, "Error: httpStart no pollfd");
350 upoll = upoll_open(info->listen_fd, httpd);
352 MHD_stop_daemon(httpd);
353 fprintf(stderr, "Error: connection to upoll of httpd failed");
356 upoll_on_readable(upoll, (void*)MHD_run);
363 void afb_hsrv_stop(struct afb_hsrv *hsrv)
366 upoll_close(hsrv->upoll);
368 if (hsrv->httpd != NULL)
369 MHD_stop_daemon(hsrv->httpd);
373 struct afb_hsrv *afb_hsrv_create()
375 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
377 result->refcount = 1;
381 void afb_hsrv_put(struct afb_hsrv *hsrv)
383 assert(hsrv->refcount != 0);
384 if (!--hsrv->refcount) {