showstatus.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. require 'erb'
  2. require 'rack/request'
  3. require 'rack/utils'
  4. module Rack
  5. # Rack::ShowStatus catches all empty responses and replaces them
  6. # with a site explaining the error.
  7. #
  8. # Additional details can be put into <tt>rack.showstatus.detail</tt>
  9. # and will be shown as HTML. If such details exist, the error page
  10. # is always rendered, even if the reply was not empty.
  11. class ShowStatus
  12. def initialize(app)
  13. @app = app
  14. @template = ERB.new(TEMPLATE)
  15. end
  16. def call(env)
  17. status, headers, body = @app.call(env)
  18. headers = Utils::HeaderHash.new(headers)
  19. empty = headers['Content-Length'].to_i <= 0
  20. # client or server error, or explicit message
  21. if (status.to_i >= 400 && empty) || env["rack.showstatus.detail"]
  22. # This double assignment is to prevent an "unused variable" warning on
  23. # Ruby 1.9.3. Yes, it is dumb, but I don't like Ruby yelling at me.
  24. req = req = Rack::Request.new(env)
  25. message = Rack::Utils::HTTP_STATUS_CODES[status.to_i] || status.to_s
  26. # This double assignment is to prevent an "unused variable" warning on
  27. # Ruby 1.9.3. Yes, it is dumb, but I don't like Ruby yelling at me.
  28. detail = 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. def h(obj) # :nodoc:
  37. case obj
  38. when String
  39. Utils.escape_html(obj)
  40. else
  41. Utils.escape_html(obj.inspect)
  42. end
  43. end
  44. # :stopdoc:
  45. # adapted from Django <djangoproject.com>
  46. # Copyright (c) 2005, the Lawrence Journal-World
  47. # Used under the modified BSD license:
  48. # http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
  49. TEMPLATE = <<'HTML'
  50. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  51. <html lang="en">
  52. <head>
  53. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  54. <title><%=h message %> at <%=h req.script_name + req.path_info %></title>
  55. <meta name="robots" content="NONE,NOARCHIVE" />
  56. <style type="text/css">
  57. html * { padding:0; margin:0; }
  58. body * { padding:10px 20px; }
  59. body * * { padding:0; }
  60. body { font:small sans-serif; background:#eee; }
  61. body>div { border-bottom:1px solid #ddd; }
  62. h1 { font-weight:normal; margin-bottom:.4em; }
  63. h1 span { font-size:60%; color:#666; font-weight:normal; }
  64. table { border:none; border-collapse: collapse; width:100%; }
  65. td, th { vertical-align:top; padding:2px 3px; }
  66. th { width:12em; text-align:right; color:#666; padding-right:.5em; }
  67. #info { background:#f6f6f6; }
  68. #info ol { margin: 0.5em 4em; }
  69. #info ol li { font-family: monospace; }
  70. #summary { background: #ffc; }
  71. #explanation { background:#eee; border-bottom: 0px none; }
  72. </style>
  73. </head>
  74. <body>
  75. <div id="summary">
  76. <h1><%=h message %> <span>(<%= status.to_i %>)</span></h1>
  77. <table class="meta">
  78. <tr>
  79. <th>Request Method:</th>
  80. <td><%=h req.request_method %></td>
  81. </tr>
  82. <tr>
  83. <th>Request URL:</th>
  84. <td><%=h req.url %></td>
  85. </tr>
  86. </table>
  87. </div>
  88. <div id="info">
  89. <p><%= detail %></p>
  90. </div>
  91. <div id="explanation">
  92. <p>
  93. You're seeing this error because you use <code>Rack::ShowStatus</code>.
  94. </p>
  95. </div>
  96. </body>
  97. </html>
  98. HTML
  99. # :startdoc:
  100. end
  101. end