couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chewbra...@apache.org
Subject [1/3] chttpd commit: updated refs/heads/1843-feature-bigcouch-clustered-eventsource-changes-feed to 17268d7
Date Tue, 27 May 2014 23:02:26 GMT
Repository: couchdb-chttpd
Updated Branches:
  refs/heads/1843-feature-bigcouch-clustered-eventsource-changes-feed 6a74f9b99 -> 17268d7bb
(forced update)


Add support for eventsource changes feeds


Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/b743d755
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/b743d755
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/b743d755

Branch: refs/heads/1843-feature-bigcouch-clustered-eventsource-changes-feed
Commit: b743d7552b8596eb2f540c3d02bd87efa0423cad
Parents: 644ca97
Author: Russell Branca <chewbranca@apache.org>
Authored: Wed May 21 17:22:39 2014 -0700
Committer: Russell Branca <chewbranca@apache.org>
Committed: Tue May 27 16:01:57 2014 -0700

----------------------------------------------------------------------
 src/chttpd_db.erl | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/b743d755/src/chttpd_db.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_db.erl b/src/chttpd_db.erl
index 0c08dd9..969da64 100644
--- a/src/chttpd_db.erl
+++ b/src/chttpd_db.erl
@@ -72,7 +72,7 @@ handle_changes_req(#httpd{method='GET'}=Req, Db) ->
                 ChangesArgs)
         end);
     Feed ->
-        % "longpoll" or "continuous"
+        % "longpoll" or "continuous" or "eventsource"
         fabric:changes(Db, fun changes_callback/2, {Feed, Req}, ChangesArgs)
     end;
 handle_changes_req(#httpd{path_parts=[_,<<"_changes">>]}=Req, _Db) ->
@@ -91,6 +91,26 @@ changes_callback({stop, EndSeq0}, {"continuous", Resp}) ->
         [?JSON_ENCODE({[{<<"last_seq">>, EndSeq}]}) | "\n"]),
     chttpd:end_delayed_json_response(Resp1);
 
+% callbacks for eventsource feed (newline-delimited eventsource Objects)
+changes_callback(start, {"eventsource", Req}) ->
+    Headers = [
+        {"Content-Type", "text/event-stream"},
+        {"Cache-Control", "no-cache"}
+    ],
+    {ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers),
+    {ok, {"eventsource", Resp}};
+changes_callback({change, {ChangeProp}=Change}, {"eventsource", Resp}) ->
+    Seq = proplists:get_value(seq, ChangeProp),
+    Chunk = [
+        "data: ", ?JSON_ENCODE(Change),
+        "\n", "id: ", ?JSON_ENCODE(Seq),
+        "\n\n"
+    ],
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, [Chunk]),
+    {ok, {"eventsource", Resp1}};
+changes_callback({stop, _EndSeq}, {"eventsource", Resp}) ->
+    chttpd:end_delayed_json_response(Resp);
+
 % callbacks for longpoll and normal (single JSON Object)
 changes_callback(start, {"normal", {"Etag", Etag}, Req}) ->
     FirstChunk = "{\"results\":[\n",
@@ -1188,13 +1208,15 @@ parse_doc_query(Req) ->
 
 parse_changes_query(Req) ->
     lists:foldl(fun({Key, Value}, Args) ->
-        case {Key, Value} of
+        case {string:to_lower(Key), Value} of
         {"feed", _} ->
             Args#changes_args{feed=Value};
         {"descending", "true"} ->
             Args#changes_args{dir=rev};
         {"since", _} ->
             Args#changes_args{since=Value};
+        {"last-event-id", _} ->
+            Args#changes_args{since=Value};
         {"limit", _} ->
             Args#changes_args{limit=list_to_integer(Value)};
         {"style", _} ->


Mime
View raw message