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 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_hreq *hreq);
76 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name);
77 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info);
78 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info);
80 static void afb_hreq_destroy(struct afb_hreq *hreq);
82 const struct afb_xreq_query_itf afb_hreq_xreq_query_itf = {
83 .json = (void*)req_json,
84 .get = (void*)req_get,
85 .success = (void*)req_success,
86 .fail = (void*)req_fail,
87 .unref = (void*)afb_hreq_destroy
90 static struct hreq_data *get_data(struct afb_hreq *hreq, const char *key, int create)
92 struct hreq_data *data = hreq->data;
93 while (data != NULL) {
94 if (!strcasecmp(data->key, key))
99 data = calloc(1, sizeof *data);
101 data->key = strdup(key);
102 if (data->key == NULL) {
106 data->next = hreq->data;
114 /* a valid subpath is a relative path not looking deeper than root using .. */
115 static int validsubpath(const char *subpath)
120 switch (subpath[i++]) {
124 if (subpath[i] == '/') {
128 if (subpath[i++] == '.') {
134 if (subpath[i++] == '/') {
141 while (subpath[i] && subpath[i] != '/')
151 static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
156 if (hreq->replied != 0)
159 k = va_arg(args, const char *);
161 v = va_arg(args, const char *);
162 MHD_add_response_header(response, k, v);
163 k = va_arg(args, const char *);
165 v = afb_context_sent_uuid(&hreq->xreq.context);
166 if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
167 MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
170 MHD_queue_response(hreq->connection, status, response);
171 MHD_destroy_response(response);
174 if (hreq->suspended != 0) {
175 extern void run_micro_httpd(struct afb_hsrv *hsrv);
176 MHD_resume_connection (hreq->connection);
178 run_micro_httpd(hreq->hsrv);
182 void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
185 va_start(args, response);
186 afb_hreq_reply_v(hreq, status, response, args);
190 void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
193 va_start(args, status);
194 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
198 void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
201 va_start(args, buffer);
202 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
206 void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
209 va_start(args, buffer);
210 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
214 void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
217 va_start(args, buffer);
218 afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
222 #if defined(USE_MAGIC_MIME_TYPE)
224 #if !defined(MAGIC_DB)
225 #define MAGIC_DB "/usr/share/misc/magic.mgc"
228 static magic_t lazy_libmagic()
231 static magic_t result = NULL;
235 /* MAGIC_MIME tells magic to return a mime of the file,
236 but you can specify different things */
237 INFO("Loading mimetype default magic database");
238 result = magic_open(MAGIC_MIME_TYPE);
239 if (result == NULL) {
240 ERROR("unable to initialize magic library");
242 /* Warning: should not use NULL for DB
243 [libmagic bug wont pass efence check] */
244 else if (magic_load(result, MAGIC_DB) != 0) {
245 ERROR("cannot load magic database: %s", magic_error(result));
254 static const char *magic_mimetype_fd(int fd)
256 magic_t lib = lazy_libmagic();
257 return lib ? magic_descriptor(lib, fd) : NULL;
262 static const char *mimetype_fd_name(int fd, const char *filename)
264 const char *result = NULL;
266 #if defined(INFER_EXTENSION)
268 * Set some well-known extensions
269 * Note that it is mandatory for example for css files in order to provide
270 * right mimetype that must be text/css (otherwise chrome browser will not
271 * load correctly css file) while libmagic returns text/plain.
273 const char *extension = strrchr(filename, '.');
275 static const char *const known[][2] = {
276 { ".js", "text/javascript" },
277 { ".html", "text/html" },
278 { ".css", "text/css" },
279 { ".ico", "image/x-icon"},
280 { ".png", "image/png" },
281 { ".svg", "image/svg+xml" },
282 { ".ttf", "application/x-font-ttf"},
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 static void afb_hreq_destroy(struct afb_hreq *hreq)
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);
325 void afb_hreq_addref(struct afb_hreq *hreq)
327 afb_xreq_addref(&hreq->xreq);
330 void afb_hreq_unref(struct afb_hreq *hreq)
332 afb_xreq_unref(&hreq->xreq);
336 * Removes the 'prefix' of 'length' from the tail of 'hreq'
337 * if and only if the prefix exists and is terminated by a leading
340 int afb_hreq_unprefix(struct afb_hreq *hreq, const char *prefix, size_t length)
342 /* check the prefix ? */
343 if (length > hreq->lentail || (hreq->tail[length] && hreq->tail[length] != '/')
344 || strncasecmp(prefix, hreq->tail, length))
347 /* removes successives / */
348 while (length < hreq->lentail && hreq->tail[length + 1] == '/')
351 /* update the tail */
352 hreq->lentail -= length;
353 hreq->tail += length;
357 int afb_hreq_valid_tail(struct afb_hreq *hreq)
359 return validsubpath(hreq->tail);
362 void afb_hreq_reply_error(struct afb_hreq *hreq, unsigned int status)
364 afb_hreq_reply_empty(hreq, status, NULL);
367 int afb_hreq_redirect_to_ending_slash_if_needed(struct afb_hreq *hreq)
371 if (hreq->url[hreq->lenurl - 1] == '/')
374 /* the redirect is needed for reliability of relative path */
375 tourl = alloca(hreq->lenurl + 2);
376 memcpy(tourl, hreq->url, hreq->lenurl);
377 tourl[hreq->lenurl] = '/';
378 tourl[hreq->lenurl + 1] = 0;
379 afb_hreq_redirect_to(hreq, tourl, 1);
383 int afb_hreq_reply_file_if_exist(struct afb_hreq *hreq, int dirfd, const char *filename)
389 char etag[1 + 2 * 8];
391 struct MHD_Response *response;
392 const char *mimetype;
394 /* Opens the file or directory */
396 fd = openat(dirfd, filename, O_RDONLY);
400 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
406 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
411 /* Retrieves file's status */
412 if (fstat(fd, &st) != 0) {
414 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
418 /* serve directory */
419 if (S_ISDIR(st.st_mode)) {
420 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
422 static const char *indexes[] = { "index.html", NULL };
424 while (indexes[i] != NULL) {
425 if (faccessat(fd, indexes[i], R_OK, 0) == 0) {
426 rc = afb_hreq_reply_file_if_exist(hreq, fd, indexes[i]);
436 /* Don't serve special files */
437 if (!S_ISREG(st.st_mode)) {
439 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
443 /* Check the method */
444 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
446 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
450 /* computes the etag */
451 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
453 /* checks the etag */
454 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
455 if (inm && 0 == strcmp(inm, etag)) {
456 /* etag ok, return NOT MODIFIED */
458 DEBUG("Not Modified: [%s]", filename);
459 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
460 status = MHD_HTTP_NOT_MODIFIED;
463 if (st.st_size != (off_t) (size_t) st.st_size) {
465 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
469 /* create the response */
470 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
471 status = MHD_HTTP_OK;
474 mimetype = mimetype_fd_name(fd, filename);
475 if (mimetype != NULL)
476 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
479 /* fills the value and send */
480 afb_hreq_reply(hreq, status, response,
481 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
482 MHD_HTTP_HEADER_ETAG, etag,
487 int afb_hreq_reply_file(struct afb_hreq *hreq, int dirfd, const char *filename)
489 int rc = afb_hreq_reply_file_if_exist(hreq, dirfd, filename);
491 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
495 int afb_hreq_reply_locale_file_if_exist(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
501 char etag[1 + 2 * 8];
503 struct MHD_Response *response;
504 const char *mimetype;
506 /* Opens the file or directory */
507 fd = locale_search_open(search, filename[0] ? filename : ".", O_RDONLY);
511 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
515 /* Retrieves file's status */
516 if (fstat(fd, &st) != 0) {
518 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
522 /* serve directory */
523 if (S_ISDIR(st.st_mode)) {
524 rc = afb_hreq_redirect_to_ending_slash_if_needed(hreq);
526 static const char *indexes[] = { "index.html", NULL };
528 size_t length = strlen(filename);
529 char *extname = alloca(length + 30); /* 30 is enough to old data of indexes */
530 memcpy(extname, filename, length);
531 if (length && extname[length - 1] != '/')
532 extname[length++] = '/';
533 while (rc == 0 && indexes[i] != NULL) {
534 strcpy(extname + length, indexes[i++]);
535 rc = afb_hreq_reply_locale_file_if_exist(hreq, search, extname);
542 /* Don't serve special files */
543 if (!S_ISREG(st.st_mode)) {
545 afb_hreq_reply_error(hreq, MHD_HTTP_FORBIDDEN);
549 /* Check the method */
550 if ((hreq->method & (afb_method_get | afb_method_head)) == 0) {
552 afb_hreq_reply_error(hreq, MHD_HTTP_METHOD_NOT_ALLOWED);
556 /* computes the etag */
557 sprintf(etag, "%08X%08X", ((int)(st.st_mtim.tv_sec) ^ (int)(st.st_mtim.tv_nsec)), (int)(st.st_size));
559 /* checks the etag */
560 inm = MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_IF_NONE_MATCH);
561 if (inm && 0 == strcmp(inm, etag)) {
562 /* etag ok, return NOT MODIFIED */
564 DEBUG("Not Modified: [%s]", filename);
565 response = MHD_create_response_from_buffer(0, empty_string, MHD_RESPMEM_PERSISTENT);
566 status = MHD_HTTP_NOT_MODIFIED;
569 if (st.st_size != (off_t) (size_t) st.st_size) {
571 afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
575 /* create the response */
576 response = MHD_create_response_from_fd((size_t) st.st_size, fd);
577 status = MHD_HTTP_OK;
580 mimetype = mimetype_fd_name(fd, filename);
581 if (mimetype != NULL)
582 MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, mimetype);
585 /* fills the value and send */
586 afb_hreq_reply(hreq, status, response,
587 MHD_HTTP_HEADER_CACHE_CONTROL, hreq->cacheTimeout,
588 MHD_HTTP_HEADER_ETAG, etag,
593 int afb_hreq_reply_locale_file(struct afb_hreq *hreq, struct locale_search *search, const char *filename)
595 int rc = afb_hreq_reply_locale_file_if_exist(hreq, search, filename);
597 afb_hreq_reply_error(hreq, MHD_HTTP_NOT_FOUND);
608 static void _mkq_add_(struct _mkq_ *mkq, char value)
610 char *text = mkq->text;
612 if (mkq->length == mkq->alloc) {
614 text = realloc(text, mkq->alloc);
622 text[mkq->length++] = value;
626 static void _mkq_add_hex_(struct _mkq_ *mkq, char value)
628 _mkq_add_(mkq, (char)(value < 10 ? value + '0' : value + 'A' - 10));
631 static void _mkq_add_esc_(struct _mkq_ *mkq, char value)
634 _mkq_add_hex_(mkq, (char)((value >> 4) & 15));
635 _mkq_add_hex_(mkq, (char)(value & 15));
638 static void _mkq_add_char_(struct _mkq_ *mkq, char value)
640 if (value <= ' ' || value >= 127)
641 _mkq_add_esc_(mkq, value);
647 _mkq_add_esc_(mkq, value);
650 _mkq_add_(mkq, value);
654 static void _mkq_append_(struct _mkq_ *mkq, const char *value)
657 _mkq_add_char_(mkq, *value++);
660 static int _mkquery_(struct _mkq_ *mkq, enum MHD_ValueKind kind, const char *key, const char *value)
662 _mkq_add_(mkq, mkq->count++ ? '&' : '?');
663 _mkq_append_(mkq, key);
666 _mkq_append_(mkq, value);
671 static char *url_with_query(struct afb_hreq *hreq, const char *url)
676 mkq.length = strlen(url);
677 mkq.alloc = mkq.length + 1000;
678 mkq.text = malloc(mkq.alloc);
679 if (mkq.text != NULL) {
680 strcpy(mkq.text, url);
681 MHD_get_connection_values(hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_mkquery_, &mkq);
687 void afb_hreq_redirect_to(struct afb_hreq *hreq, const char *url, int add_query_part)
692 wqp = add_query_part ? url_with_query(hreq, url) : NULL;
694 afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
695 MHD_HTTP_HEADER_LOCATION, to, NULL);
696 DEBUG("redirect from [%s] to [%s]", hreq->url, url);
700 const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
702 return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
705 const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
707 struct hreq_data *data = get_data(hreq, name, 0);
708 return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
711 const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
713 return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
716 int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
719 struct hreq_data *hdat = get_data(hreq, key, 1);
720 if (hdat->path != NULL) {
723 p = realloc(hdat->value, hdat->length + size + 1);
728 memcpy(&hdat->value[hdat->length], data, size);
729 hdat->length += size;
730 hdat->value[hdat->length] = 0;
734 int afb_hreq_init_download_path(const char *directory)
740 if (access(directory, R_OK|W_OK)) {
741 /* no read/write access */
744 if (stat(directory, &st)) {
748 if (!S_ISDIR(st.st_mode)) {
749 /* not a directory */
753 n = strlen(directory);
754 while(n > 1 && directory[n-1] == '/') n--;
757 /* can't allocate memory */
761 memcpy(p, directory, n);
775 static int opentempfile(char **path)
780 fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
784 fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
792 int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
796 struct hreq_data *hdat = get_data(hreq, key, 1);
798 if (hdat->value == NULL) {
799 hdat->value = strdup(file);
800 if (hdat->value == NULL)
802 fd = opentempfile(&hdat->path);
803 } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
806 fd = open(hdat->path, O_WRONLY|O_APPEND);
811 sz = write(fd, data, size);
813 hdat->length += (size_t)sz;
816 } else if (errno != EINTR)
823 static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
826 struct hreq_data *hdat = get_data(hreq, name, 0);
828 return (struct afb_arg){
830 .value = hdat->value,
834 value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
835 return (struct afb_arg){
836 .name = value == NULL ? NULL : name,
842 static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
844 json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
848 static struct json_object *req_json(struct afb_hreq *hreq)
850 struct hreq_data *hdat;
851 struct json_object *obj, *val;
855 hreq->json = obj = json_object_new_object();
858 MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
859 for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
860 if (hdat->path == NULL)
861 val = hdat->value ? json_object_new_string(hdat->value) : NULL;
863 val = json_object_new_object();
866 json_object_object_add(val, "file", json_object_new_string(hdat->value));
867 json_object_object_add(val, "path", json_object_new_string(hdat->path));
870 json_object_object_add(obj, hdat->key, val);
877 static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
879 ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
880 return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
883 static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
885 struct json_object *reply;
887 struct MHD_Response *response;
889 reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
891 reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
893 reply = afb_msg_json_reply(status, info, resp, &hreq->xreq.context, reqid);
895 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);
896 afb_hreq_reply(hreq, retcode, response, NULL);
899 static void req_fail(struct afb_hreq *hreq, const char *status, const char *info)
901 req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
904 static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info)
906 req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
909 int afb_hreq_init_req_call(struct afb_hreq *hreq, const char *api, size_t lenapi, const char *verb, size_t lenverb)
911 hreq->xreq.api = strndup(api, lenapi);
912 hreq->xreq.verb = strndup(verb, lenverb);
913 if (hreq->xreq.api == NULL || hreq->xreq.verb == NULL) {
914 ERROR("Out of memory");
918 return afb_hreq_init_context(hreq);
921 int afb_hreq_init_context(struct afb_hreq *hreq)
926 if (hreq->xreq.context.session != NULL)
929 uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
931 uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
933 uuid = afb_hreq_get_cookie(hreq, cookie_name);
935 uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
937 token = afb_hreq_get_header(hreq, long_key_for_token);
939 token = afb_hreq_get_argument(hreq, long_key_for_token);
941 token = afb_hreq_get_argument(hreq, short_key_for_token);
943 return afb_context_connect(&hreq->xreq.context, uuid, token);
946 int afb_hreq_init_cookie(int port, const char *path, int maxage)
953 cookie_setter = NULL;
956 rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
959 rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
960 cookie_name, path, maxage);
966 struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
971 struct afb_hreq *afb_hreq_create()
973 struct afb_hreq *hreq = calloc(1, sizeof *hreq);
975 /* init the request */
976 hreq->xreq.refcount = 1;
977 hreq->xreq.query = hreq;
978 hreq->xreq.queryitf = &afb_hreq_xreq_query_itf;
979 hreq->reqid = ++global_reqids;