-
Notifications
You must be signed in to change notification settings - Fork 50
/
ezwebframe.erl
228 lines (191 loc) · 6.17 KB
/
ezwebframe.erl
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
-module(ezwebframe).
-export([start_link/2,
init/2,
websocket_handle/3,
terminate/3, websocket_terminate/3,
websocket_info/3,
append_div/3,
pre/1,
fill_div/3
]).
-import(ezwebframe_mochijson2, [encode/1, decode/1]).
%% env has only one parameter - reserved for future expansion
-record(env, {dispatch}).
start_link(Dispatch, Port) ->
io:format("Starting:~p~n",[file:get_cwd()]),
ok = application:start(crypto),
ok = application:start(ranch),
ok = application:start(cowlib),
ok = application:start(cowboy),
ok = web_server_start(Port, Dispatch).
web_server_start(Port, Dispatcher) ->
E0 = #env{dispatch=Dispatcher},
Dispatch = cowboy_router:compile([{'_',[{'_', ?MODULE, E0}]}]),
%% server is the name of this module
NumberOfAcceptors = 100,
Status =
cowboy:start_http(ezwebframe,
NumberOfAcceptors,
[{port, Port}],
[{env, [{dispatch, Dispatch}]}]),
case Status of
{error, _} ->
io:format("websockets could not be started -- "
"port ~p probably in use~n", [Port]),
init:stop();
{ok, _Pid} ->
io:format("websockets started on port:~p~n",[Port])
end.
init(Req, E0) ->
%% io:format("init:~n"),
Resource = path(Req),
%% io:format("Resource:~p~n",[Resource]),
case Resource of
["/", "websocket",ModStr] ->
Self = self(),
Mod = list_to_atom(ModStr),
%% Spawn an erlang handler
%% The return value will cause cowboy
%% to call this module at the entry point
%% websocket_handle
Pid = spawn_link(Mod, start, [Self]),
{cowboy_websocket, Req, Pid};
_ ->
handle(Req, E0)
end.
handle(Req, Env) ->
Resource = filename:join(path(Req)),
io:format("ezwebframe:handle ~p~n",[Resource]),
F = Env#env.dispatch,
Res1 = F(Resource),
io:format("mapped to:~p~n",[Res1]),
case Resource of
"/" ->
serve_file("index.html", Req, Env);
"/files" ->
list_dir(F("/"), Req, Env);
_ ->
serve_file(Res1, Req, Env)
end.
serve_file(File, Req, Env) ->
case filelib:is_dir(File) of
true ->
list_dir(File, Req, Env);
false ->
serve_abs_file(File, Req, Env)
end.
serve_abs_file(File, Req, Env) ->
Val = file:read_file(File),
case Val of
{error, _} ->
io:format("*** no page called ~p~n",[File]),
reply_html(pre({no_page_called,File}), Req, Env);
{ok, Bin} ->
Ext = filename:extension(File),
Bin1 = if_erlang_add_pre(Ext, Bin),
Req1 = send_page(classify_extension(Ext), Bin1, Req),
{ok, Req1, Env}
end.
if_erlang_add_pre(".erl", B) -> ["<pre>", B, "</pre>"];
if_erlang_add_pre(_, B) -> B.
list_dir(Root, Req, Env) ->
{ok, Files} = file:list_dir(Root),
Files1 = [add_slash(I, Root) || I <- Files],
L1 = [["<li><a href='",I,"'>",I,"</a></li>\n"] || I <- lists:sort(Files1)],
reply_html(["<h1> Directory ",Root, "</h1>\n",
"<ul>\n",L1,"</ul>\n"], Req, Env).
add_slash(I, Root) ->
Full = filename:join(Root, I),
case filelib:is_dir(Full) of
true -> I ++ "/";
false -> I
end.
send_page(Type, Data, Req) ->
cowboy_req:reply(200, [{<<"Content-Type">>,
list_to_binary(mime_type(Type))}],
Data, Req).
path(Req) ->
Path = cowboy_req:path(Req),
filename:split(binary_to_list(Path)).
reply_html(Obj, Req, Env) ->
Req1 = send_page(html, Obj, Req),
{ok, Req1, Env}.
%%----------------------------------------------------------------------
%% other callbacks
terminate(_Reason,_Req,_State) ->
%% ignore why we terminate
ok.
%%----------------------------------------------------------------------
%% websocket stuff
websocket_handle({text, Msg}, Req, Pid) ->
%% This is a Json message from the browser
case catch decode(Msg) of
{'EXIT', _Why} ->
Pid ! {invalidMessageNotJSON, Msg};
{struct, _} = Z ->
X1 = atomize(Z),
Pid ! {self(), X1};
Other ->
Pid ! {invalidMessageNotStruct, Other}
end,
{ok, Req, Pid}.
websocket_info({send,Str}, Req, Pid) ->
{reply, {text, Str}, Req, Pid, hibernate};
websocket_info([{cmd,_}|_]=L, Req, Pid) ->
B = list_to_binary(encode([{struct,L}])),
{reply, {text, B}, Req, Pid, hibernate};
websocket_info(Info, Req, Pid) ->
io:format("Handle_info Info:~p Pid:~p~n",[Info,Pid]),
{ok, Req, Pid, hibernate}.
websocket_terminate(_Reason, _Req, Pid) ->
io:format("websocket.erl terminate:~n"),
exit(Pid, socketClosed),
ok.
binary_to_atom(B) ->
list_to_atom(binary_to_list(B)).
%%----------------------------------------------------------------------
%% atomize turns all the keys in a struct to atoms
atomize({struct,L}) ->
{struct, [{binary_to_atom(I), atomize(J)} || {I, J} <- L]};
atomize(L) when is_list(L) ->
[atomize(I) || I <- L];
atomize(X) ->
X.
%%----------------------------------------------------------------------
%% these are called from the gui client code
append_div(Ws, Div, X) ->
Bin = list_to_binary(X),
send_websocket(Ws,
[{cmd,append_div},{id,Div},{txt,Bin}]).
fill_div(Ws, Div, X) ->
Bin = list_to_binary(X),
send_websocket(Ws,
[{cmd,fill_div},{id,Div},{txt,Bin}]).
send_websocket(Ws, X) ->
Ws ! {send, list_to_binary(encode([{struct,X}]))}.
%%----------------------------------------------------------------------
%% Miscellaneous small functions
classify_extension(".gif") -> gif;
classify_extension(".jpg") -> jpg;
classify_extension(".png") -> png;
classify_extension(".js") -> js;
classify_extension(".css") -> css;
classify_extension(_) -> html.
mime_type(gif) -> "image/gif";
mime_type(jpg) -> "image/jpeg";
mime_type(png) -> "image/png";
mime_type(css) -> "text/css";
mime_type(special) -> "text/plain; charset=x-user-defined";
mime_type(json) -> "application/json";
mime_type(swf) -> "application/x-shockwave-flash";
mime_type(html) -> "text/html";
mime_type(xul) -> "application/vnd.mozilla.xul+xml";
mime_type(js) -> "application/x-javascript";
mime_type(svg) -> "image/svg+xml".
pre(X) ->
["<pre>\n",quote(lists:flatten(io_lib:format("~p",[X]))), "</pre>"].
%% quote HTML characters "<" and "&"
quote("<" ++ T) -> "<" ++ quote(T);
quote("&" ++ T) -> "&" ++ quote(T);
quote([H|T]) -> [H|quote(T)];
quote([]) -> [].