+ afb_hreq_reply_static(hreq, MHD_HTTP_MOVED_PERMANENTLY, 0, NULL,
+ MHD_HTTP_HEADER_LOCATION, url, NULL);
+ if (verbosity)
+ fprintf(stderr, "redirect from [%s] to [%s]\n", hreq->url, url);
+ return 1;
+}
+
+const char *afb_hreq_get_cookie(struct afb_hreq *hreq, const char *name)
+{
+ return MHD_lookup_connection_value(hreq->connection, MHD_COOKIE_KIND, name);
+}
+
+const char *afb_hreq_get_argument(struct afb_hreq *hreq, const char *name)
+{
+ struct hreq_data *data = get_data(hreq, name, 0);
+ return data ? data->value : MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name);
+}
+
+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;
+}
+
+struct afb_req afb_hreq_to_req(struct afb_hreq *hreq)
+{
+ return (struct afb_req){ .itf = &afb_hreq_itf, .req_closure = hreq };
+}
+
+static struct afb_arg req_get(struct afb_hreq *hreq, const char *name)
+{
+ struct hreq_data *hdat = get_data(hreq, name, 0);
+ if (hdat)
+ return (struct afb_arg){
+ .name = hdat->key,
+ .value = hdat->value,
+ .path = hdat->path
+ };
+
+ return (struct afb_arg){
+ .name = name,
+ .value = MHD_lookup_connection_value(hreq->connection, MHD_GET_ARGUMENT_KIND, name),
+ .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, char *buffer, size_t size)
+{
+ afb_hreq_reply_free(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(obj)+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 *token, *uuid;