aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_req.erl
diff options
context:
space:
mode:
authorLoïc Hoguin <[email protected]>2012-09-16 01:13:44 +0200
committerLoïc Hoguin <[email protected]>2012-09-16 01:13:44 +0200
commit527477bd4ac431f3810c3cec6679824112cf3612 (patch)
tree9384b8f8a5826cd4403b348851908833d667f32d /src/cowboy_req.erl
parent736bcd4841c38e8676017486aea211e624d3dc0f (diff)
downloadcowboy-527477bd4ac431f3810c3cec6679824112cf3612.tar.gz
cowboy-527477bd4ac431f3810c3cec6679824112cf3612.tar.bz2
cowboy-527477bd4ac431f3810c3cec6679824112cf3612.zip
Introduce cowboy_req:host_url/1 to remove more duplicate code
Diffstat (limited to 'src/cowboy_req.erl')
-rw-r--r--src/cowboy_req.erl22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 2625c58..699c392 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -35,6 +35,7 @@
-export([qs_val/3]).
-export([qs_vals/1]).
-export([raw_qs/1]).
+-export([host_url/1]).
-export([url/1]).
-export([binding/2]).
-export([binding/3]).
@@ -199,12 +200,12 @@ qs_vals(Req=#http_req{qs_vals=QsVals}) ->
raw_qs(Req) ->
{Req#http_req.raw_qs, Req}.
-%% @doc Return the full request URL as a binary.
+%% @doc Return the request URL as a binary without the path and query string.
%%
-%% The URL includes the scheme, host, port, path and query string.
--spec url(Req) -> {binary(), Req} when Req::req().
-url(Req=#http_req{transport=Transport, host=Host, port=Port,
- path=Path, raw_qs=QS}) ->
+%% The URL includes the scheme, host and port only.
+%% @see cowboy_req:url/1
+-spec host_url(Req) -> {binary(), Req} when Req::req().
+host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
TransportName = Transport:name(),
Secure = case TransportName of
ssl -> <<"s">>;
@@ -215,12 +216,19 @@ url(Req=#http_req{transport=Transport, host=Host, port=Port,
{tcp, 80} -> <<>>;
_ -> << ":", (list_to_binary(integer_to_list(Port)))/binary >>
end,
+ {<< "http", Secure/binary, "://", Host/binary, PortBin/binary >>, Req}.
+
+%% @doc Return the full request URL as a binary.
+%%
+%% The URL includes the scheme, host, port, path and query string.
+-spec url(Req) -> {binary(), Req} when Req::req().
+url(Req=#http_req{path=Path, raw_qs=QS}) ->
+ {HostURL, Req2} = host_url(Req),
QS2 = case QS of
<<>> -> <<>>;
_ -> << "?", QS/binary >>
end,
- {<< "http", Secure/binary, "://", Host/binary, PortBin/binary,
- Path/binary, QS2/binary >>, Req}.
+ {<< HostURL/binary, Path/binary, QS2/binary >>, Req2}.
%% @equiv binding(Name, Req, undefined)
-spec binding(atom(), Req) -> {binary() | undefined, Req} when Req::req().