forked from SWI-Prolog/packages-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_error.pl
126 lines (103 loc) · 4.52 KB
/
http_error.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (c) 2006-2023, University of Amsterdam
VU University Amsterdam
SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
:- module(http_error,
[
]).
:- use_module(library(prolog_stack), []).
:- use_module(library(settings)).
:- use_module(library(broadcast)).
:- use_module(library(debug)).
/** <module> 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 the http(error) debug channal, 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.
*/
:- setting(http:client_backtrace, boolean, true,
'Make backtrace visible to the client').
/*******************************
* LOG ERRORS TO STDERR *
*******************************/
:- debug(http(error)).
:- listen(http(Message),
http_listen(Message)).
:- dynamic
saved_request/2,
suppress_code/1.
http_listen(_) :-
\+ debugging(http(error)),
!.
http_listen(request_start(Id, Request)) :-
!,
asserta(saved_request(Id, Request)).
http_listen(request_finished(Id, Code, Status, _CPU, _Bytes)) :-
retract(saved_request(Id, Request)),
!,
Code >= 400,
\+ suppress_code(Code),
memberchk(path(Path), Request),
memberchk(method(Method), Request),
upcase_atom(Method, UMethod),
reply_status(Status, Reply),
debug(http(error),
'~w ~w: [~w] ~w', [UMethod, Path, Code, Reply]).
reply_status(Status, Reply) :-
map_exception(Status, Reply),
!.
reply_status(Status, Message) :-
Status = error(_,_),
!,
message_to_string(Status, Message).
reply_status(Status, Status).
map_exception(http_reply(bytes(ContentType,Bytes),_), bytes(ContentType,L)) :-
string_length(Bytes, L). % also does lists
map_exception(http_reply(Reply), Reply).
map_exception(http_reply(Reply, _), Reply).
map_exception(error(existence_error(http_location, Location), _Stack),
error(404, Location)).
/*******************************
* DECORATE STACK TRACES *
*******************************/
:- dynamic prolog_stack:stack_guard/1.
:- multifile prolog_stack:stack_guard/1.
prolog_stack:stack_guard(httpd_wrapper:wrapper/5).
prolog_stack:stack_guard(httpd_wrapper:handler_with_output_to/5).