/
playdar_web.erl
278 lines (242 loc) · 11.1 KB
/
playdar_web.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
%% @author author <author@example.com>
%% @copyright YYYY author.
%% @doc Web server for playdar.
-module(playdar_web).
-export([start/1, stop/0, loop/2, render/3]).
-include("playdar.hrl").
%% External API
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun(Req) -> ?MODULE:loop(Req, DocRoot) end,
Opts = [ {loop, Loop}, {name, ?MODULE} | Options1 ],
mochiweb_http:start(Opts).
stop() ->
mochiweb_http:stop(?MODULE).
loop(Req, DocRoot) ->
% TODO filter non /sid/ reqs unless from localhost
{R1,R2,R3} = now(),
random:seed(R1,R2,R3),
Peer = Req:get(peer),
?LOG(info, "~s ~s ~s ~p", [string:to_upper(atom_to_list(Req:get(method))),
Req:get(raw_path), Peer, self()]),
% Reqs from localhost can do anything
% reqs from elsewhere are only allowed to stream.
% this presumes they did the resolving using lan plugin or something.
case Req:get(peer) of
"127.0.0.1" -> loop1(Req, DocRoot);
"::1" -> loop1(Req, DocRoot);
"0:0:0:0:0:0:0:1" -> loop1(Req, DocRoot);
"::FFFF:127.0.0.1" -> loop1(Req, DocRoot); % this one, i think :)
_ ->
case Req:get(path) of
"/sid/" ++ _ ->
loop1(Req, DocRoot);
_ ->
Req:respond({403, [], <<"<h1>Not Authorised</h1>">>})
end
end.
loop1(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
case Path of
"" ->
Resolvers = [ [{"mod", atom_to_list(proplists:get_value(mod, Pl))}|proplists:delete(mod,Pl)]
|| Pl <- playdar_resolver:resolvers() ],
HttpMenus = playdar_http_registry:get_all(),
% change bools to strings, for rendering (yuk, TODO hack erlydtl)
Resolvers1 = [ begin
LOS = case proplists:get_value(localonly, Resolver, false) of
true -> "yes";
false -> "no"
end,
[ {localonly, LOS} | proplists:delete(localonly, Resolver) ]
end || Resolver <- Resolvers ],
Vars = [ {resolvers, Resolvers1},
{protocols, playdar_reader_registry:get_all()},
{http_paths, HttpMenus}
],
render(Req, DocRoot ++ "/index.html", Vars);
% serving a file that was found by a query, based on SID:
"sid/" ++ SidL ->
Sid = list_to_binary(SidL),
case playdar_resolver:result(Sid) of
undefined ->
Req:not_found();
A ->
Ref = make_ref(),
case playdar_reader_registry:get_streamer(A, self(), Ref) of
undefined ->
Req:respond({503, [], <<"Playdar server error: no such protocol handler">>});
Sfun ->
process_flag(trap_exit, true),
Spid = Sfun(),
?LOG(info, "Stream fun pid: ~p", [Spid]),
%link(Spid),
stream_result(Req, Ref)
end
end;
"auth_1/" ->
Qs = Req:parse_qs(),
Required = ["website", "name" ],
case lists:member(undefined, [ proplists:get_value(R, Qs) ||
R <- Required]) of
true ->
Req:not_found();
false ->
Ftok = playdar_auth:gen_formtoken(),
case proplists:get_value("json", Qs) of
undefined ->
FormVars= [
{"receiverurl", proplists:get_value("receiverurl",Qs,"")},
{"formtoken", Ftok},
{"website", proplists:get_value("website", Qs,"")},
{"name", proplists:get_value("name", Qs, "")}
],
render(Req, DocRoot ++ "/auth.html", [{formvars, FormVars}]);
_ ->
Resp = mochijson2:encode({struct, [{<<"formtoken">>,list_to_binary(Ftok)}]}),
Req:ok({"appplication/json; charset=utf-8",[],Resp})
end
end;
"auth_2/" ->
Qs = Req:parse_post(),
io:format("~p~n", [Qs]),
Required = ["website", "name", "formtoken"],
AllPresent = not lists:member(undefined, [ proplists:get_value(R, Qs) ||
R <- Required]),
FormTokOk = (ok == playdar_auth:consume_formtoken(proplists:get_value("formtoken", Qs))),
io:format("AllPressent ~w formtok ~w~n", [AllPresent, FormTokOk]),
if
AllPresent and FormTokOk ->
% create the entry in the auth db:
AuthCode = playdar_utils:uuid_gen(),
playdar_auth:create(AuthCode, [ {website, proplists:get_value("website",Qs, "")},
{name, proplists:get_value("name",Qs, "")}
]),
case proplists:get_value("receiverurl",Qs, "") of
"" ->
case proplists:get_value("json", Qs, "") of
"" ->
Vars = [ {website,proplists:get_value("website",Qs, "")},
{name,proplists:get_value("name",Qs, "")},
{authcode, AuthCode}
],
render(Req, DocRoot ++ "/auth.na.html", Vars);
_ ->
Resp = mochijson2:encode({struct, [{<<"authtoken">>,AuthCode}]}),
Req:ok({"appplication/json; charset=utf-8",[],Resp})
end;
RecUrl ->
% verify RecUrl doesnt contain newlines
AuthStr = binary_to_list(AuthCode),
Url = case lists:member($?, RecUrl) of
true ->
RecUrl ++ "&authtoken=" ++ AuthStr ++ "#" ++ AuthStr;
false ->
RecUrl ++ "?authtoken=" ++ AuthStr ++ "#" ++ AuthStr
end,
io:format("Redirect: ~s~n", [Url]),
Req:respond({301, [{"Location", Url}], <<"">>})
end;
true ->
Req:not_found()
end;
"queries" ->
Qids = playdar_resolver:queries(),
Fun = fun(Qid) ->
{Results, #qry{obj = Qryobj}, Solved} = playdar_resolver:results(Qid),
Json = mochijson2:encode(Qryobj),
NumResults = length(Results),
[{qid,Qid}, {solved, Solved},
{qry,Json}, {num_results,NumResults}]
end,
Vars = [ {queries, [Fun(Qid) || Qid <- Qids]} ],
render(Req, DocRoot ++ "/queries.html", Vars);
"queries/" ++ QidL ->
Qid = list_to_binary(QidL),
case playdar_resolver:results(Qid) of
undefined -> Req:not_found();
{ResultsList, #qry{obj = Qryobj}, Solved} ->
Results = [ [{list_to_atom(binary_to_list(K)),V}||{K,V}<-L]
|| {struct, L} <- ResultsList ],
Vars = [ {qid, Qid},
{qry, mochijson2:encode(Qryobj)},
{solved, Solved},
{results, Results} ],
render(Req, DocRoot ++ "/query.html", Vars)
end;
"authcodes" ->
Qs = Req:parse_qs(),
case proplists:get_value("revoke",Qs) of
undefined ->
Codes = [ [{code, Code}|Props] || {Code, Props} <- playdar_auth:all() ],
render(Req, DocRoot ++ "/authcodes.html", [{codes, Codes}]);
Code ->
playdar_auth:revoke(list_to_binary(Code)),
Req:respond({302, [{"Location", "/authcodes"}], <<"">>})
end;
% serve any file under /static/ verbatim
"static/" ++ StaticFile ->
Req:serve_file("static/" ++ StaticFile, DocRoot);
"crossdomain.xml" ->
% crossdomain support is on by default, but can be disabled in config:
case ?CONFVAL(crossdomain, true) of
true ->
Req:serve_file("crossdomain.xml", DocRoot);
false ->
Req:not_found()
end;
% hand off dynamically:
_ ->
case playdar_http_registry:get_handler(Req:get(path)) of
undefined ->
Req:not_found();
Handler ->
Handler(Req, DocRoot)
end
end.
%% Internal API
render(Req, File, Vars) ->
% if render is crashing, Vars is probably invalid:
%?LOG(info, "render(~s) ~p", [File, Vars]),
ok = erlydtl:compile(File, tpl), % TODO compiled templates could be cached
{ok, HtmlIO} = tpl:render(Vars),
Html = lists:flatten(HtmlIO),
Req:ok({"text/html",[{"Server", "Playdar"}],Html}).
stream_result(Req, Ref) ->
receive
{Ref, headers, Headers0} ->
{Mimetype0, Headers} = get_option("content-type", Headers0),
Mimetype = case Mimetype0 of
undefined -> "binary/unspecified";
X when is_list(X) -> X
end,
Resp = Req:ok( { Mimetype, [{"Server", "Playdar"}|Headers], chunked } ),
%io:format("Headers sent~n",[]),
stream_result_body(Req, Resp, Ref);
{Ref, error, _Reason} ->
Req:respond({503, [], <<"Internal fail streaming this resource">>});
{'EXIT', Pid, Reason} when Reason /= normal ->
?LOG(warn, "Streamer process ~p crashed (headers): ~p", [Pid, Reason]),
Req:respond({503, [], <<"Streamer crashed">>})
after 12000 ->
Req:ok({"text/plain", [{"Server", "Playdar"}], "Timeout on headers/initialising stream"})
end.
stream_result_body(Req, Resp, Ref) ->
receive
{Ref, data, Data} ->
Resp:write_chunk(Data),
stream_result_body(Req, Resp, Ref);
{Ref, error, _Reason} ->
err;
{Ref, eof} ->
ok
%{'EXIT', Pid, Reason} ->
% ?LOG(warn, "Streamer process ~p exited (body): ~p", [Pid, Reason]),
% stream_result_body(Req, Resp, Ref)
after 10000 ->
?LOG(info, "10secs timeout on streaming~n",[]),
timeout
end.
get_option(Option, Options) -> get_option(Option, Options, undefined).
get_option(Option, Options, Def) ->
{proplists:get_value(Option, Options, Def), proplists:delete(Option, Options)}.