aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_dispatcher.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2011-05-04 12:05:57 +0200
committerLoïc Hoguin <[email protected]>2011-05-04 12:52:13 +0200
commit6c1f73c53c9260d99f71676b400a27f0a853f584 (patch)
tree5d3ca05904b9f65647a24e8d17e6856583fb8fbc /src/cowboy_dispatcher.erl
parentcc663df5db916b4f4da532d765fc2d6b5b60933c (diff)
downloadcowboy-6c1f73c53c9260d99f71676b400a27f0a853f584.tar.gz
cowboy-6c1f73c53c9260d99f71676b400a27f0a853f584.tar.bz2
cowboy-6c1f73c53c9260d99f71676b400a27f0a853f584.zip
Add cowboy_http_req:port/1.
Returns the port given in the Host header if present, otherwise the default port of 443 for HTTPS and 80 for HTTP is returned.
Diffstat (limited to 'src/cowboy_dispatcher.erl')
-rw-r--r--src/cowboy_dispatcher.erl52
1 files changed, 37 insertions, 15 deletions
diff --git a/src/cowboy_dispatcher.erl b/src/cowboy_dispatcher.erl
index 05faa38..4769da0 100644
--- a/src/cowboy_dispatcher.erl
+++ b/src/cowboy_dispatcher.erl
@@ -24,17 +24,20 @@
-export_type([bindings/0, path_tokens/0, dispatch_rules/0]).
+-include_lib("kernel/include/inet.hrl").
-include_lib("eunit/include/eunit.hrl").
%% API.
--spec split_host(Host::string()) -> Tokens::path_tokens().
+-spec split_host(Host::string())
+ -> {Tokens::path_tokens(), Host::string(), Port::undefined | ip_port()}.
split_host(Host) ->
- Host2 = case string:chr(Host, $:) of
- 0 -> Host;
- N -> lists:sublist(Host, N - 1)
- end,
- string:tokens(Host2, ".").
+ case string:chr(Host, $:) of
+ 0 -> {string:tokens(Host, "."), Host, undefined};
+ N ->
+ {Host2, [$:|Port]} = lists:split(N - 1, Host),
+ {string:tokens(Host2, "."), Host2, list_to_integer(Port)}
+ end.
-spec split_path(Path::string())
-> {Tokens::path_tokens(), Path::string(), Qs::string()}.
@@ -119,19 +122,38 @@ list_match([], [], Binds) ->
split_host_test_() ->
%% {Host, Result}
Tests = [
- {"", []},
- {".........", []},
- {"*", ["*"]},
- {"cowboy.dev-extend.eu", ["cowboy", "dev-extend", "eu"]},
- {"dev-extend..eu", ["dev-extend", "eu"]},
- {"dev-extend.eu", ["dev-extend", "eu"]},
- {"dev-extend.eu:8080", ["dev-extend", "eu"]},
+ {"", {[], "", undefined}},
+ {".........", {[], ".........", undefined}},
+ {"*", {["*"], "*", undefined}},
+ {"cowboy.dev-extend.eu", {["cowboy", "dev-extend", "eu"],
+ "cowboy.dev-extend.eu", undefined}},
+ {"dev-extend..eu",
+ {["dev-extend", "eu"], "dev-extend..eu", undefined}},
+ {"dev-extend.eu", {["dev-extend", "eu"], "dev-extend.eu", undefined}},
+ {"dev-extend.eu:8080", {["dev-extend", "eu"], "dev-extend.eu", 8080}},
{"a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z",
- ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
- "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]}
+ {["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
+ "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"],
+ "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z", undefined}}
],
[{H, fun() -> R = split_host(H) end} || {H, R} <- Tests].
+split_host_fail_test_() ->
+ Tests = [
+ "dev-extend.eu:owns",
+ "dev-extend.eu: owns",
+ "dev-extend.eu:42fun",
+ "dev-extend.eu: 42fun",
+ "dev-extend.eu:42 fun",
+ "dev-extend.eu:fun 42",
+ "dev-extend.eu: 42",
+ ":owns",
+ ":42 fun"
+ ],
+ [{H, fun() -> case catch split_host(H) of
+ {'EXIT', _Reason} -> ok
+ end end} || H <- Tests].
+
split_path_test_() ->
%% {Path, Result, QueryString}
Tests = [