From c1b74df08076cd850617e500163fdb5fc8d55f3a Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Tue, 7 Sep 2010 21:07:17 +0000 Subject: improve rewriter. No< it's possible to pass a variable in path as so you can do /somepath/something or /somepath/.txt ... git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@993532 13f79535-47bb-0310-9956-ffa450edef68 --- src/couchdb/couch_httpd_rewrite.erl | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src') diff --git a/src/couchdb/couch_httpd_rewrite.erl b/src/couchdb/couch_httpd_rewrite.erl index 391fa016..36b90928 100644 --- a/src/couchdb/couch_httpd_rewrite.erl +++ b/src/couchdb/couch_httpd_rewrite.erl @@ -106,6 +106,15 @@ %% {"from": "/a", /a?foo=b /some/b foo =:= b %% "to": "/some/:foo", %% }} +%% +%% {"from": "/a/" /a/b /some/b foo =:= b +%% "to": "/a/b", +%% }} +%% +%% {"from": "/a/.blah" /a/b /some/b foo =:= b +%% "to": "/a/b", +%% }} + @@ -322,6 +331,14 @@ bind_path([?MATCH_ALL], [Match|_RestMatch]=Rest, Bindings) -> {ok, Rest, [{?MATCH_ALL, Match}|Bindings]}; bind_path(_, [], _) -> fail; +bind_path([{bind, {Token, MatchRe}}|RestToken], + [Match|RestMatch],Bindings) -> + case re:run(Match, MatchRe, [{capture, all, binary}]) of + {match, [_, Match1]} -> + bind_path(RestToken, RestMatch, [{{bind, Token}, Match1}|Bindings]); + _ -> + fail + end; bind_path([{bind, Token}|RestToken],[Match|RestMatch],Bindings) -> bind_path(RestToken, RestMatch, [{{bind, Token}, Match}|Bindings]); bind_path([Token|RestToken], [Token|RestMatch], Bindings) -> @@ -399,8 +416,18 @@ path_to_list([<<"..">>|R], Acc, DotDotCount) -> path_to_list(R, [<<"..">>|Acc], DotDotCount+1); path_to_list([P|R], Acc, DotDotCount) -> P1 = case P of + <<"<", _Rest/binary>> -> + {ok, VarRe} = re:compile(<<"<([^>].*)>(.*)">>), + case re:run(P, VarRe, [{capture, all, binary}]) of + {match, [_, Var, Match]} -> + {ok, MatchRe} = re:compile(<<"(.*)", Match/binary>>), + {bind, {Var, MatchRe}}; + _ -> P + end; <<":", Var/binary>> -> to_binding(Var); + + _ -> P end, path_to_list(R, [P1|Acc], DotDotCount). -- cgit v1.2.3