Skip to content

Commit

Permalink
some preliminary cookie work
Browse files Browse the repository at this point in the history
  • Loading branch information
labria committed Jan 12, 2010
1 parent ee5a5ac commit 3caf6a1
Showing 1 changed file with 64 additions and 22 deletions.
86 changes: 64 additions & 22 deletions examples/basic/src/basic_web.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,75 @@
%% External API

start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) ->
?MODULE:loop(Req, DocRoot)
end,
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).

stop() ->
mochiweb_http:stop(?MODULE).
mochiweb_http:stop(?MODULE).

loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
case Req:get(method) of
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
case Path of
_ ->
Req:serve_file(Path, DocRoot)
end;
'POST' ->
case Path of
_ ->
Req:not_found()
end;
_ ->
Req:respond({501, [], []})
end.
"/" ++ Path = Req:get(path),
case Req:get(method) of
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
case Path of
"rooms/join/" ++ Room ->
join_room(Req, Room);
"rooms/" ++ Room ->
enter_room(Req, Room);
_ ->
Req:serve_file(Path, DocRoot)
end;
'POST' ->
case Path of
"rooms/join/" ++ Room ->
join_room_post(Req, Room);
_ ->
Req:not_found()
end;
_ ->
Req:respond({501, [], []})
end.

%% Internal API

enter_room(Req, Room) ->
% if no cookie for room => redirect to /rooms/join/Rooom
% else render room
Nick = proplists:get_value(Room, Req:parse_cookie()),
case Nick of
undefined ->
Req:respond({302,
[{"Location", "/rooms/join/" ++ Room},
{"Content-Type", "text/html; charset=UTF-8"}],
""});
_ ->
Req:ok({"text/html", "come in!"})
% Req:ok({"text/html", erltl:render(Room)});
end.

join_room(Req, Room) ->
% check cookie for room
% if found => redirect to room
ok.

join_room_post(Req, Room) ->
% (check nickname?)
Nick = proplists:get_value("nickname", Req:parse_post()),
case Nick of
undefined ->
Req:respond({302, [{"Location", "/rooms/join/" ++ Room},
{"Content-Type", "text/html; charset=UTF-8"}],
""});
_ ->
H = mochiweb_cookies:cookie(Room, Nick, [{path, "/"}]),
Req:respond({302,
[{"Location", "/rooms/" ++ Room},
{"Content-Type", "text/html; charset=UTF-8"}, H],
""})
end.

get_option(Option, Options) ->
{proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
{proplists:get_value(Option, Options), proplists:delete(Option, Options)}.

0 comments on commit 3caf6a1

Please sign in to comment.