2 * Copyright (C) 2016-2019 "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.
18 #if WITH_LIBMICROHTTPD
30 #include <microhttpd.h>
31 #include <json-c/json.h>
32 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
33 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
36 #if USE_MAGIC_MIME_TYPE
40 #include "afb-method.h"
41 #include "afb-msg-json.h"
42 #include "afb-context.h"
45 #include "afb-session.h"
48 #include "locale-root.h"
50 #define SIZE_RESPONSE_BUFFER 8192
52 static int global_reqids = 0;
54 static char empty_string[] = "";
56 static const char long_key_for_uuid[] = "x-afb-uuid";
57 static const char short_key_for_uuid[] = "uuid";
59 static const char long_key_for_token[] = "x-afb-token";
60 static const char short_key_for_token[] = "token";
62 static const char long_key_for_reqid[] = "x-afb-reqid";
63 static const char short_key_for_reqid[] = "reqid";
65 static char *cookie_name = NULL;
66 static char *cookie_setter = NULL;
67 static char *tmp_pattern = NULL;
70 * Structure for storing key/values read from POST requests
73 struct hreq_data *next; /* chain to next data */
74 char *key; /* key name */
75 size_t length; /* length of the value (used for appending) */
76 char *value; /* the value (or original filename) */
77 char *path; /* path of the file saved */
80 static struct json_object *req_json(struct afb_xreq *xreq);
81 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name);
82 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
83 static void req_destroy(struct afb_xreq *xreq);
85 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
92 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
94 struct hreq_data *data = hreq->data;
95 while (data != NULL) {
96 if (!strcasecmp(data->key, key))
101 data = calloc(1, sizeof *data);
103 data->key = strdup(key);
104 if (data->key == NULL) {
108 data->next = hreq->data;
116 /* a valid subpath is a relative path not looking deeper than root using .. */
117 static int validsubpath(const char *subpath)
122 switch (subpath[i++]) {
126 if (subpath[i] == '/') {
130 if (subpath[i++] == '.') {
136 if (subpath[i++] == '/') {
143 while (subpath[i] && subpath[i] != '/')
153 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
158 if (hreq->replied != 0)
161 k = va_arg(args, const char *);
163 v = va_arg(args, const char *);
164 MHD_add_response_header(response, k, v);
165 k = va_arg(args, const char *);
167 v = afb_context_sent_uuid(&hreq->xreq.context);
168 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
169 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
172 MHD_queue_response(hreq->connection, status, response);
173 MHD_destroy_response(response);
176 if (hreq->suspended != 0) {
177 MHD_resume_connection (hreq->connection);
179 afb_hsrv_run(hreq->hsrv);
183 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
186 va_start(args, response);
187 afb_hreq_reply_v(hreq, status, response, args);
191 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
194 va_start(args, status);
195 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
199 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
202 va_start(args, buffer);
203 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
207 void afb_hreq_reply_copy(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_MUST_COPY), args);
215 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
218 va_start(args, buffer);
219 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
223 #if USE_MAGIC_MIME_TYPE
225 #if !defined(MAGIC_DB)
226 #define MAGIC_DB "/usr/share/misc/magic.mgc"
229 static magic_t lazy_libmagic()
232 static magic_t result = NULL;
236 /* MAGIC_MIME tells magic to return a mime of the file,
237 but you can specify different things */
238 INFO("Loading mimetype default magic database");
239 result = magic_open(MAGIC_MIME_TYPE);
240 if (result == NULL) {
241 ERROR("unable to initialize magic library");
243 /* Warning: should not use NULL for DB
244 [libmagic bug wont pass efence check] */
245 else if (magic_load(result, MAGIC_DB) != 0) {
246 ERROR("cannot load magic database: %s", magic_error(result));
255 static const char *magic_mimetype_fd(int fd)
257 magic_t lib = lazy_libmagic();
258 return lib ? magic_descriptor(lib, fd) : NULL;
263 static const char *mimetype_fd_name(int fd, const char *filename)
265 const char *result = NULL;
269 * Set some well-known extensions
270 * Note that it is mandatory for example for css files in order to provide
271 * right mimetype that must be text/css (otherwise chrome browser will not
272 * load correctly css file) while libmagic returns text/plain.
274 const char *extension = strrchr(filename, '.');
276 static const char *const known[][2] = {
277 /* keep it sorted for dichotomic search */
278 { ".css", "text/css" },
279 { ".gif", "image/gif" },
280 { ".html", "text/html" },
281 { ".htm", "text/html" },
282 { ".ico", "image/x-icon"},
283 { ".jpeg", "image/jpeg" },
284 { ".jpg", "image/jpeg" },
285 { ".js", "text/javascript" },
286 { ".json", "application/json" },
287 { ".mp3", "audio/mpeg" },
288 { ".png", "image/png" },
289 { ".svg", "image/svg+xml" },
290 { ".ttf", "application/x-font-ttf"},
291 { ".txt", "text/plain" },
292 { ".wav", "audio/x-wav" },
293 { ".xht", "application/xhtml+xml" },
294 { ".xhtml", "application/xhtml+xml" },
295 { ".xml", "application/xml" }
297 int i, c, l = 0, u = sizeof known / sizeof *known;
300 c = strcasecmp(extension, known[i][0]);
302 result = known[i][1];
312 #if USE_MAGIC_MIME_TYPE
314 result = magic_mimetype_fd(fd);
319 static void req_destroy(struct afb_xreq *xreq)
321 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
322 struct hreq_data *data;
324 if (hreq->postform != NULL)
325 MHD_destroy_post_processor(hreq->postform);
326 if (hreq->tokener != NULL)
327 json_tokener_free(hreq->tokener);
329 for (data = hreq->data; data; data = hreq->data) {
330 hreq->data = data->next;
339 afb_context_disconnect(&hreq->xreq.context);
340 json_object_put(hreq->json);
341 free((char*)hreq->xreq.request.called_api);
342 free((char*)hreq->xreq.request.called_verb);
343 afb_cred_unref(hreq->xreq.cred);
347 void afb_hreq_addref(struct afb_hreq *hreq)
349 afb_xreq_unhooked_addref(&hreq->xreq);
352 void afb_hreq_unref(struct afb_hreq *hreq)
355 hreq->xreq.replied = 1;
356 afb_xreq_unhooked_unref(&hreq->xreq);
360 * Removes the 'prefix' of 'length' from the tail of 'hreq'
361 * if and only if the prefix exists and is terminated by a leading
364 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
366 /* check the prefix ? */
367 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
368 || strncasecmp(prefix, hreq->tail, length))
371 /* removes successives / */
372 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
375 /* update the tail */
376 hreq->lentail -= length;
377 hreq->tail += length;
381 int afb_hreq_valid_tail(struct afb_hreq *hreq)
383 return validsubpath(hreq->tail);
386 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
388 afb_hreq_reply_empty(hreq, status, NULL);
391 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
395 if (hreq->url[hreq->lenurl - 1] == '/')
398 /* the redirect is needed for reliability of relative path */
399 tourl = alloca(hreq->lenurl + 2);
400 memcpy(tourl, hreq->url, hreq->lenurl);
401 tourl[hreq->lenurl] = '/';
402 tourl[hreq->lenurl + 1] = 0;
403 afb_hreq_redirect_to(hreq, tourl, 1);
407 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
413 char etag[1 + 2 * 8];
415 struct MHD_Response *response;
416 const char *mimetype;
418 /* Opens the file or directory */
420 fd = openat(dirfd, filename, O_RDONLY);
424 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
430 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
435 /* Retrieves file's status */
436 if (fstat(fd, &st) != 0) {
438 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
442 /* serve directory */
443 if (S_ISDIR(st.st_mode)) {
444 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
446 static const char *indexes[] = { "index.html", NULL };
448 while (indexes[i] != NULL) {
449 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
450 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
460 /* Don't serve special files */
461 if (!S_ISREG(st.st_mode)) {
463 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
467 /* Check the method */
468 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
470 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
474 /* computes the etag */
475 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
477 /* checks the etag */
478 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
479 if (inm && 0 == strcmp(inm, etag)) {
480 /* etag ok, return NOT MODIFIED */
482 DEBUG("Not Modified: [%s]", filename);
483 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
484 status = MHD_HTTP_NOT_MODIFIED;
487 if (st.st_size != (off_t) (size_t) st.st_size) {
489 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
493 /* create the response */
494 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
495 status = MHD_HTTP_OK;
498 mimetype = mimetype_fd_name(fd, filename);
499 if (mimetype != NULL)
500 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
503 /* fills the value and send */
504 afb_hreq_reply(hreq, status, response,
505 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
506 MHD_HTTP_HEADER_ETAG, etag,
511 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
513 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
515 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
519 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
525 char etag[1 + 2 * 8];
527 struct MHD_Response *response;
528 const char *mimetype;
530 /* Opens the file or directory */
531 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
535 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
539 /* Retrieves file's status */
540 if (fstat(fd, &st) != 0) {
542 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
546 /* serve directory */
547 if (S_ISDIR(st.st_mode)) {
548 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
550 static const char *indexes[] = { "index.html", NULL };
552 size_t length = strlen(filename);
553 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
554 memcpy(extname, filename, length);
555 if (length && extname[length - 1] != '/')
556 extname[length++] = '/';
557 while (rc == 0 && indexes[i] != NULL) {
558 strcpy(extname + length, indexes[i++]);
559 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
566 /* Don't serve special files */
567 if (!S_ISREG(st.st_mode)) {
569 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
573 /* Check the method */
574 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
576 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
580 /* computes the etag */
581 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
583 /* checks the etag */
584 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
585 if (inm && 0 == strcmp(inm, etag)) {
586 /* etag ok, return NOT MODIFIED */
588 DEBUG("Not Modified: [%s]", filename);
589 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
590 status = MHD_HTTP_NOT_MODIFIED;
593 if (st.st_size != (off_t) (size_t) st.st_size) {
595 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
599 /* create the response */
600 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
601 status = MHD_HTTP_OK;
604 mimetype = mimetype_fd_name(fd, filename);
605 if (mimetype != NULL)
606 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
609 /* fills the value and send */
610 afb_hreq_reply(hreq, status, response,
611 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
612 MHD_HTTP_HEADER_ETAG, etag,
617 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
619 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
621 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
632 static void _mkq_add_(struct _mkq_ *mkq, char value)
634 char *text = mkq->text;
636 if (mkq->length == mkq->alloc) {
638 text = realloc(text, mkq->alloc);
646 text[mkq->length++] = value;
650 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
652 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
655 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
658 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
659 _mkq_add_hex_(mkq, (char)(value & 15));
662 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
664 if (value <= ' ' || value >= 127)
665 _mkq_add_esc_(mkq, value);
671 _mkq_add_esc_(mkq, value);
674 _mkq_add_(mkq, value);
678 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
681 _mkq_add_char_(mkq, *value++);
684 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
686 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
687 _mkq_append_(mkq, key);
690 _mkq_append_(mkq, value);
695 static char *url_with_query(struct afb_hreq *hreq, const char *url)
700 mkq.length = strlen(url);
701 mkq.alloc = mkq.length + 1000;
702 mkq.text = malloc(mkq.alloc);
703 if (mkq.text != NULL) {
704 strcpy(mkq.text, url);
705 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
711 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
716 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
718 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
719 MHD_HTTP_HEADER_LOCATION, to, NULL);
720 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
724 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
726 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
729 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
731 struct hreq_data *data = get_data(hreq, name, 0);
732 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
735 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
737 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
740 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
743 struct hreq_data *hdat = get_data(hreq, key, 1);
744 if (hdat->path != NULL) {
747 p = realloc(hdat->value, hdat->length + size + 1);
752 memcpy(&hdat->value[hdat->length], data, size);
753 hdat->length += size;
754 hdat->value[hdat->length] = 0;
758 int afb_hreq_init_download_path(const char *directory)
764 if (access(directory, R_OK|W_OK)) {
765 /* no read/write access */
768 if (stat(directory, &st)) {
772 if (!S_ISDIR(st.st_mode)) {
773 /* not a directory */
777 n = strlen(directory);
778 while(n > 1 && directory[n-1] == '/') n--;
781 /* can't allocate memory */
785 memcpy(p, directory, n);
799 static int opentempfile(char **path)
804 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
808 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
816 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
820 struct hreq_data *hdat = get_data(hreq, key, 1);
822 if (hdat->value == NULL) {
823 hdat->value = strdup(file);
824 if (hdat->value == NULL)
826 fd = opentempfile(&hdat->path);
827 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
830 fd = open(hdat->path, O_WRONLY|O_APPEND);
835 sz = write(fd, data, size);
837 hdat->length += (size_t)sz;
840 } else if (errno != EINTR)
847 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
850 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
851 struct hreq_data *hdat = get_data(hreq, name, 0);
853 return (struct afb_arg){
855 .value = hdat->value,
859 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
860 return (struct afb_arg){
861 .name = value == NULL ? NULL : name,
867 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
869 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
873 static struct json_object *req_json(struct afb_xreq *xreq)
875 struct hreq_data *hdat;
876 struct json_object *obj, *val;
877 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
881 hreq->json = obj = json_object_new_object();
884 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
885 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
886 if (hdat->path == NULL)
887 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
889 val = json_object_new_object();
892 json_object_object_add(val, "file", json_object_new_string(hdat->value));
893 json_object_object_add(val, "path", json_object_new_string(hdat->path));
896 json_object_object_add(obj, hdat->key, val);
903 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
905 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE)+pos, max) - buf;
906 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
909 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
911 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
912 struct json_object *sub, *reply;
914 struct MHD_Response *response;
916 /* create the reply */
917 reply = afb_msg_json_reply(object, error, info, &xreq->context);
919 /* append the req id on need */
920 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
922 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
923 if (reqid != NULL && json_object_object_get_ex(reply, "request", &sub))
924 json_object_object_add(sub, "reqid", json_object_new_string(reqid));
926 response = MHD_create_response_from_callback((uint64_t)strlen(json_object_to_json_string_ext(reply, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE)), SIZE_RESPONSE_BUFFER, (void*)send_json_cb, reply, (void*)json_object_put);
927 afb_hreq_reply(hreq, MHD_HTTP_OK, response, NULL);
930 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
932 hreq->xreq.request.called_api = strndup(api, lenapi);
933 hreq->xreq.request.called_verb = strndup(verb, lenverb);
934 if (hreq->xreq.request.called_api == NULL || hreq->xreq.request.called_verb == NULL) {
935 ERROR("Out of memory");
936 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
937 } else if (afb_hreq_init_context(hreq) < 0) {
938 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
940 afb_xreq_unhooked_addref(&hreq->xreq);
941 afb_xreq_process(&hreq->xreq, apiset);
945 int afb_hreq_init_context(struct afb_hreq *hreq)
950 if (hreq->xreq.context.session != NULL)
953 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
955 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
957 uuid = afb_hreq_get_cookie(hreq, cookie_name);
959 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
961 token = afb_hreq_get_header(hreq, long_key_for_token);
963 token = afb_hreq_get_argument(hreq, long_key_for_token);
965 token = afb_hreq_get_argument(hreq, short_key_for_token);
967 return afb_context_connect(&hreq->xreq.context, uuid, token);
970 int afb_hreq_init_cookie(int port, const char *path, int maxage)
977 cookie_setter = NULL;
980 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
983 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
984 cookie_name, path, maxage);
990 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
995 struct afb_hreq *afb_hreq_create()
997 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
999 /* init the request */
1000 afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
1001 hreq->reqid = ++global_reqids;