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.
28 #include <microhttpd.h>
29 #include <json-c/json.h>
30 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
31 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
34 #if defined(USE_MAGIC_MIME_TYPE)
38 #include "afb-method.h"
39 #include "afb-msg-json.h"
40 #include "afb-context.h"
43 #include "afb-session.h"
46 #include "locale-root.h"
48 #define SIZE_RESPONSE_BUFFER 8192
50 static int global_reqids = 0;
52 static char empty_string[] = "";
54 static const char long_key_for_uuid[] = "x-afb-uuid";
55 static const char short_key_for_uuid[] = "uuid";
57 static const char long_key_for_token[] = "x-afb-token";
58 static const char short_key_for_token[] = "token";
60 static const char long_key_for_reqid[] = "x-afb-reqid";
61 static const char short_key_for_reqid[] = "reqid";
63 static char *cookie_name = NULL;
64 static char *cookie_setter = NULL;
65 static char *tmp_pattern = NULL;
68 * Structure for storing key/values read from POST requests
71 struct hreq_data *next; /* chain to next data */
72 char *key; /* key name */
73 size_t length; /* length of the value (used for appending) */
74 char *value; /* the value (or original filename) */
75 char *path; /* path of the file saved */
78 static struct json_object *req_json(struct afb_xreq *xreq);
79 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name);
80 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
81 static void req_destroy(struct afb_xreq *xreq);
83 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
90 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
92 struct hreq_data *data = hreq->data;
93 while (data != NULL) {
94 if (!strcasecmp(data->key, key))
99 data = calloc(1, sizeof *data);
101 data->key = strdup(key);
102 if (data->key == NULL) {
106 data->next = hreq->data;
114 /* a valid subpath is a relative path not looking deeper than root using .. */
115 static int validsubpath(const char *subpath)
120 switch (subpath[i++]) {
124 if (subpath[i] == '/') {
128 if (subpath[i++] == '.') {
134 if (subpath[i++] == '/') {
141 while (subpath[i] && subpath[i] != '/')
151 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
156 if (hreq->replied != 0)
159 k = va_arg(args, const char *);
161 v = va_arg(args, const char *);
162 MHD_add_response_header(response, k, v);
163 k = va_arg(args, const char *);
165 v = afb_context_sent_uuid(&hreq->xreq.context);
166 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
167 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
170 MHD_queue_response(hreq->connection, status, response);
171 MHD_destroy_response(response);
174 if (hreq->suspended != 0) {
175 MHD_resume_connection (hreq->connection);
177 afb_hsrv_run(hreq->hsrv);
181 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
184 va_start(args, response);
185 afb_hreq_reply_v(hreq, status, response, args);
189 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
192 va_start(args, status);
193 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
197 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
200 va_start(args, buffer);
201 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
205 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
208 va_start(args, buffer);
209 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
213 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
216 va_start(args, buffer);
217 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
221 #if defined(USE_MAGIC_MIME_TYPE)
223 #if !defined(MAGIC_DB)
224 #define MAGIC_DB "/usr/share/misc/magic.mgc"
227 static magic_t lazy_libmagic()
230 static magic_t result = NULL;
234 /* MAGIC_MIME tells magic to return a mime of the file,
235 but you can specify different things */
236 INFO("Loading mimetype default magic database");
237 result = magic_open(MAGIC_MIME_TYPE);
238 if (result == NULL) {
239 ERROR("unable to initialize magic library");
241 /* Warning: should not use NULL for DB
242 [libmagic bug wont pass efence check] */
243 else if (magic_load(result, MAGIC_DB) != 0) {
244 ERROR("cannot load magic database: %s", magic_error(result));
253 static const char *magic_mimetype_fd(int fd)
255 magic_t lib = lazy_libmagic();
256 return lib ? magic_descriptor(lib, fd) : NULL;
261 static const char *mimetype_fd_name(int fd, const char *filename)
263 const char *result = NULL;
265 #if defined(INFER_EXTENSION)
267 * Set some well-known extensions
268 * Note that it is mandatory for example for css files in order to provide
269 * right mimetype that must be text/css (otherwise chrome browser will not
270 * load correctly css file) while libmagic returns text/plain.
272 const char *extension = strrchr(filename, '.');
274 static const char *const known[][2] = {
275 /* keep it sorted for dichotomic search */
276 { ".css", "text/css" },
277 { ".gif", "image/gif" },
278 { ".html", "text/html" },
279 { ".htm", "text/html" },
280 { ".ico", "image/x-icon"},
281 { ".jpeg", "image/jpeg" },
282 { ".jpg", "image/jpeg" },
283 { ".js", "text/javascript" },
284 { ".json", "application/json" },
285 { ".mp3", "audio/mpeg" },
286 { ".png", "image/png" },
287 { ".svg", "image/svg+xml" },
288 { ".ttf", "application/x-font-ttf"},
289 { ".txt", "text/plain" },
290 { ".wav", "audio/x-wav" },
291 { ".xht", "application/xhtml+xml" },
292 { ".xhtml", "application/xhtml+xml" },
293 { ".xml", "application/xml" }
295 int i, c, l = 0, u = sizeof known / sizeof *known;
298 c = strcasecmp(extension, known[i][0]);
300 result = known[i][1];
310 #if defined(USE_MAGIC_MIME_TYPE)
312 result = magic_mimetype_fd(fd);
317 static void req_destroy(struct afb_xreq *xreq)
319 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
320 struct hreq_data *data;
322 if (hreq->postform != NULL)
323 MHD_destroy_post_processor(hreq->postform);
324 if (hreq->tokener != NULL)
325 json_tokener_free(hreq->tokener);
327 for (data = hreq->data; data; data = hreq->data) {
328 hreq->data = data->next;
337 afb_context_disconnect(&hreq->xreq.context);
338 json_object_put(hreq->json);
339 free((char*)hreq->xreq.request.called_api);
340 free((char*)hreq->xreq.request.called_verb);
341 afb_cred_unref(hreq->xreq.cred);
345 void afb_hreq_addref(struct afb_hreq *hreq)
347 afb_xreq_unhooked_addref(&hreq->xreq);
350 void afb_hreq_unref(struct afb_hreq *hreq)
353 hreq->xreq.replied = 1;
354 afb_xreq_unhooked_unref(&hreq->xreq);
358 * Removes the 'prefix' of 'length' from the tail of 'hreq'
359 * if and only if the prefix exists and is terminated by a leading
362 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
364 /* check the prefix ? */
365 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
366 || strncasecmp(prefix, hreq->tail, length))
369 /* removes successives / */
370 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
373 /* update the tail */
374 hreq->lentail -= length;
375 hreq->tail += length;
379 int afb_hreq_valid_tail(struct afb_hreq *hreq)
381 return validsubpath(hreq->tail);
384 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
386 afb_hreq_reply_empty(hreq, status, NULL);
389 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
393 if (hreq->url[hreq->lenurl - 1] == '/')
396 /* the redirect is needed for reliability of relative path */
397 tourl = alloca(hreq->lenurl + 2);
398 memcpy(tourl, hreq->url, hreq->lenurl);
399 tourl[hreq->lenurl] = '/';
400 tourl[hreq->lenurl + 1] = 0;
401 afb_hreq_redirect_to(hreq, tourl, 1);
405 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
411 char etag[1 + 2 * 8];
413 struct MHD_Response *response;
414 const char *mimetype;
416 /* Opens the file or directory */
418 fd = openat(dirfd, filename, O_RDONLY);
422 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
428 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
433 /* Retrieves file's status */
434 if (fstat(fd, &st) != 0) {
436 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
440 /* serve directory */
441 if (S_ISDIR(st.st_mode)) {
442 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
444 static const char *indexes[] = { "index.html", NULL };
446 while (indexes[i] != NULL) {
447 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
448 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
458 /* Don't serve special files */
459 if (!S_ISREG(st.st_mode)) {
461 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
465 /* Check the method */
466 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
468 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
472 /* computes the etag */
473 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
475 /* checks the etag */
476 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
477 if (inm && 0 == strcmp(inm, etag)) {
478 /* etag ok, return NOT MODIFIED */
480 DEBUG("Not Modified: [%s]", filename);
481 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
482 status = MHD_HTTP_NOT_MODIFIED;
485 if (st.st_size != (off_t) (size_t) st.st_size) {
487 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
491 /* create the response */
492 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
493 status = MHD_HTTP_OK;
496 mimetype = mimetype_fd_name(fd, filename);
497 if (mimetype != NULL)
498 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
501 /* fills the value and send */
502 afb_hreq_reply(hreq, status, response,
503 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
504 MHD_HTTP_HEADER_ETAG, etag,
509 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
511 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
513 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
517 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
523 char etag[1 + 2 * 8];
525 struct MHD_Response *response;
526 const char *mimetype;
528 /* Opens the file or directory */
529 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
533 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
537 /* Retrieves file's status */
538 if (fstat(fd, &st) != 0) {
540 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
544 /* serve directory */
545 if (S_ISDIR(st.st_mode)) {
546 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
548 static const char *indexes[] = { "index.html", NULL };
550 size_t length = strlen(filename);
551 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
552 memcpy(extname, filename, length);
553 if (length && extname[length - 1] != '/')
554 extname[length++] = '/';
555 while (rc == 0 && indexes[i] != NULL) {
556 strcpy(extname + length, indexes[i++]);
557 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
564 /* Don't serve special files */
565 if (!S_ISREG(st.st_mode)) {
567 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
571 /* Check the method */
572 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
574 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
578 /* computes the etag */
579 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
581 /* checks the etag */
582 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
583 if (inm && 0 == strcmp(inm, etag)) {
584 /* etag ok, return NOT MODIFIED */
586 DEBUG("Not Modified: [%s]", filename);
587 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
588 status = MHD_HTTP_NOT_MODIFIED;
591 if (st.st_size != (off_t) (size_t) st.st_size) {
593 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
597 /* create the response */
598 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
599 status = MHD_HTTP_OK;
602 mimetype = mimetype_fd_name(fd, filename);
603 if (mimetype != NULL)
604 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
607 /* fills the value and send */
608 afb_hreq_reply(hreq, status, response,
609 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
610 MHD_HTTP_HEADER_ETAG, etag,
615 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
617 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
619 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
630 static void _mkq_add_(struct _mkq_ *mkq, char value)
632 char *text = mkq->text;
634 if (mkq->length == mkq->alloc) {
636 text = realloc(text, mkq->alloc);
644 text[mkq->length++] = value;
648 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
650 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
653 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
656 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
657 _mkq_add_hex_(mkq, (char)(value & 15));
660 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
662 if (value <= ' ' || value >= 127)
663 _mkq_add_esc_(mkq, value);
669 _mkq_add_esc_(mkq, value);
672 _mkq_add_(mkq, value);
676 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
679 _mkq_add_char_(mkq, *value++);
682 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
684 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
685 _mkq_append_(mkq, key);
688 _mkq_append_(mkq, value);
693 static char *url_with_query(struct afb_hreq *hreq, const char *url)
698 mkq.length = strlen(url);
699 mkq.alloc = mkq.length + 1000;
700 mkq.text = malloc(mkq.alloc);
701 if (mkq.text != NULL) {
702 strcpy(mkq.text, url);
703 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
709 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
714 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
716 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
717 MHD_HTTP_HEADER_LOCATION, to, NULL);
718 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
722 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
724 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
727 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
729 struct hreq_data *data = get_data(hreq, name, 0);
730 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
733 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
735 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
738 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
741 struct hreq_data *hdat = get_data(hreq, key, 1);
742 if (hdat->path != NULL) {
745 p = realloc(hdat->value, hdat->length + size + 1);
750 memcpy(&hdat->value[hdat->length], data, size);
751 hdat->length += size;
752 hdat->value[hdat->length] = 0;
756 int afb_hreq_init_download_path(const char *directory)
762 if (access(directory, R_OK|W_OK)) {
763 /* no read/write access */
766 if (stat(directory, &st)) {
770 if (!S_ISDIR(st.st_mode)) {
771 /* not a directory */
775 n = strlen(directory);
776 while(n > 1 && directory[n-1] == '/') n--;
779 /* can't allocate memory */
783 memcpy(p, directory, n);
797 static int opentempfile(char **path)
802 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
806 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
814 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
818 struct hreq_data *hdat = get_data(hreq, key, 1);
820 if (hdat->value == NULL) {
821 hdat->value = strdup(file);
822 if (hdat->value == NULL)
824 fd = opentempfile(&hdat->path);
825 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
828 fd = open(hdat->path, O_WRONLY|O_APPEND);
833 sz = write(fd, data, size);
835 hdat->length += (size_t)sz;
838 } else if (errno != EINTR)
845 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
848 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
849 struct hreq_data *hdat = get_data(hreq, name, 0);
851 return (struct afb_arg){
853 .value = hdat->value,
857 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
858 return (struct afb_arg){
859 .name = value == NULL ? NULL : name,
865 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
867 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
871 static struct json_object *req_json(struct afb_xreq *xreq)
873 struct hreq_data *hdat;
874 struct json_object *obj, *val;
875 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
879 hreq->json = obj = json_object_new_object();
882 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
883 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
884 if (hdat->path == NULL)
885 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
887 val = json_object_new_object();
890 json_object_object_add(val, "file", json_object_new_string(hdat->value));
891 json_object_object_add(val, "path", json_object_new_string(hdat->path));
894 json_object_object_add(obj, hdat->key, val);
901 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
903 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;
904 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
907 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
909 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
910 struct json_object *sub, *reply;
912 struct MHD_Response *response;
914 /* create the reply */
915 reply = afb_msg_json_reply(object, error, info, &xreq->context);
917 /* append the req id on need */
918 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
920 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
921 if (reqid != NULL && json_object_object_get_ex(reply, "request", &sub))
922 json_object_object_add(sub, "reqid", json_object_new_string(reqid));
924 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);
925 afb_hreq_reply(hreq, MHD_HTTP_OK, response, NULL);
928 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
930 hreq->xreq.request.called_api = strndup(api, lenapi);
931 hreq->xreq.request.called_verb = strndup(verb, lenverb);
932 if (hreq->xreq.request.called_api == NULL || hreq->xreq.request.called_verb == NULL) {
933 ERROR("Out of memory");
934 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
935 } else if (afb_hreq_init_context(hreq) < 0) {
936 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
938 afb_xreq_unhooked_addref(&hreq->xreq);
939 afb_xreq_process(&hreq->xreq, apiset);
943 int afb_hreq_init_context(struct afb_hreq *hreq)
948 if (hreq->xreq.context.session != NULL)
951 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
953 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
955 uuid = afb_hreq_get_cookie(hreq, cookie_name);
957 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
959 token = afb_hreq_get_header(hreq, long_key_for_token);
961 token = afb_hreq_get_argument(hreq, long_key_for_token);
963 token = afb_hreq_get_argument(hreq, short_key_for_token);
965 return afb_context_connect(&hreq->xreq.context, uuid, token);
968 int afb_hreq_init_cookie(int port, const char *path, int maxage)
975 cookie_setter = NULL;
978 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
981 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
982 cookie_name, path, maxage);
988 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
993 struct afb_hreq *afb_hreq_create()
995 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
997 /* init the request */
998 afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
999 hreq->reqid = ++global_reqids;