Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'bwf-remove-resource-with-host' into bz1293-remove-resou…

…rce-with-predicate
  • Loading branch information...
commit 766ed3218a2a021f3a91d4519369235352360453 2 parents 1c814dc + 382a645
@beerriot beerriot authored
Showing with 32 additions and 18 deletions.
  1. +32 −18 src/webmachine_router.erl
View
50 src/webmachine_router.erl
@@ -95,8 +95,7 @@ handle_call(get_routes, _From, State) ->
{reply, get_dispatch_list(), State};
handle_call({remove_resource, Resource}, _From, State) ->
- DL = [D || D <- get_dispatch_list(),
- filter_by_resource(D, Resource)],
+ DL = filter_by_resource(Resource, get_dispatch_list()),
{reply, set_dispatch_list(DL), State};
handle_call({remove_route, Route}, _From, State) ->
@@ -129,22 +128,21 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% Internal functions
-filter_by_resource({_, {_, Resource, _}}, Resource) ->
- false;
-filter_by_resource({_, {_, _, _}}, _Resource) ->
- true;
-filter_by_resource({_, Resource, _}, Resource) ->
- false;
-filter_by_resource({_, _, _}, _Resource) ->
- true;
-filter_by_resource({_,_,Resource,_}, Resource) ->
- false;
-filter_by_resource({_,_,_,_}, _Resource) ->
- true;
-filter_by_resource({_,{_,_,Resource,_}}, Resource) ->
- false;
-filter_by_resource({_,{_,_,_,_}}, _Resource) ->
- true.
+
+%% @doc Remove any dispatch rule that directs requests to `Resource'
+filter_by_resource(Resource, Dispatch) ->
+ lists:foldr(filter_by_resource(Resource), [], Dispatch).
+
+filter_by_resource(Resource) ->
+ fun({_, R, _}, Acc) when R == Resource -> % basic dispatch
+ Acc;
+ ({_, _, R, _}, Acc) when R == Resource -> % guarded dispatch
+ Acc;
+ ({Host, Disp}, Acc) -> % host-based dispatch
+ [{Host, filter_by_resource(Resource, Disp)}|Acc];
+ (Other, Acc) -> % dispatch not mentioning this resource
+ [Other|Acc]
+ end.
get_dispatch_list() ->
case application:get_env(webmachine, dispatch_list) of
@@ -188,11 +186,27 @@ add_remove_resource_test() ->
PathSpec1 = {["foo"], foo, []},
PathSpec2 = {["bar"], foo, []},
PathSpec3 = {["baz"], bar, []},
+ PathSpec4 = {["foo"], fun(_) -> true end, foo, []},
+ PathSpec5 = {["foo"], {webmachine_router, test_guard}, foo, []},
webmachine_router:add_route(PathSpec1),
webmachine_router:add_route(PathSpec2),
webmachine_router:add_route(PathSpec3),
webmachine_router:remove_resource(foo),
[PathSpec3] = get_routes(),
+ webmachine_router:add_route(PathSpec4),
+ webmachine_router:remove_resource(foo),
+ [PathSpec3] = get_routes(),
+ webmachine_router:add_route(PathSpec5),
+ webmachine_router:remove_resource(foo),
+ [PathSpec3] = get_routes(),
+ webmachine_router:remove_route(PathSpec3),
+ [begin
+ PathSpec = {"localhost", [HostPath]},
+ webmachine_router:add_route(PathSpec),
+ webmachine_router:remove_resource(foo),
+ [{"localhost", []}] = get_routes(),
+ webmachine_router:remove_route({"localhost", []})
+ end || HostPath <- [PathSpec1, PathSpec4, PathSpec5]],
exit(Pid, kill).
no_dupe_path_test() ->
Please sign in to comment.
Something went wrong with that request. Please try again.