6 # Rack::ShowStatus catches all empty responses the app it wraps and
7 # replaces them with a site explaining the error.
9 # Additional details can be put into <tt>rack.showstatus.detail</tt>
10 # and will be shown as HTML. If such details exist, the error page
11 # is always rendered, even if the reply was not empty.
16 @template = ERB
.new(TEMPLATE
)
20 status
, headers
, body
= @app.call(env)
21 headers
= Utils
::HeaderHash.new(headers
)
22 empty
= headers
['Content-Length'].to_i
<= 0
24 # client or server error, or explicit message
25 if (status
.to_i
>= 400 && empty
) || env["rack.showstatus.detail"]
26 req
= Rack
::Request.new(env)
27 message
= Rack
::Utils::HTTP_STATUS_CODES[status
.to_i
] || status
.to_s
28 detail
= env["rack.showstatus.detail"] || message
29 body
= @template.result(binding
)
30 size
= Rack
::Utils.bytesize(body
)
31 [status
, headers
.merge("Content-Type" => "text/html", "Content-Length" => size
.to_s
), [body
]]
33 [status
, headers
, body
]
40 Utils
.escape_html(obj
)
42 Utils
.escape_html(obj
.inspect
)
48 # adapted from Django <djangoproject.com>
49 # Copyright (c) 2005, the Lawrence Journal-World
50 # Used under the modified BSD license:
51 # http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
53 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
56 <meta http-equiv
="content-type" content
="text/html; charset=utf-8" />
57 <title
><%=h message
%> at
<%=h req
.script_name
+ req
.path_info
%></title
>
58 <meta name
="robots" content
="NONE,NOARCHIVE" />
59 <style type
="text/css">
60 html
* { padding
:0; margin
:0; }
61 body
* { padding
:10px 20px
; }
62 body
* * { padding
:0; }
63 body
{ font
:small sans-serif
; background
:#eee; }
64 body
>div
{ border-bottom
:1px solid
#ddd; }
65 h1
{ font-weight
:normal; margin-bottom
:.4em
; }
66 h1 span
{ font-size
:60%; color
:#666; font-weight:normal; }
67 table
{ border
:none; border-collapse
: collapse
; width
:100%; }
68 td
, th
{ vertical-align
:top; padding
:2px 3px
; }
69 th
{ width
:12em; text-align
:right; color
:#666; padding-right:.5em; }
70 #info { background:#f6f6f6; }
71 #info ol { margin: 0.5em 4em; }
72 #info ol li { font-family: monospace; }
73 #summary { background: #ffc; }
74 #explanation { background:#eee; border-bottom: 0px none; }
79 <h1
><%=h message
%> <span
>(<%= status
.to_i
%>)</span></h1
>
82 <th
>Request Method
:</th
>
83 <td
><%=h req
.request_method
%></td
>
87 <td
><%=h req
.url
%></td
>
95 <div id
="explanation">
97 You
're seeing this error because you use <code>Rack::ShowStatus</code>.