forked from gotthardp/lorawan-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlorawan_admin_static.erl
More file actions
134 lines (116 loc) · 4.28 KB
/
lorawan_admin_static.erl
File metadata and controls
134 lines (116 loc) · 4.28 KB
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
%
% Copyright (c) 2016-2019 Petr Gotthard <[email protected]>
% All rights reserved.
% Distributed under the terms of the MIT License. See the LICENSE file.
%
% Simplified version of cowboy_static with authentication and gzip.
%
-module(lorawan_admin_static).
-export([init/2]).
-export([is_authorized/2]).
-export([malformed_request/2]).
-export([forbidden/2]).
-export([content_types_provided/2]).
-export([resource_exists/2]).
-export([last_modified/2]).
-export([generate_etag/2]).
-export([get_file/2]).
-include_lib("kernel/include/file.hrl").
-record(state, {file, scopes, auth_fields}).
init(Req, {file, Path, Scopes}) ->
init_rest(Req, [Path], Scopes);
init(Req, {dir, Path, Scopes}) ->
init_rest(Req, [Path | cowboy_req:path_info(Req)], Scopes);
init(Req, {priv_file, App, Path, Scopes}) ->
init_rest(Req, [priv_dir(App), Path], Scopes);
init(Req, {priv_dir, App, Path, Scopes}) ->
init_rest(Req, [priv_dir(App), Path | cowboy_req:path_info(Req)], Scopes).
priv_dir(App) ->
case code:priv_dir(App) of
{error, bad_name} ->
lager:error("Unknown priv_dir of ~p", [App]),
[];
PrivDir ->
PrivDir
end.
init_rest(Req, Path0, Scopes) ->
{cowboy_rest, Req, #state{file=file_info(Req, Path0), scopes=Scopes}}.
file_info(Req, Path0) ->
Path = filename:absname(filename:join(Path0)),
case gzip_accepted(Req) of
true ->
Path2 = <<Path/binary, ".gz">>,
case file:read_file_info(Path2, [{time, universal}]) of
{ok, Info} ->
{Path2, Info, cow_mimetypes:all(Path)};
{error, _} ->
file_info_plain(Path)
end;
false ->
file_info_plain(Path)
end.
gzip_accepted(Req) ->
case cowboy_req:parse_header(<<"accept-encoding">>, Req) of
undefined ->
false;
Encodings ->
case [E || E={<<"gzip">>, Q} <- Encodings, Q =/= 0] of
[] ->
false;
_ ->
true
end
end.
file_info_plain(Path) ->
case file:read_file_info(Path, [{time, universal}]) of
{ok, Info} -> {Path, Info, undefined};
Error -> Error
end.
is_authorized(Req, #state{scopes=Scopes}=State) ->
case proplists:get_value(<<"anonymous">>, Scopes) of
undefined ->
case lorawan_admin:handle_authorization(Req, Scopes) of
{true, AuthFields} ->
{true, Req, State#state{auth_fields=AuthFields}};
Else ->
{Else, Req, State}
end;
AnonFields ->
{true, Req, State#state{auth_fields=AnonFields}}
end.
malformed_request(Req, #state{file=Info}=State) ->
{Info =:= error, Req, State}.
forbidden(Req, #state{auth_fields=[]}=State) ->
{true, Req, State};
forbidden(Req, #state{file={error, _}}=State) ->
{true, Req, State};
forbidden(Req, #state{file={_, #file_info{type=directory}, _}}=State) ->
{true, Req, State};
forbidden(Req, #state{file={_, #file_info{access=Access}, _}}=State)
when Access =:= write; Access =:= none ->
{true, Req, State};
forbidden(Req, State) ->
{false, Req, State}.
content_types_provided(Req, #state{file={Path, _, _}}=State) ->
{[{cow_mimetypes:web(Path), get_file}], Req, State}.
resource_exists(Req, #state{file={_, #file_info{type=regular}, _}}=State) ->
{true, Req, State};
resource_exists(Req, State) ->
{false, Req, State}.
generate_etag(Req, #state{file={_, #file_info{size=Size, mtime=Mtime}, _}}=State) ->
{generate_default_etag(Size, Mtime), Req, State}.
generate_default_etag(Size, Mtime) ->
{strong, integer_to_binary(erlang:phash2({Size, Mtime}, 16#ffffffff))}.
last_modified(Req, #state{file={_, #file_info{mtime=Modified}, _}}=State) ->
{Modified, Req, State}.
get_file(Req, #state{file={Path, #file_info{size=Size}, Compressed}}=State) ->
Req2 =
case Compressed of
{Type, Subtype, []} ->
cowboy_req:set_resp_header(<<"content-type">>, <<Type/binary, $/, Subtype/binary>>,
cowboy_req:set_resp_header(<<"content-encoding">>, <<"gzip">>, Req));
undefined ->
Req
end,
{{sendfile, 0, Size, Path}, Req2, State}.
% end of file