forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_parameters.pl
347 lines (298 loc) · 10.3 KB
/
http_parameters.pl
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2004-2009, University of Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/
:- module(http_parameters,
[ http_parameters/2, % +Request, -Params
http_parameters/3, % +Request, -Params, +TypeG
http_convert_parameter/4 % +Options, +FieldName, +ValIn, -ValOut
]).
:- use_module(http_client).
:- use_module(http_mime_plugin).
:- use_module(http_hook).
:- use_module(library(debug)).
:- use_module(library(option)).
:- use_module(library(lists)).
:- use_module(library(error)).
:- predicate_options(http_parameters/3, 3,
[ form_data(-list),
attribute_declarations(callable)
]).
/** <module> Extract parameters (GET and POST) from HTTP requests
@ingroup http
This module is used to extract the value of GET or POST parameters from
an HTTP request. The typical usage is e.g.,
==
:- http_handler('/register_user', register_user, []).
register_user(Request) :-
http_parameters(Request,
[ name(Name, []),
sex(Sex, [oneof([male,female])]),
birth_year(BY, [between(1850,10000)])
]),
register_user(Name, Sex, BY),
html_reply_page(title('New user added'),
...).
==
@see http_dispatch.pl dispatches requests to predicates.
*/
:- meta_predicate
http_parameters(+, ?, :).
%% http_parameters(+Request, ?Parms) is det.
%% http_parameters(+Request, ?Parms, :Options) is det.
%
% Get HTTP GET or POST form-data, applying type validation,
% default values, etc. Provided options are:
%
% * attribute_declarations(:Goal)
% Causes the declarations for an attributed named A to be
% fetched using call(Goal, A, Declarations).
%
% * form_data(-Data)
% Return the data read from the GET por POST request as a
% list Name = Value. All data, including name/value pairs
% used for Parms, is unified with Data.
%
% The attribute_declarations hook allows sharing the declaration
% of attribute-properties between many http_parameters/3 calls. In
% this form, the requested attribute takes only one argument and
% the options are acquired by calling the hook. For example:
%
% ==
% ...,
% http_parameters(Request,
% [ sex(Sex)
% ],
% [ attribute_declarations(http_param)
% ]),
% ...
%
% http_param(sex, [ oneof(male, female),
% description('Sex of the person')
% ]).
% ==
http_parameters(Request, Params) :-
http_parameters(Request, Params, []).
http_parameters(Request, Params, Options) :-
must_be(list, Params),
meta_options(is_meta, Options, QOptions),
option(attribute_declarations(DeclGoal), QOptions, -),
http_parms(Request, Params, DeclGoal, Form),
( memberchk(form_data(RForm), QOptions)
-> RForm = Form
; true
).
is_meta(attribute_declarations).
http_parms(Request, Params, DeclGoal, Data) :-
memberchk(method(post), Request),
memberchk(content_type(Content), Request),
form_data_content_type(Content), !,
debug(post_request, 'POST Request: ~p', [Request]),
http_read_data(Request, Data, []),
debug(post, 'POST Data: ~p', [Data]),
fill_parameters(Params, Data, DeclGoal).
http_parms(Request, Params, DeclGoal, Search) :-
( memberchk(search(Search), Request)
-> true
; Search = []
),
fill_parameters(Params, Search, DeclGoal).
:- multifile
form_data_content_type/1.
form_data_content_type('application/x-www-form-urlencoded') :- !.
form_data_content_type(ContentType) :-
sub_atom(ContentType, 0, _, _, 'application/x-www-form-urlencoded;').
%% fill_parameters(+ParamDecls, +FormData, +DeclGoal)
%
% Fill values from the parameter list
fill_parameters([], _, _).
fill_parameters([H|T], FormData, DeclGoal) :-
fill_parameter(H, FormData, DeclGoal),
fill_parameters(T, FormData, DeclGoal).
fill_parameter(H, _, _) :-
var(H), !,
instantiation_error(H).
fill_parameter(group(Members, _Options), FormData, DeclGoal) :-
is_list(Members), !,
fill_parameters(Members, FormData, DeclGoal).
fill_parameter(H, FormData, _) :-
H =.. [Name,Value,Options], !,
fill_param(Name, Value, Options, FormData).
fill_parameter(H, FormData, DeclGoal) :-
H =.. [Name,Value],
( DeclGoal \== (-),
call(DeclGoal, Name, Options)
-> true
; throw(error(existence_error(attribute_declaration, Name), _))
),
fill_param(Name, Value, Options, FormData).
fill_param(Name, Values, Options, FormData) :-
memberchk(zero_or_more, Options), !,
fill_param_list(FormData, Name, Values, Options).
fill_param(Name, Values, Options, FormData) :-
memberchk(list(Type), Options), !,
fill_param_list(FormData, Name, Values, [Type|Options]).
fill_param(Name, Value, Options, FormData) :-
( memberchk(Name=Value0, FormData),
Value0 \== '' % Not sure
-> http_convert_parameter(Options, Name, Value0, Value)
; memberchk(default(Value), Options)
-> true
; memberchk(optional(true), Options)
-> true
; throw(error(existence_error(form_data, Name), _))
).
fill_param_list([], _, [], _).
fill_param_list([Name=Value0|Form], Name, [Value|VT], Options) :- !,
http_convert_parameter(Options, Name, Value0, Value),
fill_param_list(Form, Name, VT, Options).
fill_param_list([_|Form], Name, VT, Options) :-
fill_param_list(Form, Name, VT, Options).
%% http_convert_parameter(+Options, +FieldName, +ValueIn, -ValueOut) is det.
%
% Conversion of an HTTP form value. First tries the multifile hook
% http:convert_parameter/3 and next the built-in checks.
%
% @param Option List as provided with the parameter
% @param FieldName Name of the HTTP field (for better message)
% @param ValueIn Atom value as received from HTTP layer
% @param ValueOut Possibly converted final value
% @error type_error(Type, Value)
http_convert_parameter([], _, Value, Value).
http_convert_parameter([H|T], Field, Value0, Value) :-
( check_type_no_error(H, Value0, Value1)
-> http_convert_parameter(T, Field, Value1, Value)
; format(string(Msg), 'HTTP parameter ~w', [Field]),
throw(error(type_error(H, Value0),
context(_, Msg)))
).
check_type_no_error(Type, In, Out) :-
http:convert_parameter(Type, In, Out), !.
check_type_no_error(Type, In, Out) :-
check_type3(Type, In, Out).
%% check_type3(+Type, +ValueIn, -ValueOut) is semidet.
%
% HTTP parameter type-check for types that need converting.
check_type3((T1;T2), In, Out) :- !,
( check_type_no_error(T1, In, Out)
-> true
; check_type_no_error(T2, In, Out)
).
check_type3(number, Atom, Number) :- !,
to_number(Atom, Number).
check_type3(integer, Atom, Integer) :- !,
to_number(Atom, Integer),
integer(Integer).
check_type3(nonneg, Atom, Integer) :- !,
to_number(Atom, Integer),
integer(Integer),
Integer >= 0.
check_type3(float, Atom, Float) :- !,
to_number(Atom, Number),
Float is float(Number).
check_type3(between(Low, High), Atom, Value) :- !,
to_number(Atom, Number),
( (float(Low) ; float(High))
-> Value is float(Number)
; Value = Number
),
must_be(between(Low, High), Value).
check_type3(boolean, Atom, Bool) :- !,
truth(Atom, Bool).
check_type3(Type, Atom, Atom) :-
check_type2(Type, Atom).
to_number(In, Number) :-
number(In), !, Number = In.
to_number(In, Number) :-
atom(In),
catch(atom_number(In, Number), _, fail).
%% check_type2(+Type, +ValueIn) is semidet.
%
% HTTP parameter type-check for types that need no conversion.
check_type2(oneof(Set), Value) :- !,
memberchk(Value, Set).
check_type2(length > N, Value) :- !,
atom_length(Value, Len),
Len > N.
check_type2(length >= N, Value) :- !,
atom_length(Value, Len),
Len >= N.
check_type2(length < N, Value) :- !,
atom_length(Value, Len),
Len < N.
check_type2(length =< N, Value) :- !,
atom_length(Value, Len),
Len =< N.
check_type2(_, _).
%% truth(+In, -Boolean) is semidet.
%
% Translate some commonly used textual representations for true
% and false into their canonical representation.
truth(true, true).
truth('TRUE', true).
truth(yes, true).
truth('YES', true).
truth(on, true).
truth('ON', true). % IE7
truth('1', true).
truth(false, false).
truth('FALSE', false).
truth(no, false).
truth('NO', false).
truth(off, false).
truth('OFF', false).
truth('0', false).
/*******************************
* XREF SUPPORT *
*******************************/
:- multifile
prolog:called_by/2,
emacs_prolog_colours:goal_colours/2.
prolog:called_by(http_parameters(_,_,Options), [G+2]) :-
option(attribute_declarations(G), Options, _),
callable(G), !.
emacs_prolog_colours:goal_colours(http_parameters(_,_,Options),
built_in-[classify, classify, Colours]) :-
option_list_colours(Options, Colours).
option_list_colours(Var, error) :-
var(Var), !.
option_list_colours([], classify) :- !.
option_list_colours(Term, list-Elements) :-
Term = [_|_], !,
option_list_colours_2(Term, Elements).
option_list_colours(_, error).
option_list_colours_2(Var, classify) :-
var(Var).
option_list_colours_2([], []).
option_list_colours_2([H0|T0], [H|T]) :-
option_colours(H0, H),
option_list_colours_2(T0, T).
option_colours(Var, classify) :-
var(Var), !.
option_colours(_=_, built_in-[classify,classify]) :- !.
option_colours(attribute_declarations(_), % DCG = is a hack!
option(attribute_declarations)-[dcg]) :- !.
option_colours(Term, option(Name)-[classify]) :-
compound(Term),
Term =.. [Name,_Value], !.
option_colours(_, error).