aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy_req.erl
diff options
context:
space:
mode:
authorTom Burdick <[email protected]>2012-11-27 13:22:38 -0600
committerTom Burdick <[email protected]>2012-11-27 13:22:38 -0600
commitb63502e32e52e4f210b79b90a530495360abb8b3 (patch)
treefab2f6b51356d855da8ef7657c4a9bf66ddb52d2 /src/cowboy_req.erl
parent5e6aab756a8b5ff564f071d83b187413dd19535c (diff)
downloadcowboy-b63502e32e52e4f210b79b90a530495360abb8b3.tar.gz
cowboy-b63502e32e52e4f210b79b90a530495360abb8b3.tar.bz2
cowboy-b63502e32e52e4f210b79b90a530495360abb8b3.zip
url and host_url may return undefined
this can happen when the request parsing fails and onresponse needs the url, its perfectly possible that the url has not yet been defined
Diffstat (limited to 'src/cowboy_req.erl')
-rw-r--r--src/cowboy_req.erl18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/cowboy_req.erl b/src/cowboy_req.erl
index 67d2a0f..33aaa33 100644
--- a/src/cowboy_req.erl
+++ b/src/cowboy_req.erl
@@ -313,7 +313,9 @@ fragment(Req) ->
%%
%% The URL includes the scheme, host and port only.
%% @see cowboy_req:url/1
--spec host_url(Req) -> {binary(), Req} when Req::req().
+-spec host_url(Req) -> {undefined | binary(), Req} when Req::req().
+host_url(Req=#http_req{port=undefined}) ->
+ {undefined, Req};
host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
TransportName = Transport:name(),
Secure = case TransportName of
@@ -330,9 +332,14 @@ host_url(Req=#http_req{transport=Transport, host=Host, port=Port}) ->
%% @doc Return the full request URL as a binary.
%%
%% The URL includes the scheme, host, port, path, query string and fragment.
--spec url(Req) -> {binary(), Req} when Req::req().
-url(Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
+-spec url(Req) -> {undefined | binary(), Req} when Req::req().
+url(Req=#http_req{}) ->
{HostURL, Req2} = host_url(Req),
+ url2(HostURL, Req2).
+
+url2(undefined, Req=#http_req{}) ->
+ {undefined, Req};
+url2(HostURL, Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
QS2 = case QS of
<<>> -> <<>>;
_ -> << "?", QS/binary >>
@@ -341,7 +348,7 @@ url(Req=#http_req{path=Path, qs=QS, fragment=Fragment}) ->
<<>> -> <<>>;
_ -> << "#", Fragment/binary >>
end,
- {<< HostURL/binary, Path/binary, QS2/binary, Fragment2/binary >>, Req2}.
+ {<< HostURL/binary, Path/binary, QS2/binary, Fragment2/binary >>, Req}.
%% @equiv binding(Name, Req, undefined)
-spec binding(atom(), Req) -> {binary() | undefined, Req} when Req::req().
@@ -1279,6 +1286,9 @@ status(B) when is_binary(B) -> B.
-ifdef(TEST).
url_test() ->
+ {undefined, _} =
+ url(#http_req{transport=ranch_tcp, host= <<>>, port= undefined,
+ path= <<>>, qs= <<>>, fragment= <<>>, pid=self()}),
{<<"http://localhost/path">>, _ } =
url(#http_req{transport=ranch_tcp, host= <<"localhost">>, port=80,
path= <<"/path">>, qs= <<>>, fragment= <<>>, pid=self()}),