+const char *afb_hreq_get_header(struct afb_hreq *hreq, const char *name)
+{
+ return MHD_lookup_connection_value(hreq->connection, MHD_HEADER_KIND, name);
+}
+
+int afb_hreq_post_add(struct afb_hreq *hreq, const char *key, const char *data, size_t size)
+{
+ void *p;
+ struct hreq_data *hdat = get_data(hreq, key, 1);
+ if (hdat->path != NULL) {
+ return 0;
+ }
+ p = realloc(hdat->value, hdat->length + size + 1);
+ if (p == NULL) {
+ return 0;
+ }
+ hdat->value = p;
+ memcpy(&hdat->value[hdat->length], data, size);
+ hdat->length += size;
+ hdat->value[hdat->length] = 0;
+ return 1;
+}
+
+int afb_hreq_init_download_path(const char *directory)
+{
+ struct stat st;
+ size_t n;
+ char *p;
+
+ if (access(directory, R_OK|W_OK)) {
+ /* no read/write access */
+ return -1;
+ }
+ if (stat(directory, &st)) {
+ /* can't get info */
+ return -1;
+ }
+ if (!S_ISDIR(st.st_mode)) {
+ /* not a directory */
+ errno = ENOTDIR;
+ return -1;
+ }
+ n = strlen(directory);
+ while(n > 1 && directory[n-1] == '/') n--;
+ p = malloc(n + 8);
+ if (p == NULL) {
+ /* can't allocate memory */
+ errno = ENOMEM;
+ return -1;
+ }
+ memcpy(p, directory, n);
+ p[n++] = '/';
+ p[n++] = 'X';
+ p[n++] = 'X';
+ p[n++] = 'X';
+ p[n++] = 'X';
+ p[n++] = 'X';
+ p[n++] = 'X';
+ p[n] = 0;
+ free(tmp_pattern);
+ tmp_pattern = p;
+ return 0;
+}
+
+static int opentempfile(char **path)
+{
+ int fd;
+ char *fname;
+
+ fname = strdup(tmp_pattern ? : "XXXXXX"); /* TODO improve the path */
+ if (fname == NULL)
+ return -1;
+
+ fd = mkostemp(fname, O_CLOEXEC|O_WRONLY);
+ if (fd < 0)
+ free(fname);
+ else
+ *path = fname;
+ return fd;
+}
+
+int afb_hreq_post_add_file(struct afb_hreq *hreq, const char *key, const char *file, const char *data, size_t size)
+{
+ int fd;
+ ssize_t sz;
+ struct hreq_data *hdat = get_data(hreq, key, 1);
+
+ if (hdat->value == NULL) {
+ hdat->value = strdup(file);
+ if (hdat->value == NULL)
+ return 0;
+ fd = opentempfile(&hdat->path);
+ } else if (strcmp(hdat->value, file) || hdat->path == NULL) {
+ return 0;
+ } else {
+ fd = open(hdat->path, O_WRONLY|O_APPEND);
+ }
+ if (fd < 0)
+ return 0;
+ while (size) {
+ sz = write(fd, data, size);
+ if (sz >= 0) {
+ hdat->length += (size_t)sz;
+ size -= (size_t)sz;
+ data += sz;
+ } else if (errno != EINTR)
+ break;
+ }
+ close(fd);
+ return !size;
+}
+
+static struct afb_arg req_get(struct afb_xreq *xreq, const char *name)
+{
+ const char *value;
+ struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
+ struct hreq_data *hdat = get_data(hreq, name, 0);
+ if (hdat)
+ return (struct afb_arg){
+ .name = hdat->key,
+ .value = hdat->value,
+ .path = hdat->path
+ };
+
+ value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
+ return (struct afb_arg){
+ .name = value == NULL ? NULL : name,
+ .value = value,
+ .path = NULL
+ };
+}
+
+static int _iterargs_(struct json_object *obj, enum MHD_ValueKind kind, const char *key, const char *value)
+{
+ json_object_object_add(obj, key, value ? json_object_new_string(value) : NULL);
+ return 1;
+}
+
+static struct json_object *req_json(struct afb_xreq *xreq)
+{
+ struct hreq_data *hdat;
+ struct json_object *obj, *val;
+ struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
+
+ obj = hreq->json;
+ if (obj == NULL) {
+ hreq->json = obj = json_object_new_object();
+ if (obj == NULL) {
+ } else {
+ MHD_get_connection_values (hreq->connection, MHD_GET_ARGUMENT_KIND, (void*)_iterargs_, obj);
+ for (hdat = hreq->data ; hdat ; hdat = hdat->next) {
+ if (hdat->path == NULL)
+ val = hdat->value ? json_object_new_string(hdat->value) : NULL;
+ else {
+ val = json_object_new_object();
+ if (val == NULL) {
+ } else {
+ json_object_object_add(val, "file", json_object_new_string(hdat->value));
+ json_object_object_add(val, "path", json_object_new_string(hdat->path));
+ }
+ }
+ json_object_object_add(obj, hdat->key, val);
+ }
+ }
+ }
+ return obj;
+}
+
+static ssize_t send_json_cb(json_object *obj, uint64_t pos, char *buf, size_t max)
+{
+ ssize_t len = stpncpy(buf, json_object_to_json_string_ext(obj, JSON_C_TO_STRING_PLAIN)+pos, max) - buf;
+ return len ? : (ssize_t)MHD_CONTENT_READER_END_OF_STREAM;
+}
+
+static void req_reply(struct afb_hreq *hreq, unsigned retcode, const char *status, const char *info, json_object *resp)
+{
+ struct json_object *reply;
+ const char *reqid;
+ struct MHD_Response *response;
+
+ reqid = afb_hreq_get_argument(hreq, long_key_for_reqid);
+ if (reqid == NULL)
+ reqid = afb_hreq_get_argument(hreq, short_key_for_reqid);
+
+ reply = afb_msg_json_reply(status, info, resp, &hreq->xreq.context, reqid);
+
+ 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);
+ afb_hreq_reply(hreq, retcode, response, NULL);
+}
+
+static void req_fail(struct afb_xreq *xreq, const char *status, const char *info)
+{
+ struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
+ req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
+}
+
+static void req_success(struct afb_xreq *xreq, json_object *obj, const char *info)
+{
+ struct afb_hreq *hreq = CONTAINER_OF_XREQ(struct afb_hreq, xreq);
+ req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
+}
+
+void afb_hreq_call(struct afb_hreq *hreq, struct afb_apiset *apiset, const char *api, size_t lenapi, const char *verb, size_t lenverb)
+{
+ hreq->xreq.request.api = strndup(api, lenapi);
+ hreq->xreq.request.verb = strndup(verb, lenverb);
+ if (hreq->xreq.request.api == NULL || hreq->xreq.request.verb == NULL) {
+ ERROR("Out of memory");
+ afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
+ } else if (afb_hreq_init_context(hreq) < 0) {
+ afb_hreq_reply_error(hreq, MHD_HTTP_INTERNAL_SERVER_ERROR);
+ } else {
+ afb_xreq_unhooked_addref(&hreq->xreq);
+ afb_xreq_process(&hreq->xreq, apiset);
+ }
+}
+
+int afb_hreq_init_context(struct afb_hreq *hreq)
+{
+ const char *uuid;
+ const char *token;
+
+ if (hreq->xreq.context.session != NULL)
+ return 0;
+
+ uuid = afb_hreq_get_header(hreq, long_key_for_uuid);
+ if (uuid == NULL)
+ uuid = afb_hreq_get_argument(hreq, long_key_for_uuid);
+ if (uuid == NULL)
+ uuid = afb_hreq_get_cookie(hreq, cookie_name);
+ if (uuid == NULL)
+ uuid = afb_hreq_get_argument(hreq, short_key_for_uuid);
+
+ token = afb_hreq_get_header(hreq, long_key_for_token);
+ if (token == NULL)
+ token = afb_hreq_get_argument(hreq, long_key_for_token);
+ if (token == NULL)
+ token = afb_hreq_get_argument(hreq, short_key_for_token);
+
+ return afb_context_connect(&hreq->xreq.context, uuid, token);
+}
+
+int afb_hreq_init_cookie(int port, const char *path, int maxage)
+{
+ int rc;
+
+ free(cookie_name);
+ free(cookie_setter);
+ cookie_name = NULL;
+ cookie_setter = NULL;
+
+ path = path ? : "/";
+ rc = asprintf(&cookie_name, "%s-%d", long_key_for_uuid, port);
+ if (rc < 0)
+ return 0;
+ rc = asprintf(&cookie_setter, "%s=%%s; Path=%s; Max-Age=%d; HttpOnly",
+ cookie_name, path, maxage);
+ if (rc < 0)
+ return 0;
+ return 1;
+}
+
+struct afb_xreq *afb_hreq_to_xreq(struct afb_hreq *hreq)
+{
+ return &hreq->xreq;
+}
+
+struct afb_hreq *afb_hreq_create()
+{
+ struct afb_hreq *hreq = calloc(1, sizeof *hreq);
+ if (hreq) {
+ /* init the request */
+ afb_xreq_init(&hreq->xreq, &afb_hreq_xreq_query_itf);
+ hreq->reqid = ++global_reqids;
+ }
+ return hreq;
+}