PageRenderTime 23ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ewgi_testapp.erl

http://github.com/skarab/ewgi
Erlang | 90 lines | 58 code | 7 blank | 25 comment | 1 complexity | 8214b1106472d38109c5eea30b66fcad MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  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. %% Test EWGI applications
  27. -export([testapp/1, testapp_chunked/1]).
  28. -include_lib("ewgi.hrl").
  29. testapp(C) ->
  30. 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)]),
  31. Stat = {200, "OK"},
  32. H = [{"content-type", "text/html"}],
  33. ewgi_api:response_message_body(
  34. Body, ewgi_api:response_headers(
  35. H, ewgi_api:response_status(Stat, C))).
  36. testapp_chunked(C0) ->
  37. C = testapp(C0),
  38. B = ewgi_api:response_message_body(C),
  39. ewgi_api:response_message_body(list_to_stream(B), C).
  40. htmlise(C) ->
  41. iolist_to_binary(
  42. ["<dl class=\"request\">",
  43. io_lib:format("<dt>url_scheme</dt><dd><pre>~s</pre></dd>", [ewgi_api:url_scheme(C)]),
  44. io_lib:format("<dt>request_method</dt><dd><pre>~s</pre></dd>", [ewgi_api:request_method(C)]),
  45. io_lib:format("<dt>path_info</dt><dd><pre>~s</pre></dd>", [ewgi_api:path_info(C)]),
  46. io_lib:format("<dt>query_string</dt><dd><pre>~s</pre></dd>", [ewgi_api:query_string(C)]),
  47. io_lib:format("<dt>remote_addr</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_addr(C)]),
  48. io_lib:format("<dt>auth_type</dt><dd><pre>~s</pre></dd>", [ewgi_api:auth_type(C)]),
  49. io_lib:format("<dt>content_length</dt><dd><pre>~p</pre></dd>", [ewgi_api:content_length(C)]),
  50. io_lib:format("<dt>content_type</dt><dd><pre>~s</pre></dd>", [ewgi_api:content_type(C)]),
  51. io_lib:format("<dt>gateway_interface</dt><dd><pre>~s</pre></dd>", [ewgi_api:gateway_interface(C)]),
  52. io_lib:format("<dt>path_translated</dt><dd><pre>~s</pre></dd>", [ewgi_api:path_translated(C)]),
  53. io_lib:format("<dt>remote_host</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_host(C)]),
  54. io_lib:format("<dt>remote_ident</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_ident(C)]),
  55. io_lib:format("<dt>remote_user</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_user(C)]),
  56. io_lib:format("<dt>remote_user_data</dt><dd><pre>~s</pre></dd>", [ewgi_api:remote_user_data(C)]),
  57. io_lib:format("<dt>script_name</dt><dd><pre>~s</pre></dd>", [ewgi_api:script_name(C)]),
  58. io_lib:format("<dt>server_name</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_name(C)]),
  59. io_lib:format("<dt>server_port</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_port(C)]),
  60. io_lib:format("<dt>other http headers</dt><dd>~s</dd>", [htmlise_data("http_headers", ewgi_api:get_all_headers(C))]),
  61. io_lib:format("<dt>server_protocol</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_protocol(C)]),
  62. io_lib:format("<dt>server_software</dt><dd><pre>~s</pre></dd>", [ewgi_api:server_software(C)]),
  63. io_lib:format("<dt>ewgi version</dt><dd><pre>~p</pre></dd>", [ewgi_api:version(C)]),
  64. io_lib:format("<dt>ewgi extra data</dt><dd>~s</dd>", [htmlise_data("request_data", ewgi_api:get_all_data(C))]),
  65. "</dl>"]).
  66. htmlise_data(Name, L) when is_list(L) ->
  67. ["<dl class=\"", Name, "\">",
  68. [io_lib:format("<dt>~s</dt><dd><pre>~p</pre><dd>", [K, V]) || {K, V} <- L],
  69. "</dl>"];
  70. htmlise_data(Name, T) ->
  71. case gb_trees:to_list(T) of
  72. [] -> [];
  73. L -> htmlise_data(Name, L)
  74. end.
  75. list_to_stream(L) when is_list(L) ->
  76. fun() ->
  77. case L of
  78. [H|T] ->
  79. {H, list_to_stream(T)};
  80. [] ->
  81. {}
  82. end
  83. end.