]> review.fuel-infra Code Review - packages/trusty/rabbitmq-server.git/blob
797ab78b11d0d0f99656ee3cf30bf2024808d0e8
[packages/trusty/rabbitmq-server.git] /
1 %% @author Justin Sheehy <justin@basho.com>
2 %% @copyright 2007-2009 Basho Technologies, Inc.  All Rights Reserved.
3 %% @doc Example webmachine_resource.
4
5 -module(webmachine_demo_resource).
6 -author('Justin Sheehy <justin@basho.com>').
7 -export([init/1, to_html/2, to_text/2, content_types_provided/2,
8          is_authorized/2, generate_etag/2, expires/2, last_modified/2]).
9
10 -include_lib("webmachine/include/webmachine.hrl").
11
12 init([]) -> {ok, undefined}.
13     
14 content_types_provided(ReqData, Context) ->
15     {[{"text/html", to_html},{"text/plain",to_text}], ReqData, Context}.
16
17 to_text(ReqData, Context) ->
18     Path = wrq:disp_path(ReqData),
19     Body = io_lib:format("Hello ~s from webmachine.~n", [Path]),
20     {Body, ReqData, Context}.
21
22 to_html(ReqData, Context) ->
23     {Body, _RD, Ctx2} = to_text(ReqData, Context),
24     HBody = io_lib:format("<html><body>~s</body></html>~n",
25                           [erlang:iolist_to_binary(Body)]),
26     {HBody, ReqData, Ctx2}.
27
28 is_authorized(ReqData, Context) ->
29     case wrq:disp_path(ReqData) of
30         "authdemo" -> 
31             case wrq:get_req_header("authorization", ReqData) of
32                 "Basic "++Base64 ->
33                     Str = base64:mime_decode_to_string(Base64),
34                     case string:tokens(Str, ":") of
35                         ["authdemo", "demo1"] ->
36                             {true, ReqData, Context};
37                         _ ->
38                             {"Basic realm=webmachine", ReqData, Context}
39                     end;
40                 _ ->
41                     {"Basic realm=webmachine", ReqData, Context}
42             end;
43         _ -> {true, ReqData, Context}
44     end.
45
46 expires(ReqData, Context) -> {{{2021,1,1},{0,0,0}}, ReqData, Context}.
47
48 last_modified(ReqData, Context) ->
49     {calendar:now_to_universal_time(os:timestamp()), ReqData, Context}.
50
51 generate_etag(ReqData, Context) -> {wrq:raw_path(ReqData), ReqData, Context}.