+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;
+}
+
+struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
+{
+ return (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq };
+}
+
+static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
+{
+ const char *value;
+ 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_hreq *hreq)
+{
+ struct hreq_data *hdat;
+ struct json_object *obj, *val;
+
+ 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 const char *req_raw(struct afb_hreq *hreq, size_t *size)
+{
+ const char *result = json_object_get_string(req_json(hreq));
+ *size = result ? strlen(result) : 0;
+ return result;
+}
+
+static void req_send(struct afb_hreq *hreq, const char *buffer, size_t size)
+{
+ afb_hreq_reply_copy(hreq, MHD_HTTP_OK, size, buffer, NULL);
+}
+
+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->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_hreq *hreq, const char *status, const char *info)
+{
+ req_reply(hreq, MHD_HTTP_OK, status, info, NULL);
+}
+
+static void req_success(struct afb_hreq *hreq, json_object *obj, const char *info)
+{
+ req_reply(hreq, MHD_HTTP_OK, "success", info, obj);
+}
+
+static int req_subscribe_unsubscribe_error(struct afb_hreq *hreq, struct afb_event event)
+{
+ errno = EINVAL;
+ return -1;
+}
+
+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)
+{
+ afb_subcall(&hreq->context, api, verb, args, callback, closure, (struct afb_req){ .itf = &afb_hreq_req_itf, .closure = hreq });
+}
+
+int afb_hreq_init_req_call(struct afb_hreq *hreq, const char *api, size_t lenapi, const char *verb, size_t lenverb)
+{
+ free(hreq->api);
+ free(hreq->verb);
+ hreq->api = strndup(api, lenapi);
+ hreq->verb = strndup(verb, lenverb);
+ if (hreq->api == NULL || hreq->verb == NULL) {
+ ERROR("Out of memory");
+ errno = ENOMEM;
+ return -1;
+ }
+ return afb_hreq_init_context(hreq);
+}
+
+int afb_hreq_init_context(struct afb_hreq *hreq)
+{
+ const char *uuid;
+ const char *token;
+
+ if (hreq->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->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;
+}
+