aboutsummaryrefslogtreecommitdiffstats
path: root/lib/dialyzer/test/r9c_SUITE_data/src/inets/mod_responsecontrol.erl
blob: a997db68807638ffdc03b65a378da70e5ac3bd62 (plain) (blame)
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
317
318
319
320
321
%% ``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.
%%
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%%
%%     $Id: mod_responsecontrol.erl,v 1.1 2008/12/17 09:53:35 mikpe Exp $
%%

-module(mod_responsecontrol).
-export([do/1]).

-include("httpd.hrl").


do(Info) ->
    ?DEBUG("do -> response_control",[]),
    case httpd_util:key1search(Info#mod.data,status) of
	%% A status code has been generated!
	{StatusCode,PhraseArgs,Reason} ->
	    {proceed,Info#mod.data};
	%% No status code has been generated!
	undefined ->
	    case httpd_util:key1search(Info#mod.data,response) 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 contains any limitations
%%whether a response shall be created 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 httpd_util:key1search(Info#mod.parsed_header,"range") of
	undefined->
	    undefined;
	_Range ->
	    case httpd_util:key1search(Info#mod.parsed_header,"if-range") 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(httpd_util:key1search(Info#mod.parsed_header,HeaderField)) 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 modificated                                 %%
%%                                                                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%----------------------------------------------------------------------
%%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};
	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(httpd_util:key1search(Info#mod.parsed_header,HeaderField)) of
	undefined->
	    undefined;
	LastModified0 ->
	    LastModified=httpd_util:convert_request_date(LastModified0),
	    ?DEBUG("control_modification_data() -> "
		   "~n   Request-Field:    ~s"
		   "~n   FileLastModified: ~p"
		   "~n   FieldValue:       ~p",
		   [HeaderField,ModificationTime,LastModified]),
	    case LastModified of
		bad_date ->
		    undefined;
		_ ->
		    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.

%%----------------------------------------------------------------------
%%Compare to dates on the form {{YYYY,MM,DD},{HH,MIN,SS}}
%%If the first date is the biggest returns biggest1 (read biggestFirst)
%%If the first date is smaller
% compare_date(Date,bad_date)->
%     bad_date;

% compare_date({D1,T1},{D2,T2})->
%     case compare_date1(D1,D2) of
%       	equal ->
% 	    compare_date1(T1,T2);
% 	GTorLT->
% 	    GTorLT
%     end.

% compare_date1({T1,T2,T3},{T12,T22,T32}) when T1>T12 ->
%     bigger1;
% compare_date1({T1,T2,T3},{T1,T22,T32}) when T2>T22 ->
%     bigger1;
% compare_date1({T1,T2,T3},{T1,T2,T32}) when T3>T32 ->
%     bigger1;
% compare_date1({T1,T2,T3},{T1,T2,T3})->
%     equal;
% compare_date1(_D1,_D2)->
%     smaller1.


%% 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([$;,$ |Rest]) ->
    [];
strip_date([C|Rest]) ->
    [C|strip_date(Rest)].

send_return_value({412,_,_},FileInfo)->
    {status,{412,none,"Precondition Failed"}};

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"),
    Header = [{code,304},
	      {etag,httpd_util:create_etag(FileInfo)},
	      {content_length,0},
	      {last_modified,httpd_util:rfc1123_date(FileInfo#file_info.mtime)}],
    {response,{response,Header,nobody}}.