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 int global_reqids = 0;
68 static void reply_error(struct MHD_Connection *connection, unsigned int status)
70 struct MHD_Response *response = MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT);
71 MHD_queue_response(connection, status, response);
72 MHD_destroy_response(response);
75 static int postproc(void *cls,
76 enum MHD_ValueKind kind,
79 const char *content_type,
80 const char *transfer_encoding,
85 struct afb_hreq *hreq = cls;
87 return afb_hreq_post_add_file(hreq, key, filename, data, size);
89 return afb_hreq_post_add(hreq, key, data, size);
92 static int access_handler(
94 struct MHD_Connection *connection,
96 const char *methodstr,
98 const char *upload_data,
99 size_t *upload_data_size,
103 struct afb_hreq *hreq;
104 enum afb_method method;
105 struct afb_hsrv *hsrv;
106 struct hsrv_handler *iter;
113 method = get_method(methodstr);
114 method &= afb_method_get | afb_method_post;
115 if (method == afb_method_none) {
116 reply_error(connection, MHD_HTTP_BAD_REQUEST);
120 /* create the request */
121 hreq = calloc(1, sizeof *hreq);
123 reply_error(connection, MHD_HTTP_INTERNAL_SERVER_ERROR);
127 /* init the request */
129 hreq->cacheTimeout = hsrv->cache_to;
130 hreq->reqid = ++global_reqids;
134 hreq->connection = connection;
135 hreq->method = method;
136 hreq->version = version;
137 hreq->tail = hreq->url = url;
138 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)
150 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
152 } else if (strcasestr(type, JSON_CONTENT) == NULL) {
153 afb_hreq_reply_error(hreq, 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)) {
163 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
167 if (!afb_hreq_post_add(hreq, "", upload_data, *upload_data_size)) {
168 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
172 *upload_data_size = 0;
177 if (hreq->postform != NULL) {
178 rc = MHD_destroy_post_processor(hreq->postform);
179 hreq->postform = NULL;
181 afb_hreq_reply_error(hreq, MHD_HTTP_BAD_REQUEST);
186 if (hreq->scanned != 0) {
187 if (hreq->replied == 0 && hreq->suspended == 0) {
188 MHD_suspend_connection (connection);
194 /* search an handler for the request */
196 iter = hsrv->handlers;
198 if (afb_hreq_unprefix(hreq, iter->prefix, iter->length)) {
199 if (iter->handler(hreq, iter->data)) {
200 if (hreq->replied == 0 && hreq->suspended == 0) {
201 MHD_suspend_connection (connection);
206 hreq->tail = hreq->url;
207 hreq->lentail = hreq->lenurl;
213 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
217 /* Because of POST call multiple time requestApi we need to free POST handle here */
218 static void end_handler(void *cls, struct MHD_Connection *connection, void **recordreq,
219 enum MHD_RequestTerminationCode toe)
221 struct afb_hreq *hreq;
225 MHD_suspend_connection (connection);
229 void run_micro_httpd(struct afb_hsrv *hsrv)
231 if (hsrv->in_run != 0)
234 upoll_on_readable(hsrv->upoll, NULL);
237 MHD_run(hsrv->httpd);
238 } while(hsrv->in_run == 2);
240 upoll_on_readable(hsrv->upoll, (void*)run_micro_httpd);
244 static int new_client_handler(void *cls, const struct sockaddr *addr, socklen_t addrlen)
249 static struct hsrv_handler *new_handler(
250 struct hsrv_handler *head,
252 int (*handler) (struct afb_hreq *, void *),
256 struct hsrv_handler *link, *iter, *previous;
259 /* get the length of the prefix without its leading / */
260 length = strlen(prefix);
261 while (length && prefix[length - 1] == '/')
264 /* allocates the new link */
265 link = malloc(sizeof *link);
270 link->prefix = prefix;
271 link->length = length;
272 link->handler = handler;
274 link->priority = priority;
279 while (iter && (priority < iter->priority || (priority == iter->priority && length <= iter->length))) {
284 if (previous == NULL)
286 previous->next = link;
290 static int handle_alias(struct afb_hreq *hreq, void *data)
292 struct hsrv_alias *da = data;
294 if (hreq->method != afb_method_get) {
295 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
299 if (!afb_hreq_valid_tail(hreq)) {
300 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
304 return afb_hreq_reply_file(hreq, da->dirfd, &hreq->tail[1]);
307 int afb_hsrv_add_handler(
308 struct afb_hsrv *hsrv,
310 int (*handler) (struct afb_hreq *, void *),
314 struct hsrv_handler *head;
316 head = new_handler(hsrv->handlers, prefix, handler, data, priority);
319 hsrv->handlers = head;
323 int afb_hsrv_add_alias(struct afb_hsrv *hsrv, const char *prefix, const char *alias, int priority)
325 struct hsrv_alias *da;
328 dirfd = open(alias, O_PATH|O_DIRECTORY);
333 da = malloc(sizeof *da);
336 da->directory = alias;
337 da->lendir = strlen(da->directory);
339 if (afb_hsrv_add_handler(hsrv, prefix, handle_alias, da, priority))
347 int afb_hsrv_set_cache_timeout(struct afb_hsrv *hsrv, int duration)
352 rc = asprintf(&dur, "%d", duration);
356 free(hsrv->cache_to);
357 hsrv->cache_to = dur;
361 int afb_hsrv_start(struct afb_hsrv *hsrv, uint16_t port, unsigned int connection_timeout)
364 struct MHD_Daemon *httpd;
365 const union MHD_DaemonInfo *info;
367 httpd = MHD_start_daemon(
368 MHD_USE_EPOLL_LINUX_ONLY | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG | MHD_USE_SUSPEND_RESUME,
370 new_client_handler, NULL, /* Tcp Accept call back + extra attribute */
371 access_handler, hsrv, /* Http Request Call back + extra attribute */
372 MHD_OPTION_NOTIFY_COMPLETED, end_handler, hsrv,
373 MHD_OPTION_CONNECTION_TIMEOUT, connection_timeout,
374 MHD_OPTION_END); /* options-end */
377 fprintf(stderr, "Error: httpStart invalid httpd port: %d", (int)port);
381 info = MHD_get_daemon_info(httpd, MHD_DAEMON_INFO_EPOLL_FD_LINUX_ONLY);
383 MHD_stop_daemon(httpd);
384 fprintf(stderr, "Error: httpStart no pollfd");
388 upoll = upoll_open(info->listen_fd, hsrv);
390 MHD_stop_daemon(httpd);
391 fprintf(stderr, "Error: connection to upoll of httpd failed");
394 upoll_on_readable(upoll, (void*)run_micro_httpd);
401 void afb_hsrv_stop(struct afb_hsrv *hsrv)
404 upoll_close(hsrv->upoll);
406 if (hsrv->httpd != NULL)
407 MHD_stop_daemon(hsrv->httpd);
411 struct afb_hsrv *afb_hsrv_create()
413 struct afb_hsrv *result = calloc(1, sizeof(struct afb_hsrv));
415 result->refcount = 1;
419 void afb_hsrv_put(struct afb_hsrv *hsrv)
421 assert(hsrv->refcount != 0);
422 if (!--hsrv->refcount) {