+static void afb_hreq_reply_v(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, va_list args)
+{
+ char *cookie;
+ const char *k, *v;
+
+ if (hreq->replied != 0)
+ return;
+
+ k = va_arg(args, const char *);
+ while (k != NULL) {
+ v = va_arg(args, const char *);
+ MHD_add_response_header(response, k, v);
+ k = va_arg(args, const char *);
+ }
+ v = afb_context_sent_uuid(&hreq->context);
+ if (v != NULL && asprintf(&cookie, cookie_setter, v) > 0) {
+ MHD_add_response_header(response, MHD_HTTP_HEADER_SET_COOKIE, cookie);
+ free(cookie);
+ }
+ MHD_queue_response(hreq->connection, status, response);
+ MHD_destroy_response(response);
+
+ hreq->replied = 1;
+ if (hreq->suspended != 0) {
+ extern void run_micro_httpd(struct afb_hsrv *hsrv);
+ MHD_resume_connection (hreq->connection);
+ hreq->suspended = 0;
+ run_micro_httpd(hreq->hsrv);
+ }
+}
+
+void afb_hreq_reply(struct afb_hreq *hreq, unsigned status, struct MHD_Response *response, ...)
+{
+ va_list args;
+ va_start(args, response);
+ afb_hreq_reply_v(hreq, status, response, args);
+ va_end(args);
+}
+
+void afb_hreq_reply_empty(struct afb_hreq *hreq, unsigned status, ...)
+{
+ va_list args;
+ va_start(args, status);
+ afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer(0, NULL, MHD_RESPMEM_PERSISTENT), args);
+ va_end(args);
+}
+
+void afb_hreq_reply_static(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
+{
+ va_list args;
+ va_start(args, buffer);
+ afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_PERSISTENT), args);
+ va_end(args);
+}
+
+void afb_hreq_reply_copy(struct afb_hreq *hreq, unsigned status, size_t size, const char *buffer, ...)
+{
+ va_list args;
+ va_start(args, buffer);
+ afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, (char*)buffer, MHD_RESPMEM_MUST_COPY), args);
+ va_end(args);
+}
+
+void afb_hreq_reply_free(struct afb_hreq *hreq, unsigned status, size_t size, char *buffer, ...)
+{
+ va_list args;
+ va_start(args, buffer);
+ afb_hreq_reply_v(hreq, status, MHD_create_response_from_buffer((unsigned)size, buffer, MHD_RESPMEM_MUST_FREE), args);
+ va_end(args);
+}
+
+#if defined(USE_MAGIC_MIME_TYPE)
+
+#if !defined(MAGIC_DB)
+#define MAGIC_DB "/usr/share/misc/magic.mgc"
+#endif
+
+static magic_t lazy_libmagic()
+{
+ static int done = 0;
+ static magic_t result = NULL;
+
+ if (!done) {
+ done = 1;
+ /* MAGIC_MIME tells magic to return a mime of the file,
+ but you can specify different things */
+ INFO("Loading mimetype default magic database");
+ result = magic_open(MAGIC_MIME_TYPE);
+ if (result == NULL) {
+ ERROR("unable to initialize magic library");
+ }
+ /* Warning: should not use NULL for DB
+ [libmagic bug wont pass efence check] */
+ else if (magic_load(result, MAGIC_DB) != 0) {
+ ERROR("cannot load magic database: %s", magic_error(result));
+ magic_close(result);
+ result = NULL;
+ }
+ }
+
+ return result;
+}
+
+static const char *magic_mimetype_fd(int fd)
+{
+ magic_t lib = lazy_libmagic();
+ return lib ? magic_descriptor(lib, fd) : NULL;
+}
+
+#endif
+
+static const char *mimetype_fd_name(int fd, const char *filename)
+{
+ const char *result = NULL;
+
+#if defined(INFER_EXTENSION)
+ const char *extension = strrchr(filename, '.');
+ if (extension) {
+ static const char *const known[][2] = {
+ { ".js", "text/javascript" },
+ { ".html", "text/html" },
+ { ".css", "text/css" },
+ { NULL, NULL }
+ };
+ int i = 0;
+ while (known[i][0]) {
+ if (!strcasecmp(extension, known[i][0])) {
+ result = known[i][1];
+ break;
+ }
+ i++;
+ }
+ }
+#endif
+#if defined(USE_MAGIC_MIME_TYPE)
+ if (result == NULL)
+ result = magic_mimetype_fd(fd);
+#endif
+ return result;
+}
+
+void afb_hreq_addref(struct afb_hreq *hreq)
+{
+ hreq->refcount++;
+}
+
+void afb_hreq_unref(struct afb_hreq *hreq)
+{
+ struct hreq_data *data;
+
+ if (hreq == NULL || --hreq->refcount)
+ return;
+
+ if (hreq->postform != NULL)
+ MHD_destroy_post_processor(hreq->postform);
+ for (data = hreq->data; data; data = hreq->data) {
+ hreq->data = data->next;
+ if (data->path) {
+ unlink(data->path);
+ free(data->path);
+ }
+ free(data->key);
+ free(data->value);
+ free(data);
+ }
+ afb_context_disconnect(&hreq->context);
+ json_object_put(hreq->json);
+ free(hreq);
+}
+