1 %% @author Justin Sheehy <justin@basho.com>
2 %% @copyright 2007-2009 Basho Technologies, Inc. All Rights Reserved.
3 %% @doc Example webmachine_resource.
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]).
10 -include_lib("webmachine/include/webmachine.hrl").
12 init([]) -> {ok, undefined}.
14 content_types_provided(ReqData, Context) ->
15 {[{"text/html", to_html},{"text/plain",to_text}], ReqData, Context}.
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}.
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}.
28 is_authorized(ReqData, Context) ->
29 case wrq:disp_path(ReqData) of
31 case wrq:get_req_header("authorization", ReqData) of
33 Str = base64:mime_decode_to_string(Base64),
34 case string:tokens(Str, ":") of
35 ["authdemo", "demo1"] ->
36 {true, ReqData, Context};
38 {"Basic realm=webmachine", ReqData, Context}
41 {"Basic realm=webmachine", ReqData, Context}
43 _ -> {true, ReqData, Context}
46 expires(ReqData, Context) -> {{{2021,1,1},{0,0,0}}, ReqData, Context}.
48 last_modified(ReqData, Context) ->
49 {calendar:now_to_universal_time(os:timestamp()), ReqData, Context}.
51 generate_etag(ReqData, Context) -> {wrq:raw_path(ReqData), ReqData, Context}.