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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2001-2011. 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(mod_responsecontrol).
-export([do/1]).
-include("httpd.hrl").
-include("httpd_internal.hrl").
do(Info) ->
?DEBUG("do -> response_control",[]),
case proplists:get_value(status, Info#mod.data) of
%% A status code has been generated!
{_StatusCode, _PhraseArgs, _Reason} ->
{proceed, Info#mod.data};
%% No status code has been generated!
undefined ->
case proplists:get_value(response, Info#mod.data) of
%% No response has been generated!
undefined ->
case do_responsecontrol(Info) of
continue ->
{proceed, Info#mod.data};
Response ->
{proceed,[Response | Info#mod.data]}
end;
%% A response has been generated or sent!
_Response ->
{proceed, Info#mod.data}
end
end.
%%----------------------------------------------------------------------
%%Control that the request header did not contians any limitations
%%wheather a response shall be createed or not
%%----------------------------------------------------------------------
do_responsecontrol(Info) ->
?DEBUG("do_response_control -> Request URI: ~p",[Info#mod.request_uri]),
Path = mod_alias:path(Info#mod.data, Info#mod.config_db,
Info#mod.request_uri),
case file:read_file_info(Path) of
{ok, FileInfo} ->
control(Path, Info, FileInfo);
_ ->
%% The requested asset is not a plain file and then it must
%% be generated everytime its requested
continue
end.
%%----------------------------------------------------------------------
%%Control the If-Match, If-None-Match, and If-Modified-Since
%%----------------------------------------------------------------------
%% If a client sends more then one of the if-XXXX fields in a request
%% The standard says it does not specify the behaviuor so I specified it :-)
%% The priority between the fields is
%% 1.If-modified
%% 2.If-Unmodified
%% 3.If-Match
%% 4.If-Nomatch
%% This means if more than one of the fields are in the request the
%% field with highest priority will be used
%%If the request is a range request the If-Range field will be the winner.
control(Path, Info, FileInfo) ->
case control_range(Path, Info, FileInfo) of
undefined ->
case control_Etag(Path, Info, FileInfo) of
undefined ->
case control_modification(Path, Info, FileInfo) of
continue ->
continue;
ReturnValue ->
send_return_value(ReturnValue, FileInfo)
end;
continue ->
continue;
ReturnValue ->
send_return_value(ReturnValue, FileInfo)
end;
Response->
Response
end.
%%----------------------------------------------------------------------
%%If there are both a range and an if-range field control if
%%----------------------------------------------------------------------
control_range(Path,Info,FileInfo) ->
case proplists:get_value("range", Info#mod.parsed_header) of
undefined->
undefined;
_Range ->
case proplists:get_value("if-range", Info#mod.parsed_header) of
undefined ->
undefined;
EtagOrDate ->
control_if_range(Path,Info,FileInfo,EtagOrDate)
end
end.
control_if_range(_Path, Info, FileInfo, EtagOrDate) ->
case httpd_util:convert_request_date(strip_date(EtagOrDate)) of
bad_date ->
FileEtag=httpd_util:create_etag(FileInfo),
case FileEtag of
EtagOrDate ->
continue;
_ ->
{if_range,send_file}
end;
_ErlDate ->
%%We got the date in the request if it is
case control_modification_data(Info, FileInfo#file_info.mtime,
"if-range") of
modified ->
{if_range,send_file};
_UnmodifiedOrUndefined->
continue
end
end.
%%----------------------------------------------------------------------
%%Controls the values of the If-Match and I-None-Mtch
%%----------------------------------------------------------------------
control_Etag(Path, Info, FileInfo)->
FileEtag = httpd_util:create_etag(FileInfo),
%%Control if the E-Tag for the resource matches one of the Etags in
%%the -if-match header field
case control_match(Info, FileInfo, "if-match", FileEtag) of
nomatch ->
%%None of the Etags in the if-match field matched the current
%%Etag for the resource return a 304
{412, Info, Path};
match ->
continue;
undefined ->
case control_match(Info, FileInfo, "if-none-match", FileEtag) of
nomatch ->
continue;
match ->
case Info#mod.method of
"GET" ->
{304, Info, Path};
"HEAD" ->
{304, Info, Path};
_OtherrequestMethod ->
{412, Info, Path}
end;
undefined ->
undefined
end
end.
%%----------------------------------------------------------------------
%%Control if there are any Etags for HeaderField in the request if so
%%Control if they match the Etag for the requested file
%%----------------------------------------------------------------------
control_match(Info, _FileInfo, HeaderField, FileEtag)->
case split_etags(proplists:get_value(HeaderField,
Info#mod.parsed_header)) of
undefined->
undefined;
Etags->
%%Control that the match any star not is availible
case lists:member("*",Etags) of
true->
match;
false->
compare_etags(FileEtag, Etags)
end
end.
%%----------------------------------------------------------------------
%%Split the etags from the request
%%----------------------------------------------------------------------
split_etags(undefined)->
undefined;
split_etags(Tags) ->
string:tokens(Tags,", ").
%%----------------------------------------------------------------------
%%Control if the etag for the file is in the list
%%----------------------------------------------------------------------
compare_etags(Tag,Etags) ->
case lists:member(Tag,Etags) of
true ->
match;
_ ->
nomatch
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% %%
%% Control if the file is modified %%
%% %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%----------------------------------------------------------------------
%% Control the If-Modified-Since and If-Not-Modified-Since header fields
%%----------------------------------------------------------------------
control_modification(Path,Info,FileInfo)->
?DEBUG("control_modification() -> entry",[]),
case control_modification_data(Info,
FileInfo#file_info.mtime,
"if-modified-since") of
modified->
continue;
unmodified->
{304, Info, Path};
{bad_date, _} = BadDate->
{400, Info, BadDate};
undefined ->
case control_modification_data(Info,
FileInfo#file_info.mtime,
"if-unmodified-since") of
modified ->
{412, Info, Path};
_ContinueUndefined ->
continue
end
end.
%%----------------------------------------------------------------------
%%Controls the date from the http-request if-modified-since and
%%if-not-modified-since against the modification data of the
%%File
%%----------------------------------------------------------------------
%%Info is the record about the request
%%ModificationTime is the time the file was edited last
%%Header Field is the name of the field to control
control_modification_data(Info, ModificationTime, HeaderField)->
case strip_date(proplists:get_value(HeaderField,
Info#mod.parsed_header)) of
undefined->
undefined;
LastModified0 ->
case httpd_util:convert_request_date(LastModified0) of
bad_date ->
{bad_date, LastModified0};
ConvertedReqDate ->
LastModified =
calendar:universal_time_to_local_time(ConvertedReqDate),
?DEBUG("control_modification_data() -> "
"~n Request-Field: ~s"
"~n FileLastModified: ~p"
"~n FieldValue: ~p",
[HeaderField, ModificationTime, LastModified]),
FileTime =
calendar:datetime_to_gregorian_seconds(ModificationTime),
FieldTime =
calendar:datetime_to_gregorian_seconds(LastModified),
if
FileTime =< FieldTime ->
?DEBUG("File unmodified~n", []), unmodified;
FileTime >= FieldTime ->
?DEBUG("File modified~n", []), modified
end
end
end.
%% IE4 & NS4 sends an extra '; length=xxxx' string at the end of the If-Modified-Since
%% header, we detect this and ignore it (the RFCs does not mention this).
strip_date(undefined) ->
undefined;
strip_date([]) ->
[];
strip_date([$;,$ | _]) ->
[];
strip_date([C | Rest]) ->
[C | strip_date(Rest)].
send_return_value({412,_,_}, _FileInfo)->
{status,{412,none,"Precondition Failed"}};
send_return_value({400,_, {bad_date, BadDate}}, _FileInfo)->
{status, {400, none, "Bad date: " ++ BadDate}};
send_return_value({304,Info,Path}, FileInfo)->
Suffix = httpd_util:suffix(Path),
MimeType = httpd_util:lookup_mime_default(Info#mod.config_db,Suffix,
"text/plain"),
LastModified =
case (catch httpd_util:rfc1123_date(FileInfo#file_info.mtime)) of
Date when is_list(Date) ->
[{last_modified, Date}];
_ -> %% This will rarly happen, but could happen
%% if a computer is wrongly configured.
[]
end,
Header = [{code,304},
{etag, httpd_util:create_etag(FileInfo)},
{content_length,"0"}, {mime_type, MimeType} | LastModified],
{response, {response, Header, nobody}}.
|