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-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_fail(struct afb_xreq *xreq, const char *status, const char *info);
77 static void req_success(struct afb_xreq *xreq, json_object *obj, const char *info);
78 static void req_destroy(struct afb_xreq *xreq);
80 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
83 .success = req_success,
88 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
90 struct hreq_data *data = hreq->data;
91 while (data != NULL) {
92 if (!strcasecmp(data->key, key))
97 data = calloc(1, sizeof *data);
99 data->key = strdup(key);
100 if (data->key == NULL) {
104 data->next = hreq->data;
112 /* a valid subpath is a relative path not looking deeper than root using .. */
113 static int validsubpath(const char *subpath)
118 switch (subpath[i++]) {
122 if (subpath[i] == '/') {
126 if (subpath[i++] == '.') {
132 if (subpath[i++] == '/') {
139 while (subpath[i] && subpath[i] != '/')
149 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
154 if (hreq->replied != 0)
157 k = va_arg(args, const char *);
159 v = va_arg(args, const char *);
160 MHD_add_response_header(response, k, v);
161 k = va_arg(args, const char *);
163 v = afb_context_sent_uuid(&hreq->xreq.context);
164 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
165 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
168 MHD_queue_response(hreq->connection, status, response);
169 MHD_destroy_response(response);
172 if (hreq->suspended != 0) {
173 extern void run_micro_httpd(struct afb_hsrv *hsrv);
174 MHD_resume_connection (hreq->connection);
176 run_micro_httpd(hreq->hsrv);
180 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
183 va_start(args, response);
184 afb_hreq_reply_v(hreq, status, response, args);
188 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
191 va_start(args, status);
192 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
196 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
199 va_start(args, buffer);
200 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
204 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
207 va_start(args, buffer);
208 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
212 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
215 va_start(args, buffer);
216 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
220 #if defined(USE_MAGIC_MIME_TYPE)
222 #if !defined(MAGIC_DB)
223 #define MAGIC_DB "/usr/share/misc/magic.mgc"
226 static magic_t lazy_libmagic()
229 static magic_t result = NULL;
233 /* MAGIC_MIME tells magic to return a mime of the file,
234 but you can specify different things */
235 INFO("Loading mimetype default magic database");
236 result = magic_open(MAGIC_MIME_TYPE);
237 if (result == NULL) {
238 ERROR("unable to initialize magic library");
240 /* Warning: should not use NULL for DB
241 [libmagic bug wont pass efence check] */
242 else if (magic_load(result, MAGIC_DB) != 0) {
243 ERROR("cannot load magic database: %s", magic_error(result));
252 static const char *magic_mimetype_fd(int fd)
254 magic_t lib = lazy_libmagic();
255 return lib ? magic_descriptor(lib, fd) : NULL;
260 static const char *mimetype_fd_name(int fd, const char *filename)
262 const char *result = NULL;
264 #if defined(INFER_EXTENSION)
266 * Set some well-known extensions
267 * Note that it is mandatory for example for css files in order to provide
268 * right mimetype that must be text/css (otherwise chrome browser will not
269 * load correctly css file) while libmagic returns text/plain.
271 const char *extension = strrchr(filename, '.');
273 static const char *const known[][2] = {
274 { ".js", "text/javascript" },
275 { ".html", "text/html" },
276 { ".css", "text/css" },
277 { ".ico", "image/x-icon"},
278 /* TODO: CHECK ME { ".ico", "image/vnd.microsoft.icon" }, */
279 { ".jpg", "image/jpeg" },
280 { ".png", "image/png" },
281 { ".svg", "image/svg+xml" },
282 { ".ttf", "application/x-font-ttf"},
283 { ".htm", "text/html" },
284 { ".xml", "application/xml" },
285 { ".txt", "text/plain" },
286 { ".wav", "audio/x-wav" },
287 { ".xhtml", "application/xhtml+xml" },
288 { ".xht", "application/xhtml+xml" },
289 { ".gif", "image/gif" },
290 { ".png", "image/png" },
291 { ".mp3", "audio/mpeg" },
295 while (known[i][0]) {
296 if (!strcasecmp(extension, known[i][0])) {
297 result = known[i][1];
304 #if defined(USE_MAGIC_MIME_TYPE)
306 result = magic_mimetype_fd(fd);
311 static void req_destroy(struct afb_xreq *xreq)
313 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
314 struct hreq_data *data;
316 if (hreq->postform != NULL)
317 MHD_destroy_post_processor(hreq->postform);
318 for (data = hreq->data; data; data = hreq->data) {
319 hreq->data = data->next;
328 afb_context_disconnect(&hreq->xreq.context);
329 json_object_put(hreq->json);
330 free((char*)hreq->xreq.request.api);
331 free((char*)hreq->xreq.request.verb);
332 afb_cred_unref(hreq->xreq.cred);
336 void afb_hreq_addref(struct afb_hreq *hreq)
338 afb_xreq_unhooked_addref(&hreq->xreq);
341 void afb_hreq_unref(struct afb_hreq *hreq)
344 hreq->xreq.replied = 1;
345 afb_xreq_unhooked_unref(&hreq->xreq);
349 * Removes the 'prefix' of 'length' from the tail of 'hreq'
350 * if and only if the prefix exists and is terminated by a leading
353 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
355 /* check the prefix ? */
356 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
357 || strncasecmp(prefix, hreq->tail, length))
360 /* removes successives / */
361 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
364 /* update the tail */
365 hreq->lentail -= length;
366 hreq->tail += length;
370 int afb_hreq_valid_tail(struct afb_hreq *hreq)
372 return validsubpath(hreq->tail);
375 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
377 afb_hreq_reply_empty(hreq, status, NULL);
380 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
384 if (hreq->url[hreq->lenurl - 1] == '/')
387 /* the redirect is needed for reliability of relative path */
388 tourl = alloca(hreq->lenurl + 2);
389 memcpy(tourl, hreq->url, hreq->lenurl);
390 tourl[hreq->lenurl] = '/';
391 tourl[hreq->lenurl + 1] = 0;
392 afb_hreq_redirect_to(hreq, tourl, 1);
396 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
402 char etag[1 + 2 * 8];
404 struct MHD_Response *response;
405 const char *mimetype;
407 /* Opens the file or directory */
409 fd = openat(dirfd, filename, O_RDONLY);
413 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
419 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
424 /* Retrieves file's status */
425 if (fstat(fd, &st) != 0) {
427 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
431 /* serve directory */
432 if (S_ISDIR(st.st_mode)) {
433 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
435 static const char *indexes[] = { "index.html", NULL };
437 while (indexes[i] != NULL) {
438 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
439 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
449 /* Don't serve special files */
450 if (!S_ISREG(st.st_mode)) {
452 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
456 /* Check the method */
457 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
459 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
463 /* computes the etag */
464 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
466 /* checks the etag */
467 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
468 if (inm && 0 == strcmp(inm, etag)) {
469 /* etag ok, return NOT MODIFIED */
471 DEBUG("Not Modified: [%s]", filename);
472 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
473 status = MHD_HTTP_NOT_MODIFIED;
476 if (st.st_size != (off_t) (size_t) st.st_size) {
478 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
482 /* create the response */
483 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
484 status = MHD_HTTP_OK;
487 mimetype = mimetype_fd_name(fd, filename);
488 if (mimetype != NULL)
489 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
492 /* fills the value and send */
493 afb_hreq_reply(hreq, status, response,
494 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
495 MHD_HTTP_HEADER_ETAG, etag,
500 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
502 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
504 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
508 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
514 char etag[1 + 2 * 8];
516 struct MHD_Response *response;
517 const char *mimetype;
519 /* Opens the file or directory */
520 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
524 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
528 /* Retrieves file's status */
529 if (fstat(fd, &st) != 0) {
531 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
535 /* serve directory */
536 if (S_ISDIR(st.st_mode)) {
537 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
539 static const char *indexes[] = { "index.html", NULL };
541 size_t length = strlen(filename);
542 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
543 memcpy(extname, filename, length);
544 if (length && extname[length - 1] != '/')
545 extname[length++] = '/';
546 while (rc == 0 && indexes[i] != NULL) {
547 strcpy(extname + length, indexes[i++]);
548 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
555 /* Don't serve special files */
556 if (!S_ISREG(st.st_mode)) {
558 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
562 /* Check the method */
563 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
565 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
569 /* computes the etag */
570 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
572 /* checks the etag */
573 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
574 if (inm && 0 == strcmp(inm, etag)) {
575 /* etag ok, return NOT MODIFIED */
577 DEBUG("Not Modified: [%s]", filename);
578 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
579 status = MHD_HTTP_NOT_MODIFIED;
582 if (st.st_size != (off_t) (size_t) st.st_size) {
584 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
588 /* create the response */
589 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
590 status = MHD_HTTP_OK;
593 mimetype = mimetype_fd_name(fd, filename);
594 if (mimetype != NULL)
595 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
598 /* fills the value and send */
599 afb_hreq_reply(hreq, status, response,
600 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
601 MHD_HTTP_HEADER_ETAG, etag,
606 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
608 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
610 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
621 static void _mkq_add_(struct _mkq_ *mkq, char value)
623 char *text = mkq->text;
625 if (mkq->length == mkq->alloc) {
627 text = realloc(text, mkq->alloc);
635 text[mkq->length++] = value;
639 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
641 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
644 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
647 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
648 _mkq_add_hex_(mkq, (char)(value & 15));
651 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
653 if (value <= ' ' || value >= 127)
654 _mkq_add_esc_(mkq, value);
660 _mkq_add_esc_(mkq, value);
663 _mkq_add_(mkq, value);
667 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
670 _mkq_add_char_(mkq, *value++);
673 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
675 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
676 _mkq_append_(mkq, key);
679 _mkq_append_(mkq, value);
684 static char *url_with_query(struct afb_hreq *hreq, const char *url)
689 mkq.length = strlen(url);
690 mkq.alloc = mkq.length + 1000;
691 mkq.text = malloc(mkq.alloc);
692 if (mkq.text != NULL) {
693 strcpy(mkq.text, url);
694 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
700 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
705 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
707 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
708 MHD_HTTP_HEADER_LOCATION, to, NULL);
709 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
713 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
715 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
718 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
720 struct hreq_data *data = get_data(hreq, name, 0);
721 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
724 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
726 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
729 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
732 struct hreq_data *hdat = get_data(hreq, key, 1);
733 if (hdat->path != NULL) {
736 p = realloc(hdat->value, hdat->length + size + 1);
741 memcpy(&hdat->value[hdat->length], data, size);
742 hdat->length += size;
743 hdat->value[hdat->length] = 0;
747 int afb_hreq_init_download_path(const char *directory)
753 if (access(directory, R_OK|W_OK)) {
754 /* no read/write access */
757 if (stat(directory, &st)) {
761 if (!S_ISDIR(st.st_mode)) {
762 /* not a directory */
766 n = strlen(directory);
767 while(n > 1 && directory[n-1] == '/') n--;
770 /* can't allocate memory */
774 memcpy(p, directory, n);
788 static int opentempfile(char **path)
793 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
797 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
805 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
809 struct hreq_data *hdat = get_data(hreq, key, 1);
811 if (hdat->value == NULL) {
812 hdat->value = strdup(file);
813 if (hdat->value == NULL)
815 fd = opentempfile(&hdat->path);
816 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
819 fd = open(hdat->path, O_WRONLY|O_APPEND);
824 sz = write(fd, data, size);
826 hdat->length += (size_t)sz;
829 } else if (errno != EINTR)
836 static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
839 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
840 struct hreq_data *hdat = get_data(hreq, name, 0);
842 return (struct afb_arg){
844 .value = hdat->value,
848 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
849 return (struct afb_arg){
850 .name = value == NULL ? NULL : name,
856 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
858 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
862 static struct json_object *req_json(struct afb_xreq *xreq)
864 struct hreq_data *hdat;
865 struct json_object *obj, *val;
866 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
870 hreq->json = obj = json_object_new_object();
873 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
874 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
875 if (hdat->path == NULL)
876 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
878 val = json_object_new_object();
881 json_object_object_add(val, "file", json_object_new_string(hdat->value));
882 json_object_object_add(val, "path", json_object_new_string(hdat->path));
885 json_object_object_add(obj, hdat->key, val);
892 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
894 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
895 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
898 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
900 struct json_object *reply;
902 struct MHD_Response *response;
904 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
906 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
908 reply = afb_msg_json_reply(status, info, resp, &hreq->xreq.context, reqid);
910 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);
911 afb_hreq_reply(hreq, retcode, response, NULL);
914 static void req_fail(struct afb_xreq *xreq, const char *status, const char *info)
916 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
917 req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
920 static void req_success(struct afb_xreq *xreq, json_object *obj, const char *info)
922 struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
923 req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
926 void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
928 hreq->xreq.request.api = strndup(api, lenapi);
929 hreq->xreq.request.verb = strndup(verb, lenverb);
930 if (hreq->xreq.request.api == NULL || hreq->xreq.request.verb == NULL) {
931 ERROR("Out of memory");
932 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
933 } else if (afb_hreq_init_context(hreq) < 0) {
934 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
936 afb_xreq_unhooked_addref(&hreq->xreq);
937 afb_xreq_process(&hreq->xreq, apiset);
941 int afb_hreq_init_context(struct afb_hreq *hreq)
946 if (hreq->xreq.context.session != NULL)
949 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
951 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
953 uuid = afb_hreq_get_cookie(hreq, cookie_name);
955 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
957 token = afb_hreq_get_header(hreq, long_key_for_token);
959 token = afb_hreq_get_argument(hreq, long_key_for_token);
961 token = afb_hreq_get_argument(hreq, short_key_for_token);
963 return afb_context_connect(&hreq->xreq.context, uuid, token);
966 int afb_hreq_init_cookie(int port, const char *path, int maxage)
973 cookie_setter = NULL;
976 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
979 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
980 cookie_name, path, maxage);
986 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
991 struct afb_hreq *afb_hreq_create()
993 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
995 /* init the request */
996 afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
997 hreq->reqid = ++global_reqids;