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-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_fail(struct afb_xreq *xreq, const char *status, const char *info);
78 static void req_success(struct afb_xreq *xreq, json_object *obj, const char *info);
79 static void req_destroy(struct afb_xreq *xreq);
81 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
84 .success = req_success,
89 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
91 struct hreq_data *data = hreq->data;
92 while (data != NULL) {
93 if (!strcasecmp(data->key, key))
98 data = calloc(1, sizeof *data);
100 data->key = strdup(key);
101 if (data->key == NULL) {
105 data->next = hreq->data;
113 /* a valid subpath is a relative path not looking deeper than root using .. */
114 static int validsubpath(const char *subpath)
119 switch (subpath[i++]) {
123 if (subpath[i] == '/') {
127 if (subpath[i++] == '.') {
133 if (subpath[i++] == '/') {
140 while (subpath[i] && subpath[i] != '/')
150 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
155 if (hreq->replied != 0)
158 k = va_arg(args, const char *);
160 v = va_arg(args, const char *);
161 MHD_add_response_header(response, k, v);
162 k = va_arg(args, const char *);
164 v = afb_context_sent_uuid(&hreq->xreq.context);
165 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
166 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
169 MHD_queue_response(hreq->connection, status, response);
170 MHD_destroy_response(response);
173 if (hreq->suspended != 0) {
174 extern void run_micro_httpd(struct afb_hsrv *hsrv);
175 MHD_resume_connection (hreq->connection);
177 run_micro_httpd(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 { ".js", "text/javascript" },
276 { ".html", "text/html" },
277 { ".css", "text/css" },
278 { ".ico", "image/x-icon"},
279 { ".png", "image/png" },
280 { ".svg", "image/svg+xml" },
281 { ".ttf", "application/x-font-ttf"},
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 static void req_destroy(struct afb_xreq *xreq)
303 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
304 struct hreq_data *data;
306 if (hreq->postform != NULL)
307 MHD_destroy_post_processor(hreq->postform);
308 for (data = hreq->data; data; data = hreq->data) {
309 hreq->data = data->next;
318 afb_context_disconnect(&hreq->xreq.context);
319 json_object_put(hreq->json);
320 free((char*)hreq->xreq.api);
321 free((char*)hreq->xreq.verb);
322 afb_cred_unref(hreq->xreq.cred);
326 void afb_hreq_addref(struct afb_hreq *hreq)
328 afb_xreq_addref(&hreq->xreq);
331 void afb_hreq_unref(struct afb_hreq *hreq)
333 afb_xreq_unref(&hreq->xreq);
337 * Removes the 'prefix' of 'length' from the tail of 'hreq'
338 * if and only if the prefix exists and is terminated by a leading
341 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
343 /* check the prefix ? */
344 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
345 || strncasecmp(prefix, hreq->tail, length))
348 /* removes successives / */
349 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
352 /* update the tail */
353 hreq->lentail -= length;
354 hreq->tail += length;
358 int afb_hreq_valid_tail(struct afb_hreq *hreq)
360 return validsubpath(hreq->tail);
363 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
365 afb_hreq_reply_empty(hreq, status, NULL);
368 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
372 if (hreq->url[hreq->lenurl - 1] == '/')
375 /* the redirect is needed for reliability of relative path */
376 tourl = alloca(hreq->lenurl + 2);
377 memcpy(tourl, hreq->url, hreq->lenurl);
378 tourl[hreq->lenurl] = '/';
379 tourl[hreq->lenurl + 1] = 0;
380 afb_hreq_redirect_to(hreq, tourl, 1);
384 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
390 char etag[1 + 2 * 8];
392 struct MHD_Response *response;
393 const char *mimetype;
395 /* Opens the file or directory */
397 fd = openat(dirfd, filename, O_RDONLY);
401 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
407 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
412 /* Retrieves file's status */
413 if (fstat(fd, &st) != 0) {
415 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
419 /* serve directory */
420 if (S_ISDIR(st.st_mode)) {
421 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
423 static const char *indexes[] = { "index.html", NULL };
425 while (indexes[i] != NULL) {
426 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
427 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
437 /* Don't serve special files */
438 if (!S_ISREG(st.st_mode)) {
440 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
444 /* Check the method */
445 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
447 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
451 /* computes the etag */
452 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
454 /* checks the etag */
455 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
456 if (inm && 0 == strcmp(inm, etag)) {
457 /* etag ok, return NOT MODIFIED */
459 DEBUG("Not Modified: [%s]", filename);
460 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
461 status = MHD_HTTP_NOT_MODIFIED;
464 if (st.st_size != (off_t) (size_t) st.st_size) {
466 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
470 /* create the response */
471 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
472 status = MHD_HTTP_OK;
475 mimetype = mimetype_fd_name(fd, filename);
476 if (mimetype != NULL)
477 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
480 /* fills the value and send */
481 afb_hreq_reply(hreq, status, response,
482 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
483 MHD_HTTP_HEADER_ETAG, etag,
488 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
490 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
492 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
496 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
502 char etag[1 + 2 * 8];
504 struct MHD_Response *response;
505 const char *mimetype;
507 /* Opens the file or directory */
508 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
512 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
516 /* Retrieves file's status */
517 if (fstat(fd, &st) != 0) {
519 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
523 /* serve directory */
524 if (S_ISDIR(st.st_mode)) {
525 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
527 static const char *indexes[] = { "index.html", NULL };
529 size_t length = strlen(filename);
530 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
531 memcpy(extname, filename, length);
532 if (length && extname[length - 1] != '/')
533 extname[length++] = '/';
534 while (rc == 0 && indexes[i] != NULL) {
535 strcpy(extname + length, indexes[i++]);
536 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
543 /* Don't serve special files */
544 if (!S_ISREG(st.st_mode)) {
546 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
550 /* Check the method */
551 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
553 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
557 /* computes the etag */
558 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
560 /* checks the etag */
561 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
562 if (inm && 0 == strcmp(inm, etag)) {
563 /* etag ok, return NOT MODIFIED */
565 DEBUG("Not Modified: [%s]", filename);
566 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
567 status = MHD_HTTP_NOT_MODIFIED;
570 if (st.st_size != (off_t) (size_t) st.st_size) {
572 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
576 /* create the response */
577 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
578 status = MHD_HTTP_OK;
581 mimetype = mimetype_fd_name(fd, filename);
582 if (mimetype != NULL)
583 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
586 /* fills the value and send */
587 afb_hreq_reply(hreq, status, response,
588 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
589 MHD_HTTP_HEADER_ETAG, etag,
594 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
596 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
598 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
609 static void _mkq_add_(struct _mkq_ *mkq, char value)
611 char *text = mkq->text;
613 if (mkq->length == mkq->alloc) {
615 text = realloc(text, mkq->alloc);
623 text[mkq->length++] = value;
627 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
629 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
632 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
635 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
636 _mkq_add_hex_(mkq, (char)(value & 15));
639 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
641 if (value <= ' ' || value >= 127)
642 _mkq_add_esc_(mkq, value);
648 _mkq_add_esc_(mkq, value);
651 _mkq_add_(mkq, value);
655 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
658 _mkq_add_char_(mkq, *value++);
661 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
663 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
664 _mkq_append_(mkq, key);
667 _mkq_append_(mkq, value);
672 static char *url_with_query(struct afb_hreq *hreq, const char *url)
677 mkq.length = strlen(url);
678 mkq.alloc = mkq.length + 1000;
679 mkq.text = malloc(mkq.alloc);
680 if (mkq.text != NULL) {
681 strcpy(mkq.text, url);
682 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
688 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
693 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
695 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
696 MHD_HTTP_HEADER_LOCATION, to, NULL);
697 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
701 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
703 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
706 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
708 struct hreq_data *data = get_data(hreq, name, 0);
709 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
712 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
714 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
717 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
720 struct hreq_data *hdat = get_data(hreq, key, 1);
721 if (hdat->path != NULL) {
724 p = realloc(hdat->value, hdat->length + size + 1);
729 memcpy(&hdat->value[hdat->length], data, size);
730 hdat->length += size;
731 hdat->value[hdat->length] = 0;
735 int afb_hreq_init_download_path(const char *directory)
741 if (access(directory, R_OK|W_OK)) {
742 /* no read/write access */
745 if (stat(directory, &st)) {
749 if (!S_ISDIR(st.st_mode)) {
750 /* not a directory */
754 n = strlen(directory);
755 while(n > 1 && directory[n-1] == '/') n--;
758 /* can't allocate memory */
762 memcpy(p, directory, n);
776 static int opentempfile(char **path)
781 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
785 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
793 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
797 struct hreq_data *hdat = get_data(hreq, key, 1);
799 if (hdat->value == NULL) {
800 hdat->value = strdup(file);
801 if (hdat->value == NULL)
803 fd = opentempfile(&hdat->path);
804 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
807 fd = open(hdat->path, O_WRONLY|O_APPEND);
812 sz = write(fd, data, size);
814 hdat->length += (size_t)sz;
817 } else if (errno != EINTR)
824 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
827 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
828 struct hreq_data *hdat = get_data(hreq, name, 0);
830 return (struct afb_arg){
832 .value = hdat->value,
836 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
837 return (struct afb_arg){
838 .name = value == NULL ? NULL : name,
844 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
846 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
850 static struct json_object *req_json(struct afb_xreq *xreq)
852 struct hreq_data *hdat;
853 struct json_object *obj, *val;
854 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
858 hreq->json = obj = json_object_new_object();
861 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
862 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
863 if (hdat->path == NULL)
864 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
866 val = json_object_new_object();
869 json_object_object_add(val, "file", json_object_new_string(hdat->value));
870 json_object_object_add(val, "path", json_object_new_string(hdat->path));
873 json_object_object_add(obj, hdat->key, val);
880 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
882 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
883 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
886 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
888 struct json_object *reply;
890 struct MHD_Response *response;
892 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
894 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
896 reply = afb_msg_json_reply(status, info, resp, &hreq->xreq.context, reqid);
898 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);
899 afb_hreq_reply(hreq, retcode, response, NULL);
902 static void req_fail(struct afb_xreq *xreq, const char *status, const char *info)
904 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
905 req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
908 static void req_success(struct afb_xreq *xreq, json_object *obj, const char *info)
910 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
911 req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
914 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
916 hreq->xreq.api = strndup(api, lenapi);
917 hreq->xreq.verb = strndup(verb, lenverb);
918 if (hreq->xreq.api == NULL || hreq->xreq.verb == NULL) {
919 ERROR("Out of memory");
920 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
921 } else if (afb_hreq_init_context(hreq) < 0) {
922 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
924 afb_xreq_addref(&hreq->xreq); /* TODO check if needed */
925 afb_xreq_process(&hreq->xreq, apiset);
929 int afb_hreq_init_context(struct afb_hreq *hreq)
934 if (hreq->xreq.context.session != NULL)
937 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
939 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
941 uuid = afb_hreq_get_cookie(hreq, cookie_name);
943 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
945 token = afb_hreq_get_header(hreq, long_key_for_token);
947 token = afb_hreq_get_argument(hreq, long_key_for_token);
949 token = afb_hreq_get_argument(hreq, short_key_for_token);
951 return afb_context_connect(&hreq->xreq.context, uuid, token);
954 int afb_hreq_init_cookie(int port, const char *path, int maxage)
961 cookie_setter = NULL;
964 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
967 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
968 cookie_name, path, maxage);
974 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
979 struct afb_hreq *afb_hreq_create()
981 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
983 /* init the request */
984 afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
985 hreq->xreq.cred = afb_cred_current();
986 hreq->reqid = ++global_reqids;