You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by rn...@apache.org on 2014/05/30 13:21:05 UTC
[3/3] chttpd commit: updated refs/heads/master to 002a908
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/c7f8eb7c
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/c7f8eb7c
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/c7f8eb7c
Branch: refs/heads/master
Commit: c7f8eb7c1ad39a1f4b4897cb42bec4c62e8f7a4e
Parents: 9e72ce8
Author: Russell Branca <ch...@apache.org>
Authored: Wed May 21 17:22:39 2014 -0700
Committer: Russell Branca <ch...@apache.org>
Committed: Wed May 28 15:44: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/c7f8eb7c/src/chttpd_db.erl
----------------------------------------------------------------------
diff --git a/src/chttpd_db.erl b/src/chttpd_db.erl
index e77e2b7..e62b106 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",
@@ -1190,13 +1210,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", _} ->