2 * Copyright (C) 2016, 2017, 2018 "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-msg-json.h"
37 #include "afb-context.h"
40 #include "afb-session.h"
43 #include "locale-root.h"
45 #define SIZE_RESPONSE_BUFFER 8192
47 static int global_reqids = 0;
49 static char empty_string[] = "";
51 static const char long_key_for_uuid[] = "x-afb-uuid";
52 static const char short_key_for_uuid[] = "uuid";
54 static const char long_key_for_token[] = "x-afb-token";
55 static const char short_key_for_token[] = "token";
57 static const char long_key_for_reqid[] = "x-afb-reqid";
58 static const char short_key_for_reqid[] = "reqid";
60 static char *cookie_name = NULL;
61 static char *cookie_setter = NULL;
62 static char *tmp_pattern = NULL;
65 * Structure for storing key/values read from POST requests
68 struct hreq_data *next; /* chain to next data */
69 char *key; /* key name */
70 size_t length; /* length of the value (used for appending) */
71 char *value; /* the value (or original filename) */
72 char *path; /* path of the file saved */
75 static struct json_object *req_json(struct afb_xreq *xreq);
76 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name);
77 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
78 static void req_destroy(struct afb_xreq *xreq);
80 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
87 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
89 struct hreq_data *data = hreq->data;
90 while (data != NULL) {
91 if (!strcasecmp(data->key, key))
96 data = calloc(1, sizeof *data);
98 data->key = strdup(key);
99 if (data->key == NULL) {
103 data->next = hreq->data;
111 /* a valid subpath is a relative path not looking deeper than root using .. */
112 static int validsubpath(const char *subpath)
117 switch (subpath[i++]) {
121 if (subpath[i] == '/') {
125 if (subpath[i++] == '.') {
131 if (subpath[i++] == '/') {
138 while (subpath[i] && subpath[i] != '/')
148 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
153 if (hreq->replied != 0)
156 k = va_arg(args, const char *);
158 v = va_arg(args, const char *);
159 MHD_add_response_header(response, k, v);
160 k = va_arg(args, const char *);
162 v = afb_context_sent_uuid(&hreq->xreq.context);
163 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
164 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
167 MHD_queue_response(hreq->connection, status, response);
168 MHD_destroy_response(response);
171 if (hreq->suspended != 0) {
172 MHD_resume_connection (hreq->connection);
174 afb_hsrv_run(hreq->hsrv);
178 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
181 va_start(args, response);
182 afb_hreq_reply_v(hreq, status, response, args);
186 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
189 va_start(args, status);
190 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
194 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
197 va_start(args, buffer);
198 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
202 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
205 va_start(args, buffer);
206 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
210 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
213 va_start(args, buffer);
214 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
218 #if defined(USE_MAGIC_MIME_TYPE)
220 #if !defined(MAGIC_DB)
221 #define MAGIC_DB "/usr/share/misc/magic.mgc"
224 static magic_t lazy_libmagic()
227 static magic_t result = NULL;
231 /* MAGIC_MIME tells magic to return a mime of the file,
232 but you can specify different things */
233 INFO("Loading mimetype default magic database");
234 result = magic_open(MAGIC_MIME_TYPE);
235 if (result == NULL) {
236 ERROR("unable to initialize magic library");
238 /* Warning: should not use NULL for DB
239 [libmagic bug wont pass efence check] */
240 else if (magic_load(result, MAGIC_DB) != 0) {
241 ERROR("cannot load magic database: %s", magic_error(result));
250 static const char *magic_mimetype_fd(int fd)
252 magic_t lib = lazy_libmagic();
253 return lib ? magic_descriptor(lib, fd) : NULL;
258 static const char *mimetype_fd_name(int fd, const char *filename)
260 const char *result = NULL;
262 #if defined(INFER_EXTENSION)
264 * Set some well-known extensions
265 * Note that it is mandatory for example for css files in order to provide
266 * right mimetype that must be text/css (otherwise chrome browser will not
267 * load correctly css file) while libmagic returns text/plain.
269 const char *extension = strrchr(filename, '.');
271 static const char *const known[][2] = {
272 /* keep it sorted for dichotomic search */
273 { ".css", "text/css" },
274 { ".gif", "image/gif" },
275 { ".html", "text/html" },
276 { ".htm", "text/html" },
277 { ".ico", "image/x-icon"},
278 { ".jpeg", "image/jpeg" },
279 { ".jpg", "image/jpeg" },
280 { ".js", "text/javascript" },
281 { ".json", "application/json" },
282 { ".mp3", "audio/mpeg" },
283 { ".png", "image/png" },
284 { ".svg", "image/svg+xml" },
285 { ".ttf", "application/x-font-ttf"},
286 { ".txt", "text/plain" },
287 { ".wav", "audio/x-wav" },
288 { ".xht", "application/xhtml+xml" },
289 { ".xhtml", "application/xhtml+xml" },
290 { ".xml", "application/xml" }
292 int i, c, l = 0, u = sizeof known / sizeof *known;
295 c = strcasecmp(extension, known[i][0]);
297 result = known[i][1];
307 #if defined(USE_MAGIC_MIME_TYPE)
309 result = magic_mimetype_fd(fd);
314 static void req_destroy(struct afb_xreq *xreq)
316 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
317 struct hreq_data *data;
319 if (hreq->postform != NULL)
320 MHD_destroy_post_processor(hreq->postform);
321 if (hreq->tokener != NULL)
322 json_tokener_free(hreq->tokener);
324 for (data = hreq->data; data; data = hreq->data) {
325 hreq->data = data->next;
334 afb_context_disconnect(&hreq->xreq.context);
335 json_object_put(hreq->json);
336 free((char*)hreq->xreq.request.called_api);
337 free((char*)hreq->xreq.request.called_verb);
338 afb_cred_unref(hreq->xreq.cred);
342 void afb_hreq_addref(struct afb_hreq *hreq)
344 afb_xreq_unhooked_addref(&hreq->xreq);
347 void afb_hreq_unref(struct afb_hreq *hreq)
350 hreq->xreq.replied = 1;
351 afb_xreq_unhooked_unref(&hreq->xreq);
355 * Removes the 'prefix' of 'length' from the tail of 'hreq'
356 * if and only if the prefix exists and is terminated by a leading
359 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
361 /* check the prefix ? */
362 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
363 || strncasecmp(prefix, hreq->tail, length))
366 /* removes successives / */
367 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
370 /* update the tail */
371 hreq->lentail -= length;
372 hreq->tail += length;
376 int afb_hreq_valid_tail(struct afb_hreq *hreq)
378 return validsubpath(hreq->tail);
381 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
383 afb_hreq_reply_empty(hreq, status, NULL);
386 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
390 if (hreq->url[hreq->lenurl - 1] == '/')
393 /* the redirect is needed for reliability of relative path */
394 tourl = alloca(hreq->lenurl + 2);
395 memcpy(tourl, hreq->url, hreq->lenurl);
396 tourl[hreq->lenurl] = '/';
397 tourl[hreq->lenurl + 1] = 0;
398 afb_hreq_redirect_to(hreq, tourl, 1);
402 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
408 char etag[1 + 2 * 8];
410 struct MHD_Response *response;
411 const char *mimetype;
413 /* Opens the file or directory */
415 fd = openat(dirfd, filename, O_RDONLY);
419 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
425 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
430 /* Retrieves file's status */
431 if (fstat(fd, &st) != 0) {
433 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
437 /* serve directory */
438 if (S_ISDIR(st.st_mode)) {
439 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
441 static const char *indexes[] = { "index.html", NULL };
443 while (indexes[i] != NULL) {
444 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
445 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
455 /* Don't serve special files */
456 if (!S_ISREG(st.st_mode)) {
458 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
462 /* Check the method */
463 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
465 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
469 /* computes the etag */
470 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
472 /* checks the etag */
473 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
474 if (inm && 0 == strcmp(inm, etag)) {
475 /* etag ok, return NOT MODIFIED */
477 DEBUG("Not Modified: [%s]", filename);
478 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
479 status = MHD_HTTP_NOT_MODIFIED;
482 if (st.st_size != (off_t) (size_t) st.st_size) {
484 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
488 /* create the response */
489 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
490 status = MHD_HTTP_OK;
493 mimetype = mimetype_fd_name(fd, filename);
494 if (mimetype != NULL)
495 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
498 /* fills the value and send */
499 afb_hreq_reply(hreq, status, response,
500 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
501 MHD_HTTP_HEADER_ETAG, etag,
506 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
508 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
510 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
514 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
520 char etag[1 + 2 * 8];
522 struct MHD_Response *response;
523 const char *mimetype;
525 /* Opens the file or directory */
526 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
530 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
534 /* Retrieves file's status */
535 if (fstat(fd, &st) != 0) {
537 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
541 /* serve directory */
542 if (S_ISDIR(st.st_mode)) {
543 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
545 static const char *indexes[] = { "index.html", NULL };
547 size_t length = strlen(filename);
548 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
549 memcpy(extname, filename, length);
550 if (length && extname[length - 1] != '/')
551 extname[length++] = '/';
552 while (rc == 0 && indexes[i] != NULL) {
553 strcpy(extname + length, indexes[i++]);
554 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
561 /* Don't serve special files */
562 if (!S_ISREG(st.st_mode)) {
564 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
568 /* Check the method */
569 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
571 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
575 /* computes the etag */
576 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
578 /* checks the etag */
579 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
580 if (inm && 0 == strcmp(inm, etag)) {
581 /* etag ok, return NOT MODIFIED */
583 DEBUG("Not Modified: [%s]", filename);
584 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
585 status = MHD_HTTP_NOT_MODIFIED;
588 if (st.st_size != (off_t) (size_t) st.st_size) {
590 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
594 /* create the response */
595 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
596 status = MHD_HTTP_OK;
599 mimetype = mimetype_fd_name(fd, filename);
600 if (mimetype != NULL)
601 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
604 /* fills the value and send */
605 afb_hreq_reply(hreq, status, response,
606 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
607 MHD_HTTP_HEADER_ETAG, etag,
612 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
614 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
616 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
627 static void _mkq_add_(struct _mkq_ *mkq, char value)
629 char *text = mkq->text;
631 if (mkq->length == mkq->alloc) {
633 text = realloc(text, mkq->alloc);
641 text[mkq->length++] = value;
645 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
647 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
650 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
653 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
654 _mkq_add_hex_(mkq, (char)(value & 15));
657 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
659 if (value <= ' ' || value >= 127)
660 _mkq_add_esc_(mkq, value);
666 _mkq_add_esc_(mkq, value);
669 _mkq_add_(mkq, value);
673 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
676 _mkq_add_char_(mkq, *value++);
679 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
681 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
682 _mkq_append_(mkq, key);
685 _mkq_append_(mkq, value);
690 static char *url_with_query(struct afb_hreq *hreq, const char *url)
695 mkq.length = strlen(url);
696 mkq.alloc = mkq.length + 1000;
697 mkq.text = malloc(mkq.alloc);
698 if (mkq.text != NULL) {
699 strcpy(mkq.text, url);
700 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
706 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
711 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
713 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
714 MHD_HTTP_HEADER_LOCATION, to, NULL);
715 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
719 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
721 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
724 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
726 struct hreq_data *data = get_data(hreq, name, 0);
727 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
730 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
732 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
735 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
738 struct hreq_data *hdat = get_data(hreq, key, 1);
739 if (hdat->path != NULL) {
742 p = realloc(hdat->value, hdat->length + size + 1);
747 memcpy(&hdat->value[hdat->length], data, size);
748 hdat->length += size;
749 hdat->value[hdat->length] = 0;
753 int afb_hreq_init_download_path(const char *directory)
759 if (access(directory, R_OK|W_OK)) {
760 /* no read/write access */
763 if (stat(directory, &st)) {
767 if (!S_ISDIR(st.st_mode)) {
768 /* not a directory */
772 n = strlen(directory);
773 while(n > 1 && directory[n-1] == '/') n--;
776 /* can't allocate memory */
780 memcpy(p, directory, n);
794 static int opentempfile(char **path)
799 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
803 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
811 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
815 struct hreq_data *hdat = get_data(hreq, key, 1);
817 if (hdat->value == NULL) {
818 hdat->value = strdup(file);
819 if (hdat->value == NULL)
821 fd = opentempfile(&hdat->path);
822 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
825 fd = open(hdat->path, O_WRONLY|O_APPEND);
830 sz = write(fd, data, size);
832 hdat->length += (size_t)sz;
835 } else if (errno != EINTR)
842 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
845 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
846 struct hreq_data *hdat = get_data(hreq, name, 0);
848 return (struct afb_arg){
850 .value = hdat->value,
854 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
855 return (struct afb_arg){
856 .name = value == NULL ? NULL : name,
862 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
864 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
868 static struct json_object *req_json(struct afb_xreq *xreq)
870 struct hreq_data *hdat;
871 struct json_object *obj, *val;
872 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
876 hreq->json = obj = json_object_new_object();
879 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
880 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
881 if (hdat->path == NULL)
882 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
884 val = json_object_new_object();
887 json_object_object_add(val, "file", json_object_new_string(hdat->value));
888 json_object_object_add(val, "path", json_object_new_string(hdat->path));
891 json_object_object_add(obj, hdat->key, val);
898 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
900 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
901 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
904 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
906 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
907 struct json_object *sub, *reply;
909 struct MHD_Response *response;
911 /* create the reply */
912 reply = afb_msg_json_reply(object, error, info, &xreq->context);
914 /* append the req id on need */
915 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
917 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
918 if (reqid != NULL && json_object_object_get_ex(reply, "request", &sub))
919 json_object_object_add(sub, "reqid", json_object_new_string(reqid));
921 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);
922 afb_hreq_reply(hreq, MHD_HTTP_OK, response, NULL);
925 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
927 hreq->xreq.request.called_api = strndup(api, lenapi);
928 hreq->xreq.request.called_verb = strndup(verb, lenverb);
929 if (hreq->xreq.request.called_api == NULL || hreq->xreq.request.called_verb == NULL) {
930 ERROR("Out of memory");
931 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
932 } else if (afb_hreq_init_context(hreq) < 0) {
933 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
935 afb_xreq_unhooked_addref(&hreq->xreq);
936 afb_xreq_process(&hreq->xreq, apiset);
940 int afb_hreq_init_context(struct afb_hreq *hreq)
945 if (hreq->xreq.context.session != NULL)
948 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
950 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
952 uuid = afb_hreq_get_cookie(hreq, cookie_name);
954 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
956 token = afb_hreq_get_header(hreq, long_key_for_token);
958 token = afb_hreq_get_argument(hreq, long_key_for_token);
960 token = afb_hreq_get_argument(hreq, short_key_for_token);
962 return afb_context_connect(&hreq->xreq.context, uuid, token);
965 int afb_hreq_init_cookie(int port, const char *path, int maxage)
972 cookie_setter = NULL;
975 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
978 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
979 cookie_name, path, maxage);
985 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
990 struct afb_hreq *afb_hreq_create()
992 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
994 /* init the request */
995 afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
996 hreq->reqid = ++global_reqids;