1/*  Part of SWI-Prolog
2
3    Author:        Jan Wielemaker
4    E-mail:        J.Wielemaker@uva.nl
5    WWW:           http://www.swi-prolog.org
6    Copyright (C): 2009, University of Amsterdam
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 2
11    of the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public
19    License along with this library; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22    As a special exception, if you link this library with other files,
23    compiled with a Free Software compiler, to produce an executable, this
24    library does not by itself cause the resulting executable to be covered
25    by the GNU General Public License. This exception does not however
26    invalidate any other reasons why the executable file might be covered by
27    the GNU General Public License.
28*/
29
30:- module(http_exception,
31	  [ map_exception_to_http_status/3
32	  ]).
33
34/** <module> Internal module of the HTTP server
35
36@see	http_header.pl, http_wrapper.pl
37*/
38
39%%	map_exception_to_http_status(+Exception, -Reply, -HdrExtra)
40%
41%	Map certain defined  exceptions  to   special  reply  codes. The
42%	http(not_modified)   provides   backward     compatibility    to
43%	http_reply(not_modified).
44
45map_exception_to_http_status(http(not_modified),
46	      not_modified,
47	      [connection('Keep-Alive')]) :- !.
48map_exception_to_http_status(http_reply(Reply),
49	      Reply,
50	      [connection(Close)]) :- !,
51	(   keep_alive(Reply)
52	->  Close = 'Keep-Alive'
53	;   Close = close
54	).
55map_exception_to_http_status(http_reply(Reply, HdrExtra0),
56	      Reply,
57	      HdrExtra) :- !,
58	(   memberchk(close(_), HdrExtra0)
59	->  HdrExtra = HdrExtra0
60	;   HdrExtra = [close(Close)|HdrExtra0],
61	    (   keep_alive(Reply)
62	    ->  Close = 'Keep-Alive'
63	    ;   Close = close
64	    )
65	).
66map_exception_to_http_status(error(existence_error(http_location, Location), _),
67	      not_found(Location),
68	      [connection(close)]) :- !.
69map_exception_to_http_status(error(permission_error(_, http_location, Location), _),
70	      forbidden(Location),
71	      [connection(close)]) :- !.
72map_exception_to_http_status(error(threads_in_pool(_Pool), _),
73	      busy,
74	      [connection(close)]) :- !.
75map_exception_to_http_status(E,
76	      resource_error(E),
77	      [connection(close)]) :-
78	resource_error(E), !.
79map_exception_to_http_status(E,
80	      bad_request(E),
81	      [connection(close)]) :-
82	bad_request_error(E), !.
83map_exception_to_http_status(E,
84	      server_error(E),
85	      [connection(close)]).
86
87resource_error(error(resource_error(_), _)).
88
89bad_request_error(error(domain_error(http_request, _), _)).
90
91%%	keep_alive(+Reply) is semidet.
92%
93%	If true for Reply, the default is to keep the connection open.
94
95keep_alive(not_modified).
96keep_alive(file(_Type, _File)).
97keep_alive(tmp_file(_Type, _File)).
98keep_alive(stream(_In, _Len)).
99keep_alive(cgi_stream(_In, _Len)).
100
101
102		 /*******************************
103		 *	    IDE SUPPORT		*
104		 *******************************/
105
106% See library('trace/exceptions')
107
108:- multifile
109	prolog:general_exception/2.
110
111prolog:general_exception(http_reply(_), http_reply(_)).
112prolog:general_exception(http_reply(_,_), http_reply(_,_)).
113