View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2009-2017, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(http_exception,
   37          [ map_exception_to_http_status/4,     % +Exception, -Reply,
   38                                                % -HdrExtra, -Context
   39            in_or_exclude_backtrace/2           % +Error, -CleanedError
   40          ]).   41:- use_module(library(settings), [current_setting/1, setting/2]).   42
   43/** <module> Map Prolog exceptions to HTTP errors
   44
   45This module maps exceptions from various parts  of the HTTP libraries as
   46well as exceptions from user  handler   predicates  into meaningful HTTP
   47error codes such as 4XX and 5XX  codes. For example, existence errors on
   48http locations are mapped to 404 while out-of-stack is mapped to 503.
   49
   50This library provides two hooks:
   51
   52  - http:map_exception_to_http_status_hook/4 can be used to map
   53    additional exceptions to any HTTP status code.
   54  - http:bad_request_error/2 can be extended to map exceptions into 400
   55    bad request responses.
   56
   57@see    http_header.pl, http_wrapper.pl
   58*/
   59
   60:- multifile
   61    http:bad_request_error/2,                 % Formal, Context
   62    http:map_exception_to_http_status_hook/4. % Exception, Reply, HdrExtra, Ctx
   63
   64%!  map_exception_to_http_status(+Exception, -Reply, -HdrExtra, -Context)
   65%
   66%   Map certain exceptions to HTTP  status codes. The http(not_modified)
   67%   provides backward compatibility to http_reply(not_modified).
   68
   69map_exception_to_http_status(Exception, Reply, HdrExtra, Context) :-
   70    http:map_exception_to_http_status_hook(Exception, Reply, HdrExtra, Context),
   71    !.
   72map_exception_to_http_status(http(not_modified),
   73              not_modified,
   74              [connection('Keep-Alive')],
   75              []) :- !.
   76map_exception_to_http_status(http_reply(Reply),
   77              Reply,
   78              [connection(Close)],
   79              []) :-
   80    !,
   81    keep_alive(Reply, Close).
   82map_exception_to_http_status(http_reply(Reply, HdrExtra0),
   83              Reply,
   84              HdrExtra,
   85              Context) :-
   86    !,
   87    map_exception_to_http_status(http_reply(Reply, HdrExtra0, []),
   88                                 Reply,
   89                                 HdrExtra,
   90                                 Context).
   91
   92map_exception_to_http_status(http_reply(Reply, HdrExtra0, Context),
   93              Reply,
   94              HdrExtra,
   95              Context):-
   96    !,
   97    (   memberchk(connection(_), HdrExtra0)
   98    ->  HdrExtra = HdrExtra0
   99    ;   HdrExtra = [connection(Close)|HdrExtra0],
  100        keep_alive(Reply, Close)
  101    ).
  102map_exception_to_http_status(error(existence_error(http_location, Location), _),
  103              not_found(Location),
  104              [connection(close)],
  105              []) :- !.
  106map_exception_to_http_status(error(permission_error(http_method, Method, Location), _),
  107              method_not_allowed(Method, Location),
  108              [connection(close)],
  109              []) :- !.
  110map_exception_to_http_status(error(permission_error(_, http_location, Location), _),
  111              forbidden(Location),
  112              [connection(close)],
  113              []) :- !.
  114map_exception_to_http_status(error(threads_in_pool(_Pool), _),
  115              busy,
  116              [connection(close)],
  117              []) :- !.
  118map_exception_to_http_status(error(resource_error(max_request_size), Ctx),
  119              payload_too_large(MaxSize),
  120              [connection(close)],
  121              []) :-
  122    !,
  123    (   subsumes_term(context(_, payload_too_large(_)), Ctx)
  124    ->  Ctx = context(_, payload_too_large(MaxSize))
  125    ;   true
  126    ).
  127map_exception_to_http_status(error(timeout_error(read, _),
  128                                   context(_, in_http_request)),
  129              request_timeout,
  130              [connection(close)],
  131              []) :-
  132    !.
  133map_exception_to_http_status(error(resource_error(Limit), _),
  134              request_header_fields_too_large,
  135              [connection(close)],
  136              []) :-
  137    header_limit(Limit),
  138    !.
  139map_exception_to_http_status(E,
  140              resource_error(E),
  141              [connection(close)],
  142              []) :-
  143    is_resource_error(E),
  144    !.
  145map_exception_to_http_status(E,
  146              bad_request(E2),
  147              [connection(close)],
  148              []) :-
  149    bad_request_exception(E),
  150    !,
  151    discard_stack_trace(E, E2).
  152map_exception_to_http_status(E,
  153              server_error(E2),
  154              [connection(close)],
  155              []) :-
  156    in_or_exclude_backtrace(E, E2).
  157
  158is_resource_error(error(resource_error(_), _)).
  159
  160header_limit(max_line_length).
  161header_limit(max_header_size).
  162
  163bad_request_exception(error(Error, Context)) :-
  164    nonvar(Error),
  165    bad_request_error(Error, ContextGeneral),
  166    (   var(ContextGeneral)
  167    ->  true
  168    ;   Context = context(_Stack, ContextInstance)
  169    ->  subsumes_term(ContextGeneral, ContextInstance)
  170    ),
  171    !.
  172
  173bad_request_error(Error, Context) :-
  174    http:bad_request_error(Error, Context).
  175bad_request_error(Error, Context) :-
  176    default_bad_request_error(Error, Context).
  177
  178default_bad_request_error(domain_error(http_request, _), _).
  179default_bad_request_error(existence_error(http_parameter, _), _).
  180default_bad_request_error(type_error(_, _), http_parameter(_)).
  181default_bad_request_error(syntax_error(http_request_line(_)), _).
  182default_bad_request_error(syntax_error(http_request(_)), _).
  183default_bad_request_error(syntax_error(_), in_http_request).
  184default_bad_request_error(domain_error(_, _), in_http_request).
  185
  186discard_stack_trace(error(Formal, context(_,Msg)),
  187                    error(Formal, context(_,Msg))).
  188
  189%!  in_or_exclude_backtrace(+ErrorIn, -ErrorOut)
  190%
  191%   Remove  the  stacktrace  from  the   exception,  unless  setting
  192%   `http:client_backtrace` is `true`.
  193
  194in_or_exclude_backtrace(Error, Error) :-
  195    current_setting(http:client_backtrace),
  196    setting(http:client_backtrace, true),
  197    !.
  198in_or_exclude_backtrace(Error0, Error) :-
  199    discard_stack_trace(Error0, Error),
  200    !.
  201in_or_exclude_backtrace(Exception, Exception).
  202
  203
  204%!  http:bad_request_error(+Formal, -ContextTemplate) is semidet.
  205%
  206%   If  an  exception  of  the   term  error(Formal,  context(Stack,
  207%   Context)) is caught and  subsumes_term(ContextTemplate, Context)
  208%   is true, translate the exception into  an HTTP 400 exception. If
  209%   the exception contains a stack-trace, this  is stripped from the
  210%   response.
  211%
  212%   The idea behind this hook  is   that  applications can raise 400
  213%   responses by
  214%
  215%     - Throwing a specific (error) exception and adding a rule
  216%       to this predicate to interpret this as 400.
  217%     - Define rules for prolog:error_message//1 to formulate
  218%       an appropriate message.
  219
  220
  221%!  keep_alive(+Reply) is semidet.
  222%!  keep_alive(+Reply, -Connection) is det.
  223%
  224%   If true for Reply, the default is to keep the connection open.
  225
  226keep_alive(Reply, Connection) :-
  227    (   keep_alive(Reply)
  228    ->  Connection = 'Keep-Alive'
  229    ;   Connection = close
  230    ).
  231
  232keep_alive(not_modified).
  233keep_alive(bytes(_Type, _Bytes)).
  234keep_alive(file(_Type, _File)).
  235keep_alive(tmp_file(_Type, _File)).
  236keep_alive(stream(_In, _Len)).
  237keep_alive(cgi_stream(_In, _Len)).
  238keep_alive(switching_protocols(_Goal, _)).
  239
  240
  241                 /*******************************
  242                 *          IDE SUPPORT         *
  243                 *******************************/
  244
  245% See library('trace/exceptions')
  246
  247:- multifile
  248    prolog:general_exception/2.  249
  250prolog:general_exception(http_reply(_), http_reply(_)).
  251prolog:general_exception(http_reply(_,_), http_reply(_,_))