+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);
+}
+