PageRenderTime 313ms CodeModel.GetById 297ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/src/ewgi_testapp.erl

http://github.com/skarab/ewgi
Erlang | 90 lines | 58 code | 7 blank | 25 comment | 1 complexity | 8214b1106472d38109c5eea30b66fcad MD5 | raw file
 1%%%-------------------------------------------------------------------
 2%%% File    : ewgi_testapp.erl
 3%%% Authors : Hunter Morris <huntermorris@gmail.com>
 4%%% License :
 5%%% The contents of this file are subject to the Mozilla Public
 6%%% License Version 1.1 (the "License"); you may not use this file
 7%%% except in compliance with the License. You may obtain a copy of
 8%%% the License at http://www.mozilla.org/MPL/
 9%%%
10%%% Software distributed under the License is distributed on an "AS IS"
11%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12%%% the License for the specific language governing rights and
13%%% limitations under the License.
14%%% The Initial Developer of the Original Code is S.G. Consulting
15%%% srl. Portions created by S.G. Consulting s.r.l. are Copyright (C)
16%%% 2007 S.G. Consulting srl. All Rights Reserved.
17%%%
18%%% @doc 
19%%% <p>ewgi test applications</p>
20%%%
21%%% @end
22%%%
23%%% Created : 05 July 2009 by Hunter Morris <huntermorris@gmail.com>
24%%%-------------------------------------------------------------------
25-module(ewgi_testapp).
26
27%% Test EWGI applications
28-export([testapp/1, testapp_chunked/1]).
29
30-include_lib("ewgi.hrl").
31
32testapp(C) ->
33    Body = io_lib:format("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\"><html><body><h1>EWGI Context</h1><h2>Request</h2>~s</body></html>", [htmlise(C)]),
34    Stat = {200, "OK"},
35    H = [{"content-type", "text/html"}],
36    ewgi_api:response_message_body(
37      Body, ewgi_api:response_headers(
38              H, ewgi_api:response_status(Stat, C))).
39
40testapp_chunked(C0) ->
41    C = testapp(C0),
42    B = ewgi_api:response_message_body(C),
43    ewgi_api:response_message_body(list_to_stream(B), C).
44
45htmlise(C) ->
46    iolist_to_binary(
47      ["<dl class=\"request\">",
48       io_lib:format("<dt>url_scheme</dt><dd><pre>~s</pre></dd>", [ewgi_api:url_scheme(C)]),
49       io_lib:format("<dt>request_method</dt><dd><pre>~s</pre></dd>", [ewgi_api:request_method(C)]),
50       io_lib:format("<dt>path_info</dt><dd><pre>~s</pre></dd>", [ewgi_api:path_info(C)]),
51       io_lib:format("<dt>query_string</dt><dd><pre>~s</pre></dd>", [ewgi_api:query_string(C)]),
52       io_lib:format("<dt>remote_addr</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_addr(C)]),
53       io_lib:format("<dt>auth_type</dt><dd><pre>~s</pre></dd>", [ewgi_api:auth_type(C)]),
54       io_lib:format("<dt>content_length</dt><dd><pre>~p</pre></dd>", [ewgi_api:content_length(C)]),
55       io_lib:format("<dt>content_type</dt><dd><pre>~s</pre></dd>", [ewgi_api:content_type(C)]),
56       io_lib:format("<dt>gateway_interface</dt><dd><pre>~s</pre></dd>", [ewgi_api:gateway_interface(C)]),
57       io_lib:format("<dt>path_translated</dt><dd><pre>~s</pre></dd>", [ewgi_api:path_translated(C)]),
58       io_lib:format("<dt>remote_host</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_host(C)]),
59       io_lib:format("<dt>remote_ident</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_ident(C)]),
60       io_lib:format("<dt>remote_user</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_user(C)]),
61       io_lib:format("<dt>remote_user_data</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_user_data(C)]),
62       io_lib:format("<dt>script_name</dt><dd><pre>~s</pre></dd>", [ewgi_api:script_name(C)]),
63       io_lib:format("<dt>server_name</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_name(C)]),
64       io_lib:format("<dt>server_port</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_port(C)]),
65       io_lib:format("<dt>other http headers</dt><dd>~s</dd>", [htmlise_data("http_headers", ewgi_api:get_all_headers(C))]),
66       io_lib:format("<dt>server_protocol</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_protocol(C)]),
67       io_lib:format("<dt>server_software</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_software(C)]),
68       io_lib:format("<dt>ewgi version</dt><dd><pre>~p</pre></dd>", [ewgi_api:version(C)]),
69       io_lib:format("<dt>ewgi extra data</dt><dd>~s</dd>", [htmlise_data("request_data", ewgi_api:get_all_data(C))]),
70       "</dl>"]).
71
72htmlise_data(Name, L) when is_list(L) ->
73    ["<dl class=\"", Name, "\">",
74     [io_lib:format("<dt>~s</dt><dd><pre>~p</pre><dd>", [K, V]) || {K, V} <- L],
75     "</dl>"];
76htmlise_data(Name, T) ->
77    case gb_trees:to_list(T) of
78        [] -> [];
79        L -> htmlise_data(Name, L)
80    end.
81
82list_to_stream(L) when is_list(L) ->
83    fun() ->
84            case L of
85                [H|T] ->
86                    {H, list_to_stream(T)};
87                [] ->
88                    {}
89            end
90    end.