Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / showstatus.rb
1 require 'erb'
2 require 'rack/request'
3 require 'rack/utils'
4
5 module Rack
6 # Rack::ShowStatus catches all empty responses the app it wraps and
7 # replaces them with a site explaining the error.
8 #
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.
12
13 class ShowStatus
14 def initialize(app)
15 @app = app
16 @template = ERB.new(TEMPLATE)
17 end
18
19 def call(env)
20 status, headers, body = @app.call(env)
21 headers = Utils::HeaderHash.new(headers)
22 empty = headers['Content-Length'].to_i <= 0
23
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]]
32 else
33 [status, headers, body]
34 end
35 end
36
37 def h(obj) # :nodoc:
38 case obj
39 when String
40 Utils.escape_html(obj)
41 else
42 Utils.escape_html(obj.inspect)
43 end
44 end
45
46 # :stopdoc:
47
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
52 TEMPLATE = <<'HTML'
53 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
54 <html lang="en">
55 <head>
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; }
75 </style>
76 </head>
77 <body>
78 <div id="summary">
79 <h1><%=h message %> <span>(<%= status.to_i %>)</span></h1>
80 <table class="meta">
81 <tr>
82 <th>Request Method:</th>
83 <td><%=h req.request_method %></td>
84 </tr>
85 <tr>
86 <th>Request URL:</th>
87 <td><%=h req.url %></td>
88 </tr>
89 </table>
90 </div>
91 <div id="info">
92 <p><%= detail %></p>
93 </div>
94
95 <div id="explanation">
96 <p>
97 You're seeing this error because you use <code>Rack::ShowStatus</code>.
98 </p>
99 </div>
100 </body>
101 </html>
102 HTML
103
104 # :startdoc:
105 end
106 end