<div dir="ltr">I&#39;ve put together a more efficient version on School of Haskell:<div><br></div><div><a href="https://www.fpcomplete.com/user/snoyberg/random-code-snippets/wai-blaze-html">https://www.fpcomplete.com/user/snoyberg/random-code-snippets/wai-blaze-html</a><br>

</div><div><br></div><div>The differences from yours are:</div><div><ul><li>Instead of turning your H.Html into a lazy ByteString and then into a Builder, this code goes directly to a Builder via renderHtmlBuilder.</li><li>

No content-length header is included in the output, since that would require rendering the builder to a lazy bytestring, which would be an unnecessary buffer copy.</li><li>Doesn&#39;t use BS.pack, since OverloadedStrings makes it unnecessary.</li>

</ul></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 9, 2013 at 11:44 AM, Adrian May <span dir="ltr">&lt;<a href="mailto:adrian.alexander.may@gmail.com" target="_blank">adrian.alexander.may@gmail.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div>Hi All,<br><br></div>I just cobbled together the code below from a couple of samples, but got the types matched up by trial and error. I don&#39;t really understand when things are getting converted between lazy, strict, utf8, ascii, etc. I don&#39;t want ascii in the served page at all. Is it optimal?<br>


<br></div>TIA,<br></div>Adrian<br><br><span style="font-family:comic sans ms,sans-serif">{-# LANGUAGE OverloadedStrings #-}<br><br>module Main where <br><br>import Network.Wai (Application, Response (ResponseBuilder)) <br>


import Network.HTTP.Types (status200) <br>import Network.HTTP.Types.Header (hContentType, hContentLength, hConnection) <br>import Network.Wai.Handler.Warp (run) <br>import Blaze.ByteString.Builder (fromByteString, fromLazyByteString) <br>


import qualified Data.ByteString.Char8 as BS (pack, length) <br><br>import qualified Text.Blaze.Html5 as H<br>import qualified Text.Blaze.Html5.Attributes as A<br>import Text.Blaze.Html.Renderer.Utf8<br><br>import qualified Data.ByteString.Lazy as LB<br>


<br>application:: Application <br>application _ = return $   <br>    ResponseBuilder status200 [(hContentType, BS.pack &quot;text/html&quot;),  <br>                               (hContentLength, BS.pack bodyLen),  <br>                               (hConnection, BS.pack &quot;keep-alive&quot;)]  <br>


                    $ fromLazyByteString body <br>    where body = root<br>          bodyLen = show. LB.length $ body <br><br>root = renderHtml rooth<br><br>rooth :: H.Html<br>rooth = H.docTypeHtml $ do<br>    H.body $ do<br>


        H.h1 &quot;Hello&quot;<br><br>main:: IO ()  <br>main = run 8080 application <br><br></span><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>