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)
278 * Set some well-known extensions
279 * Note that it is mandatory for example for css files in order to provide
280 * right mimetype that must be text/css (otherwise chrome browser will not
281 * load correctly css file) while libmagic returns text/plain.
283 const char *extension = strrchr(filename, '.');
285 static const char *const known[][2] = {
286 { ".js", "text/javascript" },
287 { ".html", "text/html" },
288 { ".css", "text/css" },
289 { ".ico", "image/x-icon"},
290 { ".png", "image/png" },
291 { ".svg", "image/svg+xml" },
292 { ".ttf", "application/x-font-ttf"},
296 while (known[i][0]) {
297 if (!strcasecmp(extension, known[i][0])) {
298 result = known[i][1];
305 #if defined(USE_MAGIC_MIME_TYPE)
307 result = magic_mimetype_fd(fd);
312 void afb_hreq_addref(struct afb_hreq *hreq)
317 void afb_hreq_unref(struct afb_hreq *hreq)
319 struct hreq_data *data;
321 if (hreq == NULL || --hreq->refcount)
324 if (hreq->postform != NULL)
325 MHD_destroy_post_processor(hreq->postform);
326 for (data = hreq->data; data; data = hreq->data) {
327 hreq->data = data->next;
336 afb_context_disconnect(&hreq->context);
337 json_object_put(hreq->json);
344 * Removes the 'prefix' of 'length' from the tail of 'hreq'
345 * if and only if the prefix exists and is terminated by a leading
348 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
350 /* check the prefix ? */
351 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
352 || strncasecmp(prefix, hreq->tail, length))
355 /* removes successives / */
356 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
359 /* update the tail */
360 hreq->lentail -= length;
361 hreq->tail += length;
365 int afb_hreq_valid_tail(struct afb_hreq *hreq)
367 return validsubpath(hreq->tail);
370 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
372 afb_hreq_reply_empty(hreq, status, NULL);
375 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
379 if (hreq->url[hreq->lenurl - 1] == '/')
382 /* the redirect is needed for reliability of relative path */
383 tourl = alloca(hreq->lenurl + 2);
384 memcpy(tourl, hreq->url, hreq->lenurl);
385 tourl[hreq->lenurl] = '/';
386 tourl[hreq->lenurl + 1] = 0;
387 afb_hreq_redirect_to(hreq, tourl, 1);
391 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
397 char etag[1 + 2 * 8];
399 struct MHD_Response *response;
400 const char *mimetype;
402 /* Opens the file or directory */
404 fd = openat(dirfd, filename, O_RDONLY);
408 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
414 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
419 /* Retrieves file's status */
420 if (fstat(fd, &st) != 0) {
422 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
426 /* serve directory */
427 if (S_ISDIR(st.st_mode)) {
428 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
430 static const char *indexes[] = { "index.html", NULL };
432 while (indexes[i] != NULL) {
433 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
434 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
444 /* Don't serve special files */
445 if (!S_ISREG(st.st_mode)) {
447 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
451 /* Check the method */
452 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
454 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
458 /* computes the etag */
459 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
461 /* checks the etag */
462 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
463 if (inm && 0 == strcmp(inm, etag)) {
464 /* etag ok, return NOT MODIFIED */
466 DEBUG("Not Modified: [%s]", filename);
467 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
468 status = MHD_HTTP_NOT_MODIFIED;
471 if (st.st_size != (off_t) (size_t) st.st_size) {
473 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
477 /* create the response */
478 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
479 status = MHD_HTTP_OK;
482 mimetype = mimetype_fd_name(fd, filename);
483 if (mimetype != NULL)
484 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
487 /* fills the value and send */
488 afb_hreq_reply(hreq, status, response,
489 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
490 MHD_HTTP_HEADER_ETAG, etag,
495 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
497 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
499 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
503 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
509 char etag[1 + 2 * 8];
511 struct MHD_Response *response;
512 const char *mimetype;
514 /* Opens the file or directory */
515 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
519 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
523 /* Retrieves file's status */
524 if (fstat(fd, &st) != 0) {
526 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
530 /* serve directory */
531 if (S_ISDIR(st.st_mode)) {
532 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
534 static const char *indexes[] = { "index.html", NULL };
536 size_t length = strlen(filename);
537 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
538 memcpy(extname, filename, length);
539 if (length && extname[length - 1] != '/')
540 extname[length++] = '/';
541 while (rc == 0 && indexes[i] != NULL) {
542 strcpy(extname + length, indexes[i++]);
543 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
550 /* Don't serve special files */
551 if (!S_ISREG(st.st_mode)) {
553 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
557 /* Check the method */
558 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
560 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
564 /* computes the etag */
565 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
567 /* checks the etag */
568 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
569 if (inm && 0 == strcmp(inm, etag)) {
570 /* etag ok, return NOT MODIFIED */
572 DEBUG("Not Modified: [%s]", filename);
573 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
574 status = MHD_HTTP_NOT_MODIFIED;
577 if (st.st_size != (off_t) (size_t) st.st_size) {
579 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
583 /* create the response */
584 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
585 status = MHD_HTTP_OK;
588 mimetype = mimetype_fd_name(fd, filename);
589 if (mimetype != NULL)
590 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
593 /* fills the value and send */
594 afb_hreq_reply(hreq, status, response,
595 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
596 MHD_HTTP_HEADER_ETAG, etag,
601 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
603 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
605 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
616 static void _mkq_add_(struct _mkq_ *mkq, char value)
618 char *text = mkq->text;
620 if (mkq->length == mkq->alloc) {
622 text = realloc(text, mkq->alloc);
630 text[mkq->length++] = value;
634 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
636 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
639 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
642 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
643 _mkq_add_hex_(mkq, (char)(value & 15));
646 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
648 if (value <= ' ' || value >= 127)
649 _mkq_add_esc_(mkq, value);
655 _mkq_add_esc_(mkq, value);
658 _mkq_add_(mkq, value);
662 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
665 _mkq_add_char_(mkq, *value++);
668 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
670 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
671 _mkq_append_(mkq, key);
674 _mkq_append_(mkq, value);
679 static char *url_with_query(struct afb_hreq *hreq, const char *url)
684 mkq.length = strlen(url);
685 mkq.alloc = mkq.length + 1000;
686 mkq.text = malloc(mkq.alloc);
687 if (mkq.text != NULL) {
688 strcpy(mkq.text, url);
689 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
695 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
700 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
702 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
703 MHD_HTTP_HEADER_LOCATION, to, NULL);
704 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
708 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
710 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
713 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
715 struct hreq_data *data = get_data(hreq, name, 0);
716 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
719 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
721 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
724 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
727 struct hreq_data *hdat = get_data(hreq, key, 1);
728 if (hdat->path != NULL) {
731 p = realloc(hdat->value, hdat->length + size + 1);
736 memcpy(&hdat->value[hdat->length], data, size);
737 hdat->length += size;
738 hdat->value[hdat->length] = 0;
742 int afb_hreq_init_download_path(const char *directory)
748 if (access(directory, R_OK|W_OK)) {
749 /* no read/write access */
752 if (stat(directory, &st)) {
756 if (!S_ISDIR(st.st_mode)) {
757 /* not a directory */
761 n = strlen(directory);
762 while(n > 1 && directory[n-1] == '/') n--;
765 /* can't allocate memory */
769 memcpy(p, directory, n);
783 static int opentempfile(char **path)
788 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
792 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
800 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
804 struct hreq_data *hdat = get_data(hreq, key, 1);
806 if (hdat->value == NULL) {
807 hdat->value = strdup(file);
808 if (hdat->value == NULL)
810 fd = opentempfile(&hdat->path);
811 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
814 fd = open(hdat->path, O_WRONLY|O_APPEND);
819 sz = write(fd, data, size);
821 hdat->length += (size_t)sz;
824 } else if (errno != EINTR)
831 struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
833 return (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq };
836 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
839 struct hreq_data *hdat = get_data(hreq, name, 0);
841 return (struct afb_arg){
843 .value = hdat->value,
847 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
848 return (struct afb_arg){
849 .name = value == NULL ? NULL : name,
855 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
857 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
861 static struct json_object *req_json(struct afb_hreq *hreq)
863 struct hreq_data *hdat;
864 struct json_object *obj, *val;
868 hreq->json = obj = json_object_new_object();
871 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
872 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
873 if (hdat->path == NULL)
874 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
876 val = json_object_new_object();
879 json_object_object_add(val, "file", json_object_new_string(hdat->value));
880 json_object_object_add(val, "path", json_object_new_string(hdat->path));
883 json_object_object_add(obj, hdat->key, val);
890 static const char *req_raw(struct afb_hreq *hreq, size_t *size)
892 const char *result = json_object_get_string(req_json(hreq));
893 *size = result ? strlen(result) : 0;
897 static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size)
899 afb_hreq_reply_copy(hreq, MHD_HTTP_OK, size, buffer, NULL);
902 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
904 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
905 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
908 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
910 struct json_object *reply;
912 struct MHD_Response *response;
914 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
916 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
918 reply = afb_msg_json_reply(status, info, resp, &hreq->context, reqid);
920 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);
921 afb_hreq_reply(hreq, retcode, response, NULL);
924 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info)
926 req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
929 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info)
931 req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
934 static int req_subscribe_unsubscribe_error(struct afb_hreq *hreq, struct afb_event event)
940 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)
942 afb_subcall(&hreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq });
945 int afb_hreq_init_req_call(struct afb_hreq *hreq, const char *api, size_t lenapi, const char *verb, size_t lenverb)
949 hreq->api = strndup(api, lenapi);
950 hreq->verb = strndup(verb, lenverb);
951 if (hreq->api == NULL || hreq->verb == NULL) {
952 ERROR("Out of memory");
956 return afb_hreq_init_context(hreq);
959 int afb_hreq_init_context(struct afb_hreq *hreq)
964 if (hreq->context.session != NULL)
967 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
969 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
971 uuid = afb_hreq_get_cookie(hreq, cookie_name);
973 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
975 token = afb_hreq_get_header(hreq, long_key_for_token);
977 token = afb_hreq_get_argument(hreq, long_key_for_token);
979 token = afb_hreq_get_argument(hreq, short_key_for_token);
981 return afb_context_connect(&hreq->context, uuid, token);
984 int afb_hreq_init_cookie(int port, const char *path, int maxage)
991 cookie_setter = NULL;
994 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
997 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
998 cookie_name, path, maxage);