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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1997-2016. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%%
%%
-module(httpd_example).
-export([print/1]).
-export([get/2, post/2, yahoo/2, test1/2, get_bin/2]).
-export([newformat/3]).
%% These are used by the inets test-suite
-export([delay/1, chunk_timeout/3]).
print(String) ->
[header(),
top("Print"),
String++"\n",
footer()].
test1(Env, []) ->
io:format("Env:~p~n",[Env]),
["<html>",
"<head>",
"<title>Test1</title>",
"</head>",
"<body>",
"<h1>Erlang Body</h1>",
"<h2>Stuff</h2>",
"</body>",
"</html>"].
get(_Env,[]) ->
[header(),
top("GET Example"),
"<FORM ACTION=\"/cgi-bin/erl/httpd_example:get\" METHOD=GET>
<B>Input:</B> <INPUT TYPE=\"text\" NAME=\"input1\">
<INPUT TYPE=\"text\" NAME=\"input2\">
<INPUT TYPE=\"submit\"><BR>
</FORM>" ++ "\n",
footer()];
get(Env,Input) ->
default(Env,Input).
get_bin(_Env,_Input) ->
[list_to_binary(header()),
list_to_binary(top("GET Example")),
list_to_binary("<FORM ACTION=\"/cgi-bin/erl/httpd_example:get\" METHOD=GET>
<B>Input:</B> <INPUT TYPE=\"text\" NAME=\"input1\">
<INPUT TYPE=\"text\" NAME=\"input2\">
<INPUT TYPE=\"submit\"><BR>
</FORM>" ++ "\n"),
list_to_binary(footer())].
post(_Env,[]) ->
[header(),
top("POST Example"),
"<FORM ACTION=\"/cgi-bin/erl/httpd_example:post\" METHOD=POST>
<B>Input:</B> <INPUT TYPE=\"text\" NAME=\"input1\">
<INPUT TYPE=\"text\" NAME=\"input2\">
<INPUT TYPE=\"submit\"><BR>
</FORM>" ++ "\n",
footer()];
post(Env,Input) ->
default(Env,Input).
yahoo(_Env,_Input) ->
"Location: http://www.yahoo.com\r\n\r\n".
default(Env,Input) ->
[header(),
top("Default Example"),
"<B>Environment:</B> ",io_lib:format("~p",[Env]),"<BR>\n",
"<B>Input:</B> ",Input,"<BR>\n",
"<B>Parsed Input:</B> ",
io_lib:format("~p",[httpd:parse_query(Input)]),"\n",
footer()].
header() ->
header("text/html").
header(MimeType) ->
"Content-type: " ++ MimeType ++ "\r\n\r\n".
top(Title) ->
"<HTML>
<HEAD>
<TITLE>" ++ Title ++ "</TITLE>
</HEAD>
<BODY>\n".
footer() ->
"</BODY>
</HTML>\n".
newformat(SessionID, _Env, _Input)->
mod_esi:deliver(SessionID, "Content-Type:text/html\r\n\r\n"),
mod_esi:deliver(SessionID, top("new esi format test")),
mod_esi:deliver(SessionID, "This new format is nice<BR>"),
mod_esi:deliver(SessionID, "This new format is nice<BR>"),
mod_esi:deliver(SessionID, "This new format is nice<BR>"),
mod_esi:deliver(SessionID, footer()).
%% ------------------------------------------------------
delay(Time) when is_integer(Time) ->
i("httpd_example:delay(~p) -> do the delay",[Time]),
sleep(Time),
i("httpd_example:delay(~p) -> done, now reply",[Time]),
delay_reply("delay ok");
delay(Time) when is_list(Time) ->
delay(list_to_integer(Time));
delay({error,_Reason}) ->
i("delay -> called with invalid time"),
delay_reply("delay failed: invalid delay time").
delay_reply(Reply) ->
[header(),
top("delay"),
Reply,
footer()].
i(F) -> i(F,[]).
i(F,A) -> io:format(F ++ "~n",A).
sleep(T) -> receive after T -> ok end.
%% ------------------------------------------------------
chunk_timeout(SessionID, _, StrInt) ->
mod_esi:deliver(SessionID, "Tranfer-Encoding:chunked/html\r\n\r\n"),
mod_esi:deliver(SessionID, top("Test chunk encoding timeout")),
timer:sleep(20000),
mod_esi:deliver(SessionID, footer()).
|