module Hack.Contrib.ShowStatus where
import Hack
import Hack.Utils
import Hack.Constants
import Hack.Request hiding (body)
import Hack.Response
import MPSUTF8
import Prelude hiding ((.), (^), (>), head)
import Data.Maybe
show_status :: MiddleWare
show_status app = \env -> do
response <- app env
let content_length = response.header _ContentLength .fromMaybe "0"
let content_empty = content_length .read .(<= 0)
if response.status >= 400 && content_empty
then
let
i = response.status
message = i.show_status_code .fromMaybe (i.show)
detail = env.custom_"hack.showstatus.detail" .fromMaybe message
result = template message detail env response
size = result.bytesize
in
return $
response
.set_body result
.set_content_type _TextHtml
.set_content_length size
else return response
template message detail env response =
let h = escape_html in [$here|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta httpequiv="content-type" content="text/html; charset=utf-8" />
<title>#{h message } at #{h (env.path.url2unicode) }</title>
<meta name="robots" content="NONE,NOARCHIVE" />
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sansserif; background:#eee; }
body>div { borderbottom:1px solid #ddd; }
h1 { fontweight:normal; marginbottom:.4em; }
h1 span { fontsize:60%; color:#666; fontweight:normal; }
table { border:none; bordercollapse: collapse; width:100%; }
td, th { verticalalign:top; padding:2px 3px; }
th { width:12em; textalign:right; color:#666; paddingright:.5em; }
#info { background:#f6f6f6; }
#info ol { margin: 0.5em 4em; }
#info ol li { fontfamily: monospace; }
#summary { background: #ffc; }
#explanation { background:#eee; borderbottom: 0px none; }
</style>
</head>
<body>
<div id="summary">
<h1>#{h message } <span>(#{ response.status })</span></h1>
<table class="meta">
<tr>
<th>Request Method:</th>
<td>#{ env.request_method }</td>
</tr>
<tr>
<th>Request URL:</th>
<td>#{h (env.url.url2unicode) }</td>
</tr>
</table>
</div>
<div id="info">
<p>#{h detail }</p>
</div>
<div id="explanation">
<p>
You're seeing this error because you use <code>Hack.Contrib.ShowStatus</code>.
</p>
</div>
</body>
</html>
|]