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"
39 #include "afb-session.h"
42 #include "locale-root.h"
44 #define SIZE_RESPONSE_BUFFER 8192
46 static int global_reqids = 0;
48 static char empty_string[] = "";
50 static const char long_key_for_uuid[] = "x-afb-uuid";
51 static const char short_key_for_uuid[] = "uuid";
53 static const char long_key_for_token[] = "x-afb-token";
54 static const char short_key_for_token[] = "token";
56 static const char long_key_for_reqid[] = "x-afb-reqid";
57 static const char short_key_for_reqid[] = "reqid";
59 static char *cookie_name = NULL;
60 static char *cookie_setter = NULL;
61 static char *tmp_pattern = NULL;
64 * Structure for storing key/values read from POST requests
67 struct hreq_data *next; /* chain to next data */
68 char *key; /* key name */
69 size_t length; /* length of the value (used for appending) */
70 char *value; /* the value (or original filename) */
71 char *path; /* path of the file saved */
74 static struct json_object *req_json(struct afb_xreq *xreq);
75 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name);
76 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info);
77 static void req_destroy(struct afb_xreq *xreq);
79 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
86 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
88 struct hreq_data *data = hreq->data;
89 while (data != NULL) {
90 if (!strcasecmp(data->key, key))
95 data = calloc(1, sizeof *data);
97 data->key = strdup(key);
98 if (data->key == NULL) {
102 data->next = hreq->data;
110 /* a valid subpath is a relative path not looking deeper than root using .. */
111 static int validsubpath(const char *subpath)
116 switch (subpath[i++]) {
120 if (subpath[i] == '/') {
124 if (subpath[i++] == '.') {
130 if (subpath[i++] == '/') {
137 while (subpath[i] && subpath[i] != '/')
147 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
152 if (hreq->replied != 0)
155 k = va_arg(args, const char *);
157 v = va_arg(args, const char *);
158 MHD_add_response_header(response, k, v);
159 k = va_arg(args, const char *);
161 v = afb_context_sent_uuid(&hreq->xreq.context);
162 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
163 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
166 MHD_queue_response(hreq->connection, status, response);
167 MHD_destroy_response(response);
170 if (hreq->suspended != 0) {
171 extern void run_micro_httpd(struct afb_hsrv *hsrv);
172 MHD_resume_connection (hreq->connection);
174 run_micro_httpd(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 for (data = hreq->data; data; data = hreq->data) {
322 hreq->data = data->next;
331 afb_context_disconnect(&hreq->xreq.context);
332 json_object_put(hreq->json);
333 free((char*)hreq->xreq.request.called_api);
334 free((char*)hreq->xreq.request.called_verb);
335 afb_cred_unref(hreq->xreq.cred);
339 void afb_hreq_addref(struct afb_hreq *hreq)
341 afb_xreq_unhooked_addref(&hreq->xreq);
344 void afb_hreq_unref(struct afb_hreq *hreq)
347 hreq->xreq.replied = 1;
348 afb_xreq_unhooked_unref(&hreq->xreq);
352 * Removes the 'prefix' of 'length' from the tail of 'hreq'
353 * if and only if the prefix exists and is terminated by a leading
356 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
358 /* check the prefix ? */
359 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
360 || strncasecmp(prefix, hreq->tail, length))
363 /* removes successives / */
364 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
367 /* update the tail */
368 hreq->lentail -= length;
369 hreq->tail += length;
373 int afb_hreq_valid_tail(struct afb_hreq *hreq)
375 return validsubpath(hreq->tail);
378 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
380 afb_hreq_reply_empty(hreq, status, NULL);
383 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
387 if (hreq->url[hreq->lenurl - 1] == '/')
390 /* the redirect is needed for reliability of relative path */
391 tourl = alloca(hreq->lenurl + 2);
392 memcpy(tourl, hreq->url, hreq->lenurl);
393 tourl[hreq->lenurl] = '/';
394 tourl[hreq->lenurl + 1] = 0;
395 afb_hreq_redirect_to(hreq, tourl, 1);
399 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
405 char etag[1 + 2 * 8];
407 struct MHD_Response *response;
408 const char *mimetype;
410 /* Opens the file or directory */
412 fd = openat(dirfd, filename, O_RDONLY);
416 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
422 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
427 /* Retrieves file's status */
428 if (fstat(fd, &st) != 0) {
430 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
434 /* serve directory */
435 if (S_ISDIR(st.st_mode)) {
436 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
438 static const char *indexes[] = { "index.html", NULL };
440 while (indexes[i] != NULL) {
441 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
442 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
452 /* Don't serve special files */
453 if (!S_ISREG(st.st_mode)) {
455 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
459 /* Check the method */
460 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
462 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
466 /* computes the etag */
467 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
469 /* checks the etag */
470 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
471 if (inm && 0 == strcmp(inm, etag)) {
472 /* etag ok, return NOT MODIFIED */
474 DEBUG("Not Modified: [%s]", filename);
475 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
476 status = MHD_HTTP_NOT_MODIFIED;
479 if (st.st_size != (off_t) (size_t) st.st_size) {
481 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
485 /* create the response */
486 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
487 status = MHD_HTTP_OK;
490 mimetype = mimetype_fd_name(fd, filename);
491 if (mimetype != NULL)
492 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
495 /* fills the value and send */
496 afb_hreq_reply(hreq, status, response,
497 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
498 MHD_HTTP_HEADER_ETAG, etag,
503 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
505 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
507 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
511 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
517 char etag[1 + 2 * 8];
519 struct MHD_Response *response;
520 const char *mimetype;
522 /* Opens the file or directory */
523 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
527 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
531 /* Retrieves file's status */
532 if (fstat(fd, &st) != 0) {
534 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
538 /* serve directory */
539 if (S_ISDIR(st.st_mode)) {
540 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
542 static const char *indexes[] = { "index.html", NULL };
544 size_t length = strlen(filename);
545 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
546 memcpy(extname, filename, length);
547 if (length && extname[length - 1] != '/')
548 extname[length++] = '/';
549 while (rc == 0 && indexes[i] != NULL) {
550 strcpy(extname + length, indexes[i++]);
551 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
558 /* Don't serve special files */
559 if (!S_ISREG(st.st_mode)) {
561 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
565 /* Check the method */
566 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
568 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
572 /* computes the etag */
573 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
575 /* checks the etag */
576 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
577 if (inm && 0 == strcmp(inm, etag)) {
578 /* etag ok, return NOT MODIFIED */
580 DEBUG("Not Modified: [%s]", filename);
581 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
582 status = MHD_HTTP_NOT_MODIFIED;
585 if (st.st_size != (off_t) (size_t) st.st_size) {
587 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
591 /* create the response */
592 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
593 status = MHD_HTTP_OK;
596 mimetype = mimetype_fd_name(fd, filename);
597 if (mimetype != NULL)
598 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
601 /* fills the value and send */
602 afb_hreq_reply(hreq, status, response,
603 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
604 MHD_HTTP_HEADER_ETAG, etag,
609 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
611 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
613 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
624 static void _mkq_add_(struct _mkq_ *mkq, char value)
626 char *text = mkq->text;
628 if (mkq->length == mkq->alloc) {
630 text = realloc(text, mkq->alloc);
638 text[mkq->length++] = value;
642 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
644 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
647 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
650 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
651 _mkq_add_hex_(mkq, (char)(value & 15));
654 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
656 if (value <= ' ' || value >= 127)
657 _mkq_add_esc_(mkq, value);
663 _mkq_add_esc_(mkq, value);
666 _mkq_add_(mkq, value);
670 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
673 _mkq_add_char_(mkq, *value++);
676 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
678 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
679 _mkq_append_(mkq, key);
682 _mkq_append_(mkq, value);
687 static char *url_with_query(struct afb_hreq *hreq, const char *url)
692 mkq.length = strlen(url);
693 mkq.alloc = mkq.length + 1000;
694 mkq.text = malloc(mkq.alloc);
695 if (mkq.text != NULL) {
696 strcpy(mkq.text, url);
697 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
703 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
708 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
710 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
711 MHD_HTTP_HEADER_LOCATION, to, NULL);
712 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
716 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
718 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
721 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
723 struct hreq_data *data = get_data(hreq, name, 0);
724 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
727 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
729 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
732 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
735 struct hreq_data *hdat = get_data(hreq, key, 1);
736 if (hdat->path != NULL) {
739 p = realloc(hdat->value, hdat->length + size + 1);
744 memcpy(&hdat->value[hdat->length], data, size);
745 hdat->length += size;
746 hdat->value[hdat->length] = 0;
750 int afb_hreq_init_download_path(const char *directory)
756 if (access(directory, R_OK|W_OK)) {
757 /* no read/write access */
760 if (stat(directory, &st)) {
764 if (!S_ISDIR(st.st_mode)) {
765 /* not a directory */
769 n = strlen(directory);
770 while(n > 1 && directory[n-1] == '/') n--;
773 /* can't allocate memory */
777 memcpy(p, directory, n);
791 static int opentempfile(char **path)
796 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
800 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
808 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
812 struct hreq_data *hdat = get_data(hreq, key, 1);
814 if (hdat->value == NULL) {
815 hdat->value = strdup(file);
816 if (hdat->value == NULL)
818 fd = opentempfile(&hdat->path);
819 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
822 fd = open(hdat->path, O_WRONLY|O_APPEND);
827 sz = write(fd, data, size);
829 hdat->length += (size_t)sz;
832 } else if (errno != EINTR)
839 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
842 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
843 struct hreq_data *hdat = get_data(hreq, name, 0);
845 return (struct afb_arg){
847 .value = hdat->value,
851 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
852 return (struct afb_arg){
853 .name = value == NULL ? NULL : name,
859 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
861 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
865 static struct json_object *req_json(struct afb_xreq *xreq)
867 struct hreq_data *hdat;
868 struct json_object *obj, *val;
869 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
873 hreq->json = obj = json_object_new_object();
876 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
877 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
878 if (hdat->path == NULL)
879 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
881 val = json_object_new_object();
884 json_object_object_add(val, "file", json_object_new_string(hdat->value));
885 json_object_object_add(val, "path", json_object_new_string(hdat->path));
888 json_object_object_add(obj, hdat->key, val);
895 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
897 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
898 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
901 static void req_reply(struct afb_xreq *xreq, struct json_object *object, const char *error, const char *info)
903 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
904 struct json_object *sub, *reply;
906 struct MHD_Response *response;
908 /* create the reply */
909 reply = afb_msg_json_reply(object, error, info, &xreq->context);
911 /* append the req id on need */
912 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
914 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
915 if (reqid != NULL && json_object_object_get_ex(reply, "request", &sub))
916 json_object_object_add(sub, "reqid", json_object_new_string(reqid));
918 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);
919 afb_hreq_reply(hreq, MHD_HTTP_OK, response, NULL);
922 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
924 hreq->xreq.request.called_api = strndup(api, lenapi);
925 hreq->xreq.request.called_verb = strndup(verb, lenverb);
926 if (hreq->xreq.request.called_api == NULL || hreq->xreq.request.called_verb == NULL) {
927 ERROR("Out of memory");
928 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
929 } else if (afb_hreq_init_context(hreq) < 0) {
930 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
932 afb_xreq_unhooked_addref(&hreq->xreq);
933 afb_xreq_process(&hreq->xreq, apiset);
937 int afb_hreq_init_context(struct afb_hreq *hreq)
942 if (hreq->xreq.context.session != NULL)
945 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
947 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
949 uuid = afb_hreq_get_cookie(hreq, cookie_name);
951 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
953 token = afb_hreq_get_header(hreq, long_key_for_token);
955 token = afb_hreq_get_argument(hreq, long_key_for_token);
957 token = afb_hreq_get_argument(hreq, short_key_for_token);
959 return afb_context_connect(&hreq->xreq.context, uuid, token);
962 int afb_hreq_init_cookie(int port, const char *path, int maxage)
969 cookie_setter = NULL;
972 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
975 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
976 cookie_name, path, maxage);
982 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
987 struct afb_hreq *afb_hreq_create()
989 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
991 /* init the request */
992 afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
993 hreq->reqid = ++global_reqids;