Author: benoitc
Date: Wed Aug 4 20:11:53 2010
New Revision: 982388
URL: http://svn.apache.org/viewvc?rev=982388&view=rev
Log:
add wildcard support in query args so it's now possible to have rules
like :
{
"from": "/welcome4/*",
"to" : "_show/welcome3",
"query": {
"name": "*"
}
}
rewriting "_rewrite/welcome4/test" to "_show/welcome3?name=test"
or
{
"from": "/welcome5/*",
"to" : "_show/*",
}
rewriting "_rewrite/welcome5/welcome3" to "_show/welcome3"
Modified:
couchdb/trunk/share/www/script/test/rewrite.js
couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl
Modified: couchdb/trunk/share/www/script/test/rewrite.js
URL:
http://svn.apache.org/viewvc/couchdb/trunk/share/www/script/test/rewrite.js?rev=982388&r1=982387&r2=982388&view=diff
==============================================================================
--- couchdb/trunk/share/www/script/test/rewrite.js (original)
+++ couchdb/trunk/share/www/script/test/rewrite.js Wed Aug 4 20:11:53 2010
@@ -84,6 +84,20 @@ couchTests.rewrite = function(debug) {
"method": "GET"
},
{
+ "from": "/welcome4/*",
+ "to" : "_show/welcome3",
+ "query": {
+ "name": "*"
+ }
+ },
+ {
+ "from": "/welcome5/*",
+ "to" : "_show/*",
+ "query": {
+ "name": "*"
+ }
+ },
+ {
"from": "simpleForm/basicView",
"to": "_list/simpleForm/basicView",
},
@@ -169,6 +183,9 @@ couchTests.rewrite = function(debug) {
"welcome2": stringFun(function(doc, req) {
return "Welcome " + doc.name;
}),
+ "welcome3": stringFun(function(doc,req) {
+ return "Welcome " + req.query["name"];
+ })
},
updates: {
"hello" : stringFun(function(doc, req) {
@@ -288,6 +305,12 @@ couchTests.rewrite = function(debug) {
xhr = CouchDB.request("GET",
"/test_suite_db/_design/test/_rewrite/welcome3/test");
T(xhr.responseText == "Welcome test");
+
+ req = CouchDB.request("GET",
"/test_suite_db/_design/test/_rewrite/welcome4/user");
+ T(req.responseText == "Welcome user");
+
+ req = CouchDB.request("GET",
"/test_suite_db/_design/test/_rewrite/welcome5/welcome3");
+ T(req.responseText == "Welcome welcome3");
// get with query params
@@ -368,4 +391,4 @@ couchTests.rewrite = function(debug) {
});
-}
\ No newline at end of file
+}
Modified: couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl
URL:
http://svn.apache.org/viewvc/couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl?rev=982388&r1=982387&r2=982388&view=diff
==============================================================================
--- couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl (original)
+++ couchdb/trunk/src/couchdb/couch_httpd_rewrite.erl Wed Aug 4 20:11:53 2010
@@ -245,6 +245,8 @@ replace_var(Key, Value, Bindings) ->
case Value of
<<":", Var/binary>> ->
get_var(Var, Bindings, Value);
+ <<"*">> ->
+ get_var(Value, Bindings, Value);
_ when is_list(Value) ->
Value1 = lists:foldr(fun(V, Acc) ->
V1 = case V of
@@ -254,8 +256,9 @@ replace_var(Key, Value, Bindings) ->
iolist_to_binary(V2);
V2 -> V2
end;
+ <<"*">> ->
+ get_var(V, Bindings, V);
_ ->
-
V
end,
[V1|Acc]
@@ -303,7 +306,7 @@ make_new_path([P|Rest], Bindings, Remain
%% method rule is '*', which is the default, all
%% request method will bind. It allows us to make rules
%% depending on HTTP method.
-bind_method(?MATCH_ALL, _Method) ->
+bind_method(?MATCH_ALL, _Method ) ->
true;
bind_method({bind, Method}, Method) ->
true;
@@ -315,8 +318,8 @@ bind_method(_, _) ->
%% to the current url by pattern matching
bind_path([], [], Bindings) ->
{ok, [], Bindings};
-bind_path([?MATCH_ALL], Rest, Bindings) when is_list(Rest) ->
- {ok, Rest, Bindings};
+bind_path([?MATCH_ALL], [Match|RestMatch]=Rest, Bindings) when is_list(Rest) ->
+ {ok, Rest, [{?MATCH_ALL, Match}|Bindings]};
bind_path(_, [], _) ->
fail;
bind_path([{bind, Token}|RestToken],[Match|RestMatch],Bindings) ->
@@ -402,15 +405,19 @@ path_to_list([P|R], Acc, DotDotCount) ->
end,
path_to_list(R, [P1|Acc], DotDotCount).
-encode_query(Props) ->
+encode_query(Props) ->
Props1 = lists:foldl(fun ({{bind, K}, V}, Acc) ->
- V1 = case is_list(V) orelse is_binary(V) of
- true -> V;
- false ->
- % probably it's a number
- quote_plus(V)
- end,
- [{K, V1} | Acc]
+ case K of
+ <<"*">> -> Acc;
+ _ ->
+ V1 = case is_list(V) orelse is_binary(V) of
+ true -> V;
+ false ->
+ % probably it's a number
+ quote_plus(V)
+ end,
+ [{K, V1} | Acc]
+ end
end, [], Props),
lists:flatten(mochiweb_util:urlencode(Props1)).