2 * Copyright (C) 2016, 2017 "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"
41 #include "afb-session.h"
43 #include "locale-root.h"
45 #define SIZE_RESPONSE_BUFFER 8192
47 static char empty_string[] = "";
49 static const char long_key_for_uuid[] = "x-afb-uuid";
50 static const char short_key_for_uuid[] = "uuid";
52 static const char long_key_for_token[] = "x-afb-token";
53 static const char short_key_for_token[] = "token";
55 static const char long_key_for_reqid[] = "x-afb-reqid";
56 static const char short_key_for_reqid[] = "reqid";
58 static char *cookie_name = NULL;
59 static char *cookie_setter = NULL;
60 static char *tmp_pattern = NULL;
63 * Structure for storing key/values read from POST requests
66 struct hreq_data *next; /* chain to next data */
67 char *key; /* key name */
68 size_t length; /* length of the value (used for appending) */
69 char *value; /* the value (or original filename) */
70 char *path; /* path of the file saved */
73 static struct json_object *req_json(struct afb_hreq *hreq);
74 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name);
75 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info);
76 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info);
77 static const char *req_raw(struct afb_hreq *hreq, size_t *size);
78 static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size);
79 static int req_subscribe_unsubscribe_error(struct afb_hreq *hreq, struct afb_event event);
80 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);
82 const struct afb_req_itf afb_hreq_req_itf = {
83 .json = (void*)req_json,
84 .get = (void*)req_get,
85 .success = (void*)req_success,
86 .fail = (void*)req_fail,
87 .raw = (void*)req_raw,
88 .send = (void*)req_send,
89 .context_get = (void*)afb_context_get,
90 .context_set = (void*)afb_context_set,
91 .addref = (void*)afb_hreq_addref,
92 .unref = (void*)afb_hreq_unref,
93 .session_close = (void*)afb_context_close,
94 .session_set_LOA = (void*)afb_context_change_loa,
95 .subscribe = (void*)req_subscribe_unsubscribe_error,
96 .unsubscribe = (void*)req_subscribe_unsubscribe_error,
97 .subcall = (void*)req_subcall
100 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
102 struct hreq_data *data = hreq->data;
103 while (data != NULL) {
104 if (!strcasecmp(data->key, key))
109 data = calloc(1, sizeof *data);
111 data->key = strdup(key);
112 if (data->key == NULL) {
116 data->next = hreq->data;
124 /* a valid subpath is a relative path not looking deeper than root using .. */
125 static int validsubpath(const char *subpath)
130 switch (subpath[i++]) {
134 if (subpath[i] == '/') {
138 if (subpath[i++] == '.') {
144 if (subpath[i++] == '/') {
151 while (subpath[i] && subpath[i] != '/')
161 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
166 if (hreq->replied != 0)
169 k = va_arg(args, const char *);
171 v = va_arg(args, const char *);
172 MHD_add_response_header(response, k, v);
173 k = va_arg(args, const char *);
175 v = afb_context_sent_uuid(&hreq->context);
176 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
177 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
180 MHD_queue_response(hreq->connection, status, response);
181 MHD_destroy_response(response);
184 if (hreq->suspended != 0) {
185 extern void run_micro_httpd(struct afb_hsrv *hsrv);
186 MHD_resume_connection (hreq->connection);
188 run_micro_httpd(hreq->hsrv);
192 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
195 va_start(args, response);
196 afb_hreq_reply_v(hreq, status, response, args);
200 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
203 va_start(args, status);
204 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
208 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
211 va_start(args, buffer);
212 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
216 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
219 va_start(args, buffer);
220 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
224 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
227 va_start(args, buffer);
228 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
232 #if defined(USE_MAGIC_MIME_TYPE)
234 #if !defined(MAGIC_DB)
235 #define MAGIC_DB "/usr/share/misc/magic.mgc"
238 static magic_t lazy_libmagic()
241 static magic_t result = NULL;
245 /* MAGIC_MIME tells magic to return a mime of the file,
246 but you can specify different things */
247 INFO("Loading mimetype default magic database");
248 result = magic_open(MAGIC_MIME_TYPE);
249 if (result == NULL) {
250 ERROR("unable to initialize magic library");
252 /* Warning: should not use NULL for DB
253 [libmagic bug wont pass efence check] */
254 else if (magic_load(result, MAGIC_DB) != 0) {
255 ERROR("cannot load magic database: %s", magic_error(result));
264 static const char *magic_mimetype_fd(int fd)
266 magic_t lib = lazy_libmagic();
267 return lib ? magic_descriptor(lib, fd) : NULL;
272 static const char *mimetype_fd_name(int fd, const char *filename)
274 const char *result = NULL;
276 #if defined(INFER_EXTENSION)
277 const char *extension = strrchr(filename, '.');
279 static const char *const known[][2] = {
280 { ".js", "text/javascript" },
281 { ".html", "text/html" },
282 { ".css", "text/css" },
286 while (known[i][0]) {
287 if (!strcasecmp(extension, known[i][0])) {
288 result = known[i][1];
295 #if defined(USE_MAGIC_MIME_TYPE)
297 result = magic_mimetype_fd(fd);
302 void afb_hreq_addref(struct afb_hreq *hreq)
307 void afb_hreq_unref(struct afb_hreq *hreq)
309 struct hreq_data *data;
311 if (hreq == NULL || --hreq->refcount)
314 if (hreq->postform != NULL)
315 MHD_destroy_post_processor(hreq->postform);
316 for (data = hreq->data; data; data = hreq->data) {
317 hreq->data = data->next;
326 afb_context_disconnect(&hreq->context);
327 json_object_put(hreq->json);
332 * Removes the 'prefix' of 'length' from the tail of 'hreq'
333 * if and only if the prefix exists and is terminated by a leading
336 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
338 /* check the prefix ? */
339 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
340 || strncasecmp(prefix, hreq->tail, length))
343 /* removes successives / */
344 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
347 /* update the tail */
348 hreq->lentail -= length;
349 hreq->tail += length;
353 int afb_hreq_valid_tail(struct afb_hreq *hreq)
355 return validsubpath(hreq->tail);
358 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
360 afb_hreq_reply_empty(hreq, status, NULL);
363 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
367 if (hreq->url[hreq->lenurl - 1] == '/')
370 /* the redirect is needed for reliability of relative path */
371 tourl = alloca(hreq->lenurl + 2);
372 memcpy(tourl, hreq->url, hreq->lenurl);
373 tourl[hreq->lenurl] = '/';
374 tourl[hreq->lenurl + 1] = 0;
375 afb_hreq_redirect_to(hreq, tourl, 1);
379 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
385 char etag[1 + 2 * 8];
387 struct MHD_Response *response;
388 const char *mimetype;
390 /* Opens the file or directory */
392 fd = openat(dirfd, filename, O_RDONLY);
396 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
402 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
407 /* Retrieves file's status */
408 if (fstat(fd, &st) != 0) {
410 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
414 /* serve directory */
415 if (S_ISDIR(st.st_mode)) {
416 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
418 static const char *indexes[] = { "index.html", NULL };
420 while (indexes[i] != NULL) {
421 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
422 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
432 /* Don't serve special files */
433 if (!S_ISREG(st.st_mode)) {
435 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
439 /* Check the method */
440 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
442 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
446 /* computes the etag */
447 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
449 /* checks the etag */
450 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
451 if (inm && 0 == strcmp(inm, etag)) {
452 /* etag ok, return NOT MODIFIED */
454 DEBUG("Not Modified: [%s]", filename);
455 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
456 status = MHD_HTTP_NOT_MODIFIED;
459 if (st.st_size != (off_t) (size_t) st.st_size) {
461 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
465 /* create the response */
466 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
467 status = MHD_HTTP_OK;
470 mimetype = mimetype_fd_name(fd, filename);
471 if (mimetype != NULL)
472 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
475 /* fills the value and send */
476 afb_hreq_reply(hreq, status, response,
477 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
478 MHD_HTTP_HEADER_ETAG, etag,
483 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
485 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
487 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
491 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
497 char etag[1 + 2 * 8];
499 struct MHD_Response *response;
500 const char *mimetype;
502 /* Opens the file or directory */
503 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
507 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
511 /* Retrieves file's status */
512 if (fstat(fd, &st) != 0) {
514 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
518 /* serve directory */
519 if (S_ISDIR(st.st_mode)) {
520 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
522 static const char *indexes[] = { "index.html", NULL };
524 size_t length = strlen(filename);
525 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
526 memcpy(extname, filename, length);
527 if (length && extname[length - 1] != '/')
528 extname[length++] = '/';
529 while (rc == 0 && indexes[i] != NULL) {
530 strcpy(extname + length, indexes[i++]);
531 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
538 /* Don't serve special files */
539 if (!S_ISREG(st.st_mode)) {
541 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
545 /* Check the method */
546 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
548 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
552 /* computes the etag */
553 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
555 /* checks the etag */
556 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
557 if (inm && 0 == strcmp(inm, etag)) {
558 /* etag ok, return NOT MODIFIED */
560 DEBUG("Not Modified: [%s]", filename);
561 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
562 status = MHD_HTTP_NOT_MODIFIED;
565 if (st.st_size != (off_t) (size_t) st.st_size) {
567 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
571 /* create the response */
572 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
573 status = MHD_HTTP_OK;
576 mimetype = mimetype_fd_name(fd, filename);
577 if (mimetype != NULL)
578 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
581 /* fills the value and send */
582 afb_hreq_reply(hreq, status, response,
583 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
584 MHD_HTTP_HEADER_ETAG, etag,
589 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
591 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
593 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
604 static void _mkq_add_(struct _mkq_ *mkq, char value)
606 char *text = mkq->text;
608 if (mkq->length == mkq->alloc) {
610 text = realloc(text, mkq->alloc);
618 text[mkq->length++] = value;
622 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
624 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
627 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
630 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
631 _mkq_add_hex_(mkq, (char)(value & 15));
634 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
636 if (value <= ' ' || value >= 127)
637 _mkq_add_esc_(mkq, value);
643 _mkq_add_esc_(mkq, value);
646 _mkq_add_(mkq, value);
650 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
653 _mkq_add_char_(mkq, *value++);
656 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
658 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
659 _mkq_append_(mkq, key);
662 _mkq_append_(mkq, value);
667 static char *url_with_query(struct afb_hreq *hreq, const char *url)
672 mkq.length = strlen(url);
673 mkq.alloc = mkq.length + 1000;
674 mkq.text = malloc(mkq.alloc);
675 if (mkq.text != NULL) {
676 strcpy(mkq.text, url);
677 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
683 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
688 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
690 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
691 MHD_HTTP_HEADER_LOCATION, to, NULL);
692 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
696 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
698 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
701 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
703 struct hreq_data *data = get_data(hreq, name, 0);
704 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
707 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
709 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
712 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
715 struct hreq_data *hdat = get_data(hreq, key, 1);
716 if (hdat->path != NULL) {
719 p = realloc(hdat->value, hdat->length + size + 1);
724 memcpy(&hdat->value[hdat->length], data, size);
725 hdat->length += size;
726 hdat->value[hdat->length] = 0;
730 int afb_hreq_init_download_path(const char *directory)
736 if (access(directory, R_OK|W_OK)) {
737 /* no read/write access */
740 if (stat(directory, &st)) {
744 if (!S_ISDIR(st.st_mode)) {
745 /* not a directory */
749 n = strlen(directory);
750 while(n > 1 && directory[n-1] == '/') n--;
753 /* can't allocate memory */
757 memcpy(p, directory, n);
771 static int opentempfile(char **path)
776 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
780 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
788 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
792 struct hreq_data *hdat = get_data(hreq, key, 1);
794 if (hdat->value == NULL) {
795 hdat->value = strdup(file);
796 if (hdat->value == NULL)
798 fd = opentempfile(&hdat->path);
799 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
802 fd = open(hdat->path, O_WRONLY|O_APPEND);
807 sz = write(fd, data, size);
809 hdat->length += (size_t)sz;
812 } else if (errno != EINTR)
819 struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
821 return (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq };
824 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
827 struct hreq_data *hdat = get_data(hreq, name, 0);
829 return (struct afb_arg){
831 .value = hdat->value,
835 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
836 return (struct afb_arg){
837 .name = value == NULL ? NULL : name,
843 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
845 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
849 static struct json_object *req_json(struct afb_hreq *hreq)
851 struct hreq_data *hdat;
852 struct json_object *obj, *val;
856 hreq->json = obj = json_object_new_object();
859 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
860 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
861 if (hdat->path == NULL)
862 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
864 val = json_object_new_object();
867 json_object_object_add(val, "file", json_object_new_string(hdat->value));
868 json_object_object_add(val, "path", json_object_new_string(hdat->path));
871 json_object_object_add(obj, hdat->key, val);
878 static const char *req_raw(struct afb_hreq *hreq, size_t *size)
880 const char *result = json_object_get_string(req_json(hreq));
881 *size = result ? strlen(result) : 0;
885 static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size)
887 afb_hreq_reply_copy(hreq, MHD_HTTP_OK, size, buffer, NULL);
890 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
892 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
893 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
896 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
898 struct json_object *reply;
900 struct MHD_Response *response;
902 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
904 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
906 reply = afb_msg_json_reply(status, info, resp, &hreq->context, reqid);
908 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);
909 afb_hreq_reply(hreq, retcode, response, NULL);
912 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info)
914 req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
917 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info)
919 req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
922 static int req_subscribe_unsubscribe_error(struct afb_hreq *hreq, struct afb_event event)
928 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)
930 afb_subcall(&hreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq });
933 int afb_hreq_init_context(struct afb_hreq *hreq)
938 if (hreq->context.session != NULL)
941 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
943 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
945 uuid = afb_hreq_get_cookie(hreq, cookie_name);
947 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
949 token = afb_hreq_get_header(hreq, long_key_for_token);
951 token = afb_hreq_get_argument(hreq, long_key_for_token);
953 token = afb_hreq_get_argument(hreq, short_key_for_token);
955 return afb_context_connect(&hreq->context, uuid, token);
958 int afb_hreq_init_cookie(int port, const char *path, int maxage)
965 cookie_setter = NULL;
968 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
971 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
972 cookie_name, path, maxage);