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) 2006-2023, University of Amsterdam 7 VU University Amsterdam 8 SWI-Prolog Solutions b.v. 9 All rights reserved. 10 11 Redistribution and use in source and binary forms, with or without 12 modification, are permitted provided that the following conditions 13 are met: 14 15 1. Redistributions of source code must retain the above copyright 16 notice, this list of conditions and the following disclaimer. 17 18 2. Redistributions in binary form must reproduce the above copyright 19 notice, this list of conditions and the following disclaimer in 20 the documentation and/or other materials provided with the 21 distribution. 22 23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 27 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 POSSIBILITY OF SUCH DAMAGE. 35*/ 36 37:- module(http_error, 38 [ 39 ]). 40:- use_module(library(prolog_stack), []). 41:- use_module(library(settings)). 42:- use_module(library(broadcast)). 43:- use_module(library(debug)).
65:- setting(http:client_backtrace, boolean, true, 66 'Make backtrace visible to the client'). 67 68 69 /******************************* 70 * LOG ERRORS TO STDERR * 71 *******************************/ 72 73:- debug(http(error)). 74 75:- listen(http(Message), 76 http_listen(Message)). 77 78:- dynamic 79 saved_request/2, 80 suppress_code/1. 81 82http_listen(_) :- 83 \+ debugging(http(error)), 84 !. 85http_listen(request_start(Id, Request)) :- 86 !, 87 asserta(saved_request(Id, Request)). 88http_listen(request_finished(Id, Code, Status, _CPU, _Bytes)) :- 89 retract(saved_request(Id, Request)), 90 !, 91 Code >= 400, 92 \+ suppress_code(Code), 93 memberchk(path(Path), Request), 94 memberchk(method(Method), Request), 95 upcase_atom(Method, UMethod), 96 reply_status(Status, Reply), 97 debug(http(error), 98 '~w ~w: [~w] ~w', [UMethod, Path, Code, Reply]). 99 100reply_status(Status, Reply) :- 101 map_exception(Status, Reply), 102 !. 103reply_status(Status, Message) :- 104 Status = error(_,_), 105 !, 106 message_to_string(Status, Message). 107reply_status(Status, Status). 108 109map_exception(http_reply(bytes(ContentType,Bytes),_), bytes(ContentType,L)) :- 110 string_length(Bytes, L). % also does lists 111map_exception(http_reply(Reply), Reply). 112map_exception(http_reply(Reply, _), Reply). 113map_exception(error(existence_error(http_location, Location), _Stack), 114 error(404, Location)). 115 116 117 /******************************* 118 * DECORATE STACK TRACES * 119 *******************************/ 120 121:- dynamic prolog_stack:stack_guard/1. 122:- multifile prolog_stack:stack_guard/1. 123 124prolog_stackstack_guard(httpd_wrapper:wrapper/5). 125prolog_stackstack_guard(httpd_wrapper:handler_with_output_to/5)
Decorate uncaught HTTP exceptions with stack-trace
This module decorates uncaught exceptions of the user code with a full stack-trace and sends error reports to the Prolog console. The behaviour can be controlled by
nodebug(http(error))
After disabling thehttp(error)
debug channel, errors are only sent to the client. See nodebug/1 and debug/1.set_setting(http:client_backtrace, false)
Stop sending stack traces to the client. Note that sending the stack trace to the client simplifies debugging, it also provides clues to hackers on how to compromise your site. The more information you give them, the easier it is to break into your server! See set_setting/2 and set_setting_default/2.assert(http_error:suppress_code(Code))
makes this library become silent for replies with a matching HTTP status code. This may be used to suppress >400 replies that are "normal" in the application. */