Reset rewrite counter on new request We were spuriously throwing rewrite limit exceeded for non-looping rewrites. This patch resets the count to zero at the start of a new request and adds a test.
COUCHDB-1651 Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/09063e93 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/09063e93 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/09063e93 Branch: refs/heads/1.2.x Commit: 09063e937d1c7884ceedd7738bb309749df3b169 Parents: 1f22df7 Author: Robert Newson <[email protected]> Authored: Sun Feb 24 20:15:51 2013 +0000 Committer: Robert Newson <[email protected]> Committed: Sun Feb 24 20:35:49 2013 +0000 ---------------------------------------------------------------------- CHANGES | 3 +++ share/www/script/test/rewrite.js | 19 ++++++++++++++++++- src/couchdb/couch_db.hrl | 2 ++ src/couchdb/couch_httpd.erl | 2 ++ src/couchdb/couch_httpd_rewrite.erl | 17 +++++++---------- 5 files changed, 32 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/09063e93/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index 15b23b8..44a310b 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,9 @@ Version 1.2.2 Note that this version has not been released yet. +* Reset rewrite counter on new request, avoiding unnecessary request + failures due to bogus rewrite limit reports + Version 1.2.1 ------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/09063e93/share/www/script/test/rewrite.js ---------------------------------------------------------------------- diff --git a/share/www/script/test/rewrite.js b/share/www/script/test/rewrite.js index 352e6b9..6a7fa10 100644 --- a/share/www/script/test/rewrite.js +++ b/share/www/script/test/rewrite.js @@ -464,6 +464,7 @@ couchTests.rewrite = function(debug) { }; db.save(ddoc_loop); + // Assert loop detection run_on_modified_server( [{section: "httpd", key: "rewrite_limit", @@ -471,6 +472,22 @@ couchTests.rewrite = function(debug) { function(){ var url = "/test_suite_db/_design/loop/_rewrite/loop"; var xhr = CouchDB.request("GET", url); - T(xhr.status = 400); + TEquals(400, xhr.status); + }); + + // Assert serial execution is not spuriously counted as loop + run_on_modified_server( + [{section: "httpd", + key: "rewrite_limit", + value: "2"}, + {section: "httpd", + key: "secure_rewrites", + value: "false"}], + function(){ + var url = "/test_suite_db/_design/test/_rewrite/foo"; + for (var i=0; i < 5; i++) { + var xhr = CouchDB.request("GET", url); + TEquals(200, xhr.status); + } }); } http://git-wip-us.apache.org/repos/asf/couchdb/blob/09063e93/src/couchdb/couch_db.hrl ---------------------------------------------------------------------- diff --git a/src/couchdb/couch_db.hrl b/src/couchdb/couch_db.hrl index 59a5815..74dd454 100644 --- a/src/couchdb/couch_db.hrl +++ b/src/couchdb/couch_db.hrl @@ -21,6 +21,8 @@ % the lowest possible database sequence number -define(LOWEST_SEQ, 0). +-define(REWRITE_COUNT, couch_rewrite_count). + -define(JSON_ENCODE(V), ejson:encode(V)). -define(JSON_DECODE(V), ejson:decode(V)). http://git-wip-us.apache.org/repos/asf/couchdb/blob/09063e93/src/couchdb/couch_httpd.erl ---------------------------------------------------------------------- diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 58f5ec6..79ef6fb 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -219,6 +219,8 @@ make_fun_spec_strs(SpecStr) -> handle_request(MochiReq, DefaultFun, UrlHandlers, DbUrlHandlers, DesignUrlHandlers) -> + %% reset rewrite count for new request + erlang:put(?REWRITE_COUNT, 0), MochiReq1 = couch_httpd_vhost:dispatch_host(MochiReq), http://git-wip-us.apache.org/repos/asf/couchdb/blob/09063e93/src/couchdb/couch_httpd_rewrite.erl ---------------------------------------------------------------------- diff --git a/src/couchdb/couch_httpd_rewrite.erl b/src/couchdb/couch_httpd_rewrite.erl index 207891a..d8c7be9 100644 --- a/src/couchdb/couch_httpd_rewrite.erl +++ b/src/couchdb/couch_httpd_rewrite.erl @@ -119,17 +119,14 @@ handle_rewrite_req(#httpd{ Prefix = <<"/", DbName/binary, "/", DesignId/binary>>, QueryList = lists:map(fun decode_query_value/1, couch_httpd:qs(Req)), - MaxRewritesList = couch_config:get("httpd", "rewrite_limit", "100"), - MaxRewrites = list_to_integer(MaxRewritesList), - NRewrites = case get(couch_rewrite_count) of - undefined -> - put(couch_rewrite_count, 1); - NumRewrites when NumRewrites < MaxRewrites -> - put(couch_rewrite_count, NumRewrites + 1); - _ -> - throw({bad_request, <<"Exceeded rewrite recursion limit">>}) + RewritesSoFar = erlang:get(?REWRITE_COUNT), + MaxRewrites = list_to_integer(couch_config:get("httpd", "rewrite_limit", "100")), + case RewritesSoFar >= MaxRewrites of + true -> + throw({bad_request, <<"Exceeded rewrite recursion limit">>}); + false -> + erlang:put(?REWRITE_COUNT, RewritesSoFar + 1) end, - #doc{body={Props}} = DDoc, % get rules from ddoc
