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.
28 #include <microhttpd.h>
29 #include <json-c/json.h>
31 #if defined(USE_MAGIC_MIME_TYPE)
35 #include "afb-method.h"
36 #include <afb/afb-req-itf.h>
37 #include "afb-msg-json.h"
38 #include "afb-context.h"
40 #include "afb-subcall.h"
44 #define SIZE_RESPONSE_BUFFER 8192
46 static char empty_string[] = "";
48 static const char long_key_for_uuid[] = "x-afb-uuid";
49 static const char short_key_for_uuid[] = "uuid";
51 static const char long_key_for_token[] = "x-afb-token";
52 static const char short_key_for_token[] = "token";
54 static const char long_key_for_reqid[] = "x-afb-reqid";
55 static const char short_key_for_reqid[] = "reqid";
57 static char *cookie_name = NULL;
58 static char *cookie_setter = NULL;
59 static char *tmp_pattern = NULL;
62 * Structure for storing key/values read from POST requests
65 struct hreq_data *next; /* chain to next data */
66 char *key; /* key name */
67 size_t length; /* length of the value (used for appending) */
68 char *value; /* the value (or original filename) */
69 char *path; /* path of the file saved */
72 static struct json_object *req_json(struct afb_hreq *hreq);
73 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name);
74 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info);
75 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info);
76 static const char *req_raw(struct afb_hreq *hreq, size_t *size);
77 static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size);
78 static int req_subscribe_unsubscribe_error(struct afb_hreq *hreq, struct afb_event event);
79 static void req_subcall(struct afb_hreq *hreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure);
81 const struct afb_req_itf afb_hreq_req_itf = {
82 .json = (void*)req_json,
83 .get = (void*)req_get,
84 .success = (void*)req_success,
85 .fail = (void*)req_fail,
86 .raw = (void*)req_raw,
87 .send = (void*)req_send,
88 .context_get = (void*)afb_context_get,
89 .context_set = (void*)afb_context_set,
90 .addref = (void*)afb_hreq_addref,
91 .unref = (void*)afb_hreq_unref,
92 .session_close = (void*)afb_context_close,
93 .session_set_LOA = (void*)afb_context_change_loa,
94 .subscribe = (void*)req_subscribe_unsubscribe_error,
95 .unsubscribe = (void*)req_subscribe_unsubscribe_error,
96 .subcall = (void*)req_subcall
99 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
101 struct hreq_data *data = hreq->data;
102 while (data != NULL) {
103 if (!strcasecmp(data->key, key))
108 data = calloc(1, sizeof *data);
110 data->key = strdup(key);
111 if (data->key == NULL) {
115 data->next = hreq->data;
123 /* a valid subpath is a relative path not looking deeper than root using .. */
124 static int validsubpath(const char *subpath)
129 switch (subpath[i++]) {
133 if (subpath[i] == '/') {
137 if (subpath[i++] == '.') {
143 if (subpath[i++] == '/') {
150 while (subpath[i] && subpath[i] != '/')
160 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
165 if (hreq->replied != 0)
168 k = va_arg(args, const char *);
170 v = va_arg(args, const char *);
171 MHD_add_response_header(response, k, v);
172 k = va_arg(args, const char *);
174 v = afb_context_sent_uuid(&hreq->context);
175 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
176 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
179 MHD_queue_response(hreq->connection, status, response);
180 MHD_destroy_response(response);
183 if (hreq->suspended != 0) {
184 extern void run_micro_httpd(struct afb_hsrv *hsrv);
185 MHD_resume_connection (hreq->connection);
187 run_micro_httpd(hreq->hsrv);
191 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
194 va_start(args, response);
195 afb_hreq_reply_v(hreq, status, response, args);
199 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
202 va_start(args, status);
203 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
207 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
210 va_start(args, buffer);
211 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
215 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
218 va_start(args, buffer);
219 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
223 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
226 va_start(args, buffer);
227 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
231 #if defined(USE_MAGIC_MIME_TYPE)
233 #if !defined(MAGIC_DB)
234 #define MAGIC_DB "/usr/share/misc/magic.mgc"
237 static magic_t lazy_libmagic()
240 static magic_t result = NULL;
244 /* MAGIC_MIME tells magic to return a mime of the file,
245 but you can specify different things */
246 INFO("Loading mimetype default magic database");
247 result = magic_open(MAGIC_MIME_TYPE);
248 if (result == NULL) {
249 ERROR("unable to initialize magic library");
251 /* Warning: should not use NULL for DB
252 [libmagic bug wont pass efence check] */
253 else if (magic_load(result, MAGIC_DB) != 0) {
254 ERROR("cannot load magic database: %s", magic_error(result));
263 static const char *magic_mimetype_fd(int fd)
265 magic_t lib = lazy_libmagic();
266 return lib ? magic_descriptor(lib, fd) : NULL;
271 static const char *mimetype_fd_name(int fd, const char *filename)
273 const char *result = NULL;
275 #if defined(INFER_EXTENSION)
276 const char *extension = strrchr(filename, '.');
278 static const char *const known[][2] = {
279 { ".js", "text/javascript" },
280 { ".html", "text/html" },
281 { ".css", "text/css" },
285 while (known[i][0]) {
286 if (!strcasecmp(extension, known[i][0])) {
287 result = known[i][1];
294 #if defined(USE_MAGIC_MIME_TYPE)
296 result = magic_mimetype_fd(fd);
301 void afb_hreq_addref(struct afb_hreq *hreq)
306 void afb_hreq_unref(struct afb_hreq *hreq)
308 struct hreq_data *data;
310 if (hreq == NULL || --hreq->refcount)
313 if (hreq->postform != NULL)
314 MHD_destroy_post_processor(hreq->postform);
315 for (data = hreq->data; data; data = hreq->data) {
316 hreq->data = data->next;
325 afb_context_disconnect(&hreq->context);
326 json_object_put(hreq->json);
331 * Removes the 'prefix' of 'length' from the tail of 'hreq'
332 * if and only if the prefix exists and is terminated by a leading
335 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
337 /* check the prefix ? */
338 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
339 || strncasecmp(prefix, hreq->tail, length))
342 /* removes successives / */
343 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
346 /* update the tail */
347 hreq->lentail -= length;
348 hreq->tail += length;
352 int afb_hreq_valid_tail(struct afb_hreq *hreq)
354 return validsubpath(hreq->tail);
357 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
359 afb_hreq_reply_empty(hreq, status, NULL);
362 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
368 char etag[1 + 2 * 8];
370 struct MHD_Response *response;
371 const char *mimetype;
373 /* Opens the file or directory */
375 fd = openat(dirfd, filename, O_RDONLY);
379 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
385 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
390 /* Retrieves file's status */
391 if (fstat(fd, &st) != 0) {
393 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
397 /* serve directory */
398 if (S_ISDIR(st.st_mode)) {
399 if (hreq->url[hreq->lenurl - 1] != '/') {
400 /* the redirect is needed for reliability of relative path */
401 char *tourl = alloca(hreq->lenurl + 2);
402 memcpy(tourl, hreq->url, hreq->lenurl);
403 tourl[hreq->lenurl] = '/';
404 tourl[hreq->lenurl + 1] = 0;
405 rc = afb_hreq_redirect_to(hreq, tourl, 1);
407 static const char *indexes[] = { "index.html", NULL };
410 while (indexes[i] != NULL) {
411 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
412 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
422 /* Don't serve special files */
423 if (!S_ISREG(st.st_mode)) {
425 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
429 /* Check the method */
430 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
432 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
436 /* computes the etag */
437 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
439 /* checks the etag */
440 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
441 if (inm && 0 == strcmp(inm, etag)) {
442 /* etag ok, return NOT MODIFIED */
444 DEBUG("Not Modified: [%s]", filename);
445 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
446 status = MHD_HTTP_NOT_MODIFIED;
449 if (st.st_size != (off_t) (size_t) st.st_size) {
451 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
455 /* create the response */
456 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
457 status = MHD_HTTP_OK;
460 mimetype = mimetype_fd_name(fd, filename);
461 if (mimetype != NULL)
462 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
465 /* fills the value and send */
466 afb_hreq_reply(hreq, status, response,
467 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
468 MHD_HTTP_HEADER_ETAG, etag,
473 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
475 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
477 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
488 static void _mkq_add_(struct _mkq_ *mkq, char value)
490 char *text = mkq->text;
492 if (mkq->length == mkq->alloc) {
494 text = realloc(text, mkq->alloc);
502 text[mkq->length++] = value;
506 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
508 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
511 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
514 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
515 _mkq_add_hex_(mkq, (char)(value & 15));
518 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
520 if (value <= ' ' || value >= 127)
521 _mkq_add_esc_(mkq, value);
527 _mkq_add_esc_(mkq, value);
530 _mkq_add_(mkq, value);
534 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
537 _mkq_add_char_(mkq, *value++);
540 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
542 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
543 _mkq_append_(mkq, key);
546 _mkq_append_(mkq, value);
551 static char *url_with_query(struct afb_hreq *hreq, const char *url)
556 mkq.length = strlen(url);
557 mkq.alloc = mkq.length + 1000;
558 mkq.text = malloc(mkq.alloc);
559 if (mkq.text != NULL) {
560 strcpy(mkq.text, url);
561 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
567 int afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
572 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
574 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
575 MHD_HTTP_HEADER_LOCATION, to, NULL);
576 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
581 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
583 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
586 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
588 struct hreq_data *data = get_data(hreq, name, 0);
589 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
592 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
594 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
597 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
600 struct hreq_data *hdat = get_data(hreq, key, 1);
601 if (hdat->path != NULL) {
604 p = realloc(hdat->value, hdat->length + size + 1);
609 memcpy(&hdat->value[hdat->length], data, size);
610 hdat->length += size;
611 hdat->value[hdat->length] = 0;
615 int afb_hreq_init_download_path(const char *directory)
621 if (access(directory, R_OK|W_OK)) {
622 /* no read/write access */
625 if (stat(directory, &st)) {
629 if (!S_ISDIR(st.st_mode)) {
630 /* not a directory */
634 n = strlen(directory);
635 while(n > 1 && directory[n-1] == '/') n--;
638 /* can't allocate memory */
642 memcpy(p, directory, n);
656 static int opentempfile(char **path)
661 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
665 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
673 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
677 struct hreq_data *hdat = get_data(hreq, key, 1);
679 if (hdat->value == NULL) {
680 hdat->value = strdup(file);
681 if (hdat->value == NULL)
683 fd = opentempfile(&hdat->path);
684 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
687 fd = open(hdat->path, O_WRONLY|O_APPEND);
692 sz = write(fd, data, size);
694 hdat->length += (size_t)sz;
697 } else if (errno != EINTR)
704 struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
706 return (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq };
709 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
712 struct hreq_data *hdat = get_data(hreq, name, 0);
714 return (struct afb_arg){
716 .value = hdat->value,
720 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
721 return (struct afb_arg){
722 .name = value == NULL ? NULL : name,
728 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
730 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
734 static struct json_object *req_json(struct afb_hreq *hreq)
736 struct hreq_data *hdat;
737 struct json_object *obj, *val;
741 hreq->json = obj = json_object_new_object();
744 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
745 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
746 if (hdat->path == NULL)
747 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
749 val = json_object_new_object();
752 json_object_object_add(val, "file", json_object_new_string(hdat->value));
753 json_object_object_add(val, "path", json_object_new_string(hdat->path));
756 json_object_object_add(obj, hdat->key, val);
763 static const char *req_raw(struct afb_hreq *hreq, size_t *size)
765 const char *result = json_object_get_string(req_json(hreq));
766 *size = result ? strlen(result) : 0;
770 static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size)
772 afb_hreq_reply_copy(hreq, MHD_HTTP_OK, size, buffer, NULL);
775 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
777 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
778 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
781 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
783 struct json_object *reply;
785 struct MHD_Response *response;
787 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
789 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
791 reply = afb_msg_json_reply(status, info, resp, &hreq->context, reqid);
793 response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PLAIN)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
794 afb_hreq_reply(hreq, retcode, response, NULL);
797 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info)
799 req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
802 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info)
804 req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
807 static int req_subscribe_unsubscribe_error(struct afb_hreq *hreq, struct afb_event event)
813 static void req_subcall(struct afb_hreq *hreq, const char *api, const char *verb, struct json_object *args, void (*callback)(void*, int, struct json_object*), void *closure)
815 afb_subcall(&hreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq });
818 int afb_hreq_init_context(struct afb_hreq *hreq)
823 if (hreq->context.session != NULL)
826 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
828 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
830 uuid = afb_hreq_get_cookie(hreq, cookie_name);
832 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
834 token = afb_hreq_get_header(hreq, long_key_for_token);
836 token = afb_hreq_get_argument(hreq, long_key_for_token);
838 token = afb_hreq_get_argument(hreq, short_key_for_token);
840 return afb_context_connect(&hreq->context, uuid, token);
843 int afb_hreq_init_cookie(int port, const char *path, int maxage)
850 cookie_setter = NULL;
853 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
856 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
857 cookie_name, path, maxage);