Skip to content

Commit

Permalink
Merge branch 'bwf-pr46-rebase'
Browse files Browse the repository at this point in the history
  • Loading branch information
beerriot committed Aug 22, 2012
2 parents f28218e + 247e1ef commit 216d3e0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 33 deletions.
27 changes: 18 additions & 9 deletions src/webmachine_dispatcher.erl
Expand Up @@ -47,23 +47,26 @@ dispatch(HostAsString, PathAsString, DispatchList, RD) ->
true -> 1;
_ -> 0
end,
{Host, Port} = split_host_port(HostAsString),
{Host, Port} = split_host_port(HostAsString, wrq:scheme(RD)),
try_host_binding(DispatchList, Host, Port, Path, ExtraDepth, RD).

split_host_port(HostAsString) ->
split_host_port(HostAsString, Scheme) ->
case string:tokens(HostAsString, ":") of
[HostPart, PortPart] ->
{split_host(HostPart), list_to_integer(PortPart)};
[HostPart] ->
{split_host(HostPart), 80};
{split_host(HostPart), default_port(Scheme)};
[] ->
%% no host header
{[], 80}
{[], default_port(Scheme)}
end.

split_host(HostAsString) ->
string:tokens(HostAsString, ".").

default_port(http) -> 80;
default_port(https) -> 443.

%% @type matchterm() = hostmatchterm() | pathmatchterm().
% The dispatch configuration is a list of these terms, and the
% first one whose host and path terms match the input is used.
Expand Down Expand Up @@ -284,11 +287,17 @@ split_host_test() ->
?assertEqual(["foo","bar","baz"], split_host("foo.bar.baz")).

split_host_port_test() ->
?assertEqual({[], 80}, split_host_port("")),
?assertEqual({[], 80}, split_host_port("", http)),
?assertEqual({["foo","bar","baz"], 80},
split_host_port("foo.bar.baz:80")),
split_host_port("foo.bar.baz:80", http)),
?assertEqual({["foo","bar","baz"], 1234},
split_host_port("foo.bar.baz:1234", http)),

?assertEqual({[], 443}, split_host_port("", https)),
?assertEqual({["foo","bar","baz"], 443},
split_host_port("foo.bar.baz", https)),
?assertEqual({["foo","bar","baz"], 1234},
split_host_port("foo.bar.baz:1234")).
split_host_port("foo.bar.baz:1234", https)).

%% port binding
bind_port_simple_match_test() ->
Expand Down Expand Up @@ -445,7 +454,7 @@ try_host_binding_fullmatch_test() ->
try_host_binding(Dispatch, ["foo","quux","bar"],80,["d"],0, RD)).

try_host_binding_wildcard_token_order_test() ->
RD = testing,
RD = wrq:create('GET', http, {1,1}, "testing", mochiweb_headers:from_list([])),
Dispatch = [{{['*',"quux","com"],80},[{['*'],x,y}]}],
?assertEqual({x,y,["foo","bar","baz"],80,[],[],".",""},
dispatch("foo.bar.baz.quux.com","/",Dispatch,RD)).
Expand All @@ -456,7 +465,7 @@ try_host_binding_fail_test() ->
try_host_binding([], ["bar","foo"], 1234, ["x","y","z"], 0, RD)).

dispatch_test() ->
RD = testing,
RD = wrq:create('GET', http, {1,1}, "testing", mochiweb_headers:from_list([])),
TrueFun = fun(_) -> true end,
FalseFun = fun(_) -> false end,

Expand Down
21 changes: 1 addition & 20 deletions src/webmachine_request.erl
Expand Up @@ -122,11 +122,7 @@ x_peername(Default) ->
end.

call(base_uri) ->
RD = ReqState#wm_reqstate.reqdata,
Scheme = erlang:atom_to_list(RD#wm_reqdata.scheme),
Host = string:join(RD#wm_reqdata.host_tokens, "."),
PortString = port_string(Scheme, RD#wm_reqdata.port),
{Scheme ++ "://" ++ Host ++ PortString,ReqState};
{wrq:base_uri(ReqState#wm_reqstate.reqdata), ReqState};
call(socket) -> {ReqState#wm_reqstate.socket,ReqState};
call(get_reqdata) -> {ReqState#wm_reqstate.reqdata, ReqState};
call({set_reqdata, RD}) -> {ok, ReqState#wm_reqstate{reqdata=RD}};
Expand Down Expand Up @@ -809,18 +805,3 @@ load_dispatch_data(Bindings, HostTokens, Port, PathTokens,
PathTokens, AppRoot, DispPath}).

log_data() -> call(log_data).

port_string(Scheme, Port) ->
case Scheme of
"http" ->
case Port of
80 -> "";
_ -> ":" ++ erlang:integer_to_list(Port)
end;
"https" ->
case Port of
443 -> "";
_ -> ":" ++ erlang:integer_to_list(Port)
end;
_ -> ":" ++ erlang:integer_to_list(Port)
end.
65 changes: 61 additions & 4 deletions src/wrq.erl
Expand Up @@ -20,7 +20,8 @@
-export([method/1,scheme/1,version/1,peer/1,disp_path/1,path/1,raw_path/1,
path_info/1,response_code/1,req_cookie/1,req_qs/1,req_headers/1,
req_body/1,stream_req_body/2,resp_redirect/1,resp_headers/1,
resp_body/1,app_root/1,path_tokens/1, host_tokens/1, port/1]).
resp_body/1,app_root/1,path_tokens/1, host_tokens/1, port/1,
base_uri/1]).
-export([path_info/2,get_req_header/2,do_redirect/2,fresh_resp_headers/2,
get_resp_header/2,set_resp_header/3,set_resp_headers/2,
set_disp_path/2,set_req_body/2,set_resp_body/2,set_response_code/2,
Expand Down Expand Up @@ -225,6 +226,27 @@ add_note(K, V, RD) -> RD#wm_reqdata{notes=[{K, V} | RD#wm_reqdata.notes]}.

get_notes(RD) -> RD#wm_reqdata.notes.

base_uri(RD) ->
Scheme = erlang:atom_to_list(RD#wm_reqdata.scheme),
Host = string:join(RD#wm_reqdata.host_tokens, "."),
PortString = port_string(RD#wm_reqdata.scheme, RD#wm_reqdata.port),
Scheme ++ "://" ++ Host ++ PortString.

port_string(Scheme, Port) ->
case Scheme of
http ->
case Port of
80 -> "";
_ -> ":" ++ erlang:integer_to_list(Port)
end;
https ->
case Port of
443 -> "";
_ -> ":" ++ erlang:integer_to_list(Port)
end;
_ -> ":" ++ erlang:integer_to_list(Port)
end.

%%
%% Tests
%%
Expand All @@ -233,7 +255,10 @@ get_notes(RD) -> RD#wm_reqdata.notes.
-include_lib("eunit/include/eunit.hrl").

make_wrq(Method, RawPath, Headers) ->
create(Method, {1,1}, RawPath, mochiweb_headers:from_list(Headers)).
make_wrq(Method, http, RawPath, Headers).

make_wrq(Method, Scheme, RawPath, Headers) ->
create(Method, Scheme, {1,1}, RawPath, mochiweb_headers:from_list(Headers)).

accessor_test() ->
R0 = make_wrq('GET', "/foo?a=1&b=2", [{"Cookie", "foo=bar"}]),
Expand All @@ -248,7 +273,6 @@ accessor_test() ->
?assertEqual([{"foo", "bar"}], req_cookie(R)),
?assertEqual("bar", get_cookie_value("foo", R)),
?assertEqual("127.0.0.1", peer(R)).


simple_dispatch_test() ->
R0 = make_wrq('GET', "/foo?a=1&b=2", [{"Cookie", "foo=bar"}]),
Expand All @@ -264,6 +288,39 @@ simple_dispatch_test() ->
StringPath,
R1),
?assertEqual(".", app_root(R)),
?assertEqual(80, port(R)).
?assertEqual(80, port(R)),
?assertEqual("http://127.0.0.1", base_uri(R)).

base_uri_test_() ->
Make_req =
fun(Scheme, Host) ->
R0 = make_wrq('GET', Scheme, "/foo?a=1&b=2",
[{"Cookie", "foo=bar"}]),
R1 = set_peer("127.0.0.1", R0),
DispatchRule = {["foo"], foo_resource, []},
{_, _, HostTokens, Port, PathTokens,
Bindings, AppRoot,StringPath} =
webmachine_dispatcher:dispatch(Host, "/foo", [DispatchRule],
R1),
load_dispatch_data(Bindings,
HostTokens,
Port,
PathTokens,
AppRoot,
StringPath,
R1)
end,
Tests = [{{http, "somewhere.com:8080"}, "http://somewhere.com:8080"},
{{https, "somewhere.com:8080"}, "https://somewhere.com:8080"},

{{http, "somewhere.com"}, "http://somewhere.com"},
{{https, "somewhere.com"}, "https://somewhere.com"},

{{http, "somewhere.com:80"}, "http://somewhere.com"},
{{https, "somewhere.com:443"}, "https://somewhere.com"},
{{https, "somewhere.com:80"}, "https://somewhere.com:80"},
{{http, "somewhere.com:443"}, "http://somewhere.com:443"}],
[ ?_assertEqual(Expect, base_uri(Make_req(Scheme, Host)))
|| {{Scheme, Host}, Expect} <- Tests ].

-endif.

0 comments on commit 216d3e0

Please sign in to comment.