chiark / gitweb /
journal: support epxorting the journal in a format suitable for text/event-stream
[elogind.git] / src / journal / journal-gatewayd.c
index 274ef5ffeb08a99d3e9798ef7aa4ddfd82261d46..b2d4abf4fee705a2757fbc7611ff6c17ccfe63e1 100644 (file)
@@ -49,12 +49,14 @@ typedef struct RequestMeta {
         int argument_parse_error;
 
         bool follow;
+        bool discrete;
 } RequestMeta;
 
 static const char* const mime_types[_OUTPUT_MODE_MAX] = {
         [OUTPUT_SHORT] = "text/plain",
         [OUTPUT_JSON] = "application/json",
-        [OUTPUT_EXPORT] = "application/vnd.fdo.journal"
+        [OUTPUT_JSON_SSE] = "text/event-stream",
+        [OUTPUT_EXPORT] = "application/vnd.fdo.journal",
 };
 
 static RequestMeta *request_meta(void **connection_cls) {
@@ -205,6 +207,19 @@ static ssize_t request_reader_entries(
                         return MHD_CONTENT_READER_END_OF_STREAM;
                 }
 
+                if (m->discrete) {
+                        assert(m->cursor);
+
+                        r = sd_journal_test_cursor(m->journal, m->cursor);
+                        if (r < 0) {
+                                log_error("Failed to test cursor: %s", strerror(-r));
+                                return MHD_CONTENT_READER_END_WITH_ERROR;
+                        }
+
+                        if (r == 0)
+                                return MHD_CONTENT_READER_END_OF_STREAM;
+                }
+
                 pos -= m->size;
                 m->delta += m->size;
 
@@ -272,6 +287,8 @@ static int request_parse_accept(
 
         if (streq(accept, mime_types[OUTPUT_JSON]))
                 m->mode = OUTPUT_JSON;
+        else if (streq(accept, mime_types[OUTPUT_JSON_SSE]))
+                m->mode = OUTPUT_JSON_SSE;
         else if (streq(accept, mime_types[OUTPUT_EXPORT]))
                 m->mode = OUTPUT_EXPORT;
         else
@@ -380,6 +397,22 @@ static int request_parse_arguments_iterator(
                 return MHD_YES;
         }
 
+        if (streq(key, "discrete")) {
+                if (isempty(value)) {
+                        m->discrete = true;
+                        return MHD_YES;
+                }
+
+                r = parse_boolean(value);
+                if (r < 0) {
+                        m->argument_parse_error = r;
+                        return MHD_NO;
+                }
+
+                m->discrete = r;
+                return MHD_YES;
+        }
+
         p = strjoin(key, "=", strempty(value), NULL);
         if (!p) {
                 m->argument_parse_error = log_oom();
@@ -436,6 +469,14 @@ static int request_handler_entries(
         if (request_parse_arguments(m, connection) < 0)
                 return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Failed to parse URL arguments.\n");
 
+        if (m->discrete) {
+                if (!m->cursor)
+                        return respond_error(connection, MHD_HTTP_BAD_REQUEST, "Discrete seeks require a cursor specification.\n");
+
+                m->n_entries = 1;
+                m->n_entries_set = true;
+        }
+
         if (m->cursor)
                 r = sd_journal_seek_cursor(m->journal, m->cursor);
         else if (m->n_skip >= 0)