3QgNpijQDRsecOI37FmGLl changeset

Changeset306437326561 (b)
ParentNone (a)
ab
0+%% @author author <author@example.com>
0+%% @copyright YYYY author.
0+
0+%% @doc Web server for myapp.
0+
0+-module(myapp_web).
0+-author('author <author@example.com>').
0+
0+-export([start/1, stop/0, loop/2]).
0+
0+%% External API
0+
0+start(Options) ->
0+    {DocRoot, Options1} = get_option(docroot, Options),
0+    Loop = fun (Req) ->
0+                   ?MODULE:loop(Req, DocRoot)
0+           end,
0+    mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
0+
0+stop() ->
0+    mochiweb_http:stop(?MODULE).
0+
0+loop(Req, DocRoot) ->
0+    "/" ++ Path = Req:get(path),
0+    case Req:get(method) of
0+        Method when Method =:= 'GET'; Method =:= 'HEAD' ->
0+            case Path of
0+                "captcha" ->
0+                        {CodeHex, BinPng} =  mycaptcha:new(),
0+                                               
0+                        Cookie = mochiweb_cookies:cookie("cap", CodeHex, []),
0+
0+                        Req:ok({"image/png", [Cookie], [BinPng]});
0+
0+                _ ->
0+                    Req:serve_file(Path, DocRoot)
0+            end;
0+        'POST' ->
0+            case Path of
0+                "captcha" ->
0+                        CodeHex = Req:get_cookie_value("cap"),
0+                        DataIn = Req:parse_post(),
0+                        CapCode = proplists:get_value("capCode", DataIn),
0+                                                               
0+                        case mycaptcha:check(CodeHex, CapCode) of
0+                                true ->
0+                                        %% a cool one
0+                                        Req:ok({"text/html", [], ["<h1>Cool!</h1>"]});
0+                                false ->
0+                                        %% redirection
0+                                        Req:respond({302, [{"Location", "/"}, {"Content-Type", "text/html; charset=UTF-8"}],""})
0+                        end;
0+
0+
0+                _ ->
0+                    Req:not_found()
0+            end;
0+        _ ->
0+            Req:respond({501, [], []})
0+    end.
0+
0+%% Internal API
0+
0+get_option(Option, Options) ->
0+    {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
--- Revision None
+++ Revision 306437326561
@@ -0,0 +1,65 @@
+%% @author author <author@example.com>
+%% @copyright YYYY author.
+
+%% @doc Web server for myapp.
+
+-module(myapp_web).
+-author('author <author@example.com>').
+
+-export([start/1, stop/0, loop/2]).
+
+%% 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]).
+
+stop() ->
+ 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
+ "captcha" ->
+ {CodeHex, BinPng} = mycaptcha:new(),
+
+ Cookie = mochiweb_cookies:cookie("cap", CodeHex, []),
+
+ Req:ok({"image/png", [Cookie], [BinPng]});
+
+ _ ->
+ Req:serve_file(Path, DocRoot)
+ end;
+ 'POST' ->
+ case Path of
+ "captcha" ->
+ CodeHex = Req:get_cookie_value("cap"),
+ DataIn = Req:parse_post(),
+ CapCode = proplists:get_value("capCode", DataIn),
+
+ case mycaptcha:check(CodeHex, CapCode) of
+ true ->
+ %% a cool one
+ Req:ok({"text/html", [], ["<h1>Cool!</h1>"]});
+ false ->
+ %% redirection
+ Req:respond({302, [{"Location", "/"}, {"Content-Type", "text/html; charset=UTF-8"}],""})
+ end;
+
+
+ _ ->
+ Req:not_found()
+ end;
+ _ ->
+ Req:respond({501, [], []})
+ end.
+
+%% Internal API
+
+get_option(Option, Options) ->
+ {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.