Browse Source

Introduce cowboy_req:host_url/1 to remove more duplicate code

Loïc Hoguin 12 years ago
parent
commit
527477bd4a
2 changed files with 19 additions and 28 deletions
  1. 15 7
      src/cowboy_req.erl
  2. 4 21
      src/cowboy_rest.erl

+ 15 - 7
src/cowboy_req.erl

@@ -35,6 +35,7 @@
 -export([qs_val/3]).
 -export([qs_val/3]).
 -export([qs_vals/1]).
 -export([qs_vals/1]).
 -export([raw_qs/1]).
 -export([raw_qs/1]).
+-export([host_url/1]).
 -export([url/1]).
 -export([url/1]).
 -export([binding/2]).
 -export([binding/2]).
 -export([binding/3]).
 -export([binding/3]).
@@ -199,12 +200,12 @@ qs_vals(Req=#http_req{qs_vals=QsVals}) ->
 raw_qs(Req) ->
 raw_qs(Req) ->
 	{Req#http_req.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(),
 	TransportName = Transport:name(),
 	Secure = case TransportName of
 	Secure = case TransportName of
 		ssl -> <<"s">>;
 		ssl -> <<"s">>;
@@ -215,12 +216,19 @@ url(Req=#http_req{transport=Transport, host=Host, port=Port,
 		{tcp, 80} -> <<>>;
 		{tcp, 80} -> <<>>;
 		_ -> << ":", (list_to_binary(integer_to_list(Port)))/binary >>
 		_ -> << ":", (list_to_binary(integer_to_list(Port)))/binary >>
 	end,
 	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
 	QS2 = case QS of
 		<<>> -> <<>>;
 		<<>> -> <<>>;
 		_ -> << "?", QS/binary >>
 		_ -> << "?", QS/binary >>
 	end,
 	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)
 %% @equiv binding(Name, Req, undefined)
 -spec binding(atom(), Req) -> {binary() | undefined, Req} when Req::req().
 -spec binding(atom(), Req) -> {binary() | undefined, Req} when Req::req().

+ 4 - 21
src/cowboy_rest.erl

@@ -671,31 +671,14 @@ create_path(Req, State) ->
 		{halt, Req2, HandlerState} ->
 		{halt, Req2, HandlerState} ->
 			terminate(Req2, State#state{handler_state=HandlerState});
 			terminate(Req2, State#state{handler_state=HandlerState});
 		{Path, Req2, HandlerState} ->
 		{Path, Req2, HandlerState} ->
-			Location = create_path_location(Req2, Path),
+			{HostURL, Req3} = cowboy_req:host_url(Req2),
 			State2 = State#state{handler_state=HandlerState},
 			State2 = State#state{handler_state=HandlerState},
-			{ok, Req3} = cowboy_req:set_resp_header(
-				<<"Location">>, Location, Req2),
-			put_resource(cowboy_req:set_meta(put_path, Path, Req3),
+			{ok, Req4} = cowboy_req:set_resp_header(
+				<<"Location">>, << HostURL/binary, Path/binary >>, Req3),
+			put_resource(cowboy_req:set_meta(put_path, Path, Req4),
 				State2, 303)
 				State2, 303)
 	end.
 	end.
 
 
-create_path_location(#http_req{transport=Transport, host=Host,
-		port=Port}, Path) ->
-	TransportName = Transport:name(),
-	<< (create_path_location_protocol(TransportName))/binary, "://",
-		Host/binary, (create_path_location_port(TransportName, Port))/binary,
-		Path/binary >>.
-
-create_path_location_protocol(ssl) -> <<"https">>;
-create_path_location_protocol(_) -> <<"http">>.
-
-create_path_location_port(ssl, 443) ->
-	<<>>;
-create_path_location_port(tcp, 80) ->
-	<<>>;
-create_path_location_port(_, Port) ->
-	<<":", (list_to_binary(integer_to_list(Port)))/binary>>.
-
 %% process_post should return true when the POST body could be processed
 %% process_post should return true when the POST body could be processed
 %% and false when it hasn't, in which case a 500 error is sent.
 %% and false when it hasn't, in which case a 500 error is sent.
 process_post(Req, State) ->
 process_post(Req, State) ->