public
Description: A new revision of Fuzed, the Erlang-based frontend for web apps. Check out the mailing list at http://groups.google.com/group/fuzed
Clone URL: git://github.com/KirinDave/fuzed.git
mojombo (author)
Sun May 04 20:33:26 -0700 2008
commit  84159d727f2ae308bcb1518c66420e17e790bec7
tree    9f0a478b8f0ce7e87d821d0bd2c6c76ab993ebf1
parent  aac45964081d8d0ec8a423f1f536c722284ca91f
fuzed / elibs / frontend_responder.erl
100644 95 lines (83 sloc) 3.8 kb
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-module(frontend_responder).
-include("yaws_api.hrl").
-include("yaws.hrl").
-include("fuzed.hrl").
-compile(export_all).
 
out404(A, _GC, SC) ->
  Parameters = {struct, parse_arg(A, SC)},
  io:format("Param restructure:~n~p~n", [Parameters]),
  case node_api:safely_send_call_to_pool(handle_request,
                                         Parameters,
                                         {handle_request, {request}},
                                         pure,
                                         details()) of
    {result, R} ->
      convert_response(R);
    {error, R} ->
      error_logger:info_msg("500 Internal Server Error: ~p~n", [R]),
      [{status, 500}, {html, "Sumpin fucked."}]
  end.
 
parse_arg(Request, ServerOptions) ->
  Headers = Request#arg.headers,
  {convert_method(Request),
   convert_version(Request),
   convert_querypath(Request),
   {querydata, prep(Request#arg.querydata)},
   {servername, prep(ServerOptions#sconf.servername)},
   {headers, {struct, convert_headers(Request#arg.headers)}},
   {cookies, {array, list_to_tuple(lists:map(fun(X) -> prep(X) end, Headers#headers.cookie))}},
   {pathinfo, prep(ServerOptions#sconf.docroot)},
   {postdata, Request#arg.clidata}}.
    
 
convert_method(Request) ->
  R = Request#arg.req,
  {http_request,Method,{_Type,_Path},_} = R,
  {method, Method}.
 
convert_querypath(Request) ->
   R = Request#arg.req,
   {http_request,_Method,{_Type,Path},_} = R,
   {querypath, prep(Path)}.
 
convert_version(Request) ->
  R = Request#arg.req,
  {http_request,_Method,{_Type,_Path},Version} = R,
  {http_version, Version}.
 
convert_req(R) ->
  {http_request,Method,{_Type,Path},_} = R,
  {Method, prep(Path)}.
 
convert_headers(A) ->
  NormalHeaders = [{connection, prep(A#headers.connection)},
                   {accept, prep(A#headers.accept)},
                   {host, prep(A#headers.host)},
                   {if_modified_since, prep(A#headers.if_modified_since)},
                   {if_match, prep(A#headers.if_match)},
                   {if_none_match, prep(A#headers.if_none_match)},
                   {if_range, prep(A#headers.if_range)},
                   {if_unmodified_since, prep(A#headers.if_unmodified_since)},
                   {range, prep(A#headers.range)},
                   {referer, prep(A#headers.referer)},
                   {user_agent, prep(A#headers.user_agent)},
                   {accept_ranges, prep(A#headers.accept_ranges)},
                   {keep_alive, prep(A#headers.keep_alive)},
                   {location, prep(A#headers.location)},
                   {content_length, prep(A#headers.content_length)},
                   {content_type, prep(A#headers.content_type)},
                   {content_encoding, prep(A#headers.content_encoding)},
                   {authorization, prep(A#headers.authorization)},
                   {transfer_encoding, prep(A#headers.transfer_encoding)}],
  SpecialHeaders =
    lists:map(fun({http_header, _Len, Name, _, Value}) -> {prep(Name), prep(Value)} end,
              A#headers.other),
  list_to_tuple([{Name, Res} || {Name, Res} <- NormalHeaders, Res /= undefined] ++ SpecialHeaders).
 
convert_response(EhtmlTuple) ->
  {Status, AllHeaders, Html} = EhtmlTuple,
  {allheaders, HeaderList} = AllHeaders,
  ProcessedHeaderList = lists:map(fun({header, Name, Value}) -> {header, [binary_to_list(Name) ++ ":", binary_to_list(Value)]} end,
                                  tuple_to_list(HeaderList)),
  {html, RawResult} = Html,
  [Status, {allheaders, ProcessedHeaderList}, {html, binary_to_list(RawResult)}].
 
prep(A) when is_list(A) -> list_to_binary(A);
prep(A) -> A.
 
details() ->
  {ok, Details} =
    application:get_env(fuzed_frontend, details),
  error_logger:info_msg("Using frontend details ~p~n", [Details]),
  Details.