Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added a shortcut for temporary and permanent redirections

  • Loading branch information...
commit 309df57fbeaac7a954c0facda2e0f529a94bb75f 1 parent 33ab595
@ostinelli authored
View
56 examples/misultin_redirect.erl
@@ -0,0 +1,56 @@
+% ==========================================================================================================
+% MISULTIN - Example: Redirect.
+%
+% >-|-|-(°>
+%
+% Copyright (C) 2011, Roberto Ostinelli <roberto@ostinelli.net>
+% All rights reserved.
+%
+% BSD License
+%
+% Redistribution and use in source and binary forms, with or without modification, are permitted provided
+% that the following conditions are met:
+%
+% * Redistributions of source code must retain the above copyright notice, this list of conditions and the
+% following disclaimer.
+% * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
+% the following disclaimer in the documentation and/or other materials provided with the distribution.
+% * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
+% products derived from this software without specific prior written permission.
+%
+% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+% ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
+% TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+% POSSIBILITY OF SUCH DAMAGE.
+% ==========================================================================================================
+-module(misultin_redirect).
+-export([start/1, stop/0]).
+
+% start misultin http server
+start(Port) ->
+ misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req) end}]).
+
+% stop misultin
+stop() ->
+ misultin:stop().
+
+% callback function called on incoming http request
+handle_http(Req) ->
+ % dispatch to rest
+ handle(Req:get(method), Req:resource([lowercase, urldecode]), Req).
+
+% handle a get on /
+handle('GET', [], Req) ->
+ Req:redirect("/redirected");
+
+% handle a get on /redirected
+handle('GET', ["redirected"], Req) ->
+ Req:ok("You got temporarily redirected here.");
+
+% handle the 404 page not found
+handle(_, _, Req) ->
+ Req:ok([{"Content-Type", "text/plain"}], "Page not found.").
View
10 src/misultin_req.erl
@@ -37,7 +37,7 @@
-define(FILE_READ_BUFFER, 64*1024).
% API
--export([ok/2, ok/3, ok/4, respond/2, respond/3, respond/4, respond/5]).
+-export([ok/2, ok/3, ok/4, respond/2, respond/3, respond/4, respond/5, redirect/2, redirect/3]).
-export([options/2]).
-export([chunk/2, chunk/3, stream/2, stream/3, stream/4]).
-export([raw/1, get/2]).
@@ -189,6 +189,14 @@ respond(HttpCode, Headers, Template, {misultin_req, SocketPid, _TableDateRef}) -
respond(HttpCode, Headers, Template, Vars, {misultin_req, SocketPid, _TableDateRef}) when is_list(Template) =:= true ->
SocketPid ! {response, HttpCode, Headers, io_lib:format(Template, Vars)}.
+% shortcut for redirections
+-spec redirect(Url::string(), reqt()) -> term().
+-spec redirect(permanent, Url::string(), reqt()) -> term().
+redirect(Url, ReqT) ->
+ respond(302, [{'Location', Url}], "", ReqT).
+redirect(permanent, Url, ReqT) ->
+ respond(301, [{'Location', Url}], "", ReqT).
+
% Chunked Transfer-Encoding.
-spec chunk
(head | done, reqt()) -> term();
View
2  src/misultin_utility.erl
@@ -419,7 +419,7 @@ get_key_value(Key, List) ->
end.
% Find atom Tag in Headers, Headers being both atoms [for known headers] and strings. Comparison on string Header Tags is case insensitive.
--spec header_get_value(Tag::atom(), Headers::http_headers()) -> false | string() | false.
+-spec header_get_value(Tag::atom(), Headers::http_headers()) -> string() | false.
header_get_value(Tag, Headers) when is_atom(Tag) ->
case lists:keyfind(Tag, 1, Headers) of
false ->
Please sign in to comment.
Something went wrong with that request. Please try again.