<div dir="ltr"><div class="gmail_default" style="font-family:'courier new',monospace">I attempted to write a tiny HTTP server using only Haskell Platform packages:</div><div class="gmail_default" style="font-family:'courier new',monospace">

<br></div><div class="gmail_default" style="font-family:'courier new',monospace">* <a href="https://github.com/coreyoconnor/tiny-http-hp">https://github.com/coreyoconnor/tiny-http-hp</a></div><div class="gmail_default" style="font-family:'courier new',monospace">

<br></div><div class="gmail_default" style="font-family:'courier new',monospace">Which works... Unless the threaded runtime is used.</div><div class="gmail_default" style="font-family:'courier new',monospace">

<br></div><div class="gmail_default" style="font-family:'courier new',monospace">When compiled using the threaded runtime and run with +RTS -N this server fails to reply correctly to ~3% of requests. The expectation is that the reply will be exactly the request body. However, 3% of the time the HTTP request fails to be parsed due to an "ErrorClosed"</div>

<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">The server then executes:</div><div class="gmail_default" style="font-family:'courier new',monospace">

<br></div><div class="gmail_default" style="font-family:'courier new',monospace">* <a href="https://github.com/coreyoconnor/tiny-http-hp/blob/master/TinyHttp.hs#L23">https://github.com/coreyoconnor/tiny-http-hp/blob/master/TinyHttp.hs#L23</a></div>

<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">which responds to the client. The client receives the response correctly most of the time.</div>

<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">My current hypothesis is that some aspect of lazy IO is not playing nice. The connection is being closed before the request can be completely parsed.</div>

<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">Is this correct? What am I missing?</div><div class="gmail_default" style="font-family:'courier new',monospace">

<br></div><div class="gmail_default" style="font-family:'courier new',monospace">I know there are other HTTP server packages that could be used. However, the exercise was to build a HTTP server using only Haskell Platform packages.</div>

<div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div class="gmail_default" style="font-family:'courier new',monospace">Below is the main body of the code:</div><div class="gmail_default" style="font-family:'courier new',monospace">

<br></div><div class="gmail_default" style="font-family:'courier new',monospace"><pre style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;margin-top:0px;margin-bottom:0px;color:rgb(51,51,51);line-height:18px">

<div class="" id="LC18" style="padding-left:10px"><pre style="font-family:Consolas,'Liberation Mono',Courier,monospace;margin-top:0px;margin-bottom:0px"><div class="" id="LC18" style="padding-left:10px"><span class="" style="color:rgb(153,0,0);font-weight:bold">main</span> <span class="" style="font-weight:bold">=</span> <span class="">withSocketsDo</span> <span class="" style="font-weight:bold">$</span> <span class="" style="font-weight:bold">do</span></div>

<div class="" id="LC19" style="padding-left:10px">    <span class="">http_socket</span> <span class="" style="font-weight:bold"><-</span> <span class="">listenOn</span> <span class="" style="font-weight:bold">$</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">PortNumber</span> <span class="" style="color:rgb(0,153,153)">9090</span></div>

<div class="" id="LC20" style="padding-left:10px">    <span class="">dispatch_on_accept</span> <span class="">http_socket</span> <span class="" style="font-weight:bold">$</span> <span class="">either</span> <span class="">handle_failed_request</span> <span class="">handle_valid_request</span></div>

<div class="" id="LC21" style="padding-left:10px">    <span class="">sClose</span> <span class="">http_socket</span></div><div class="" id="LC22" style="padding-left:10px"><br></div><div class="" id="LC23" style="padding-left:10px">

<span class="" style="color:rgb(153,0,0);font-weight:bold">handle_failed_request</span> <span class="">failure</span> <span class="" style="font-weight:bold">=</span> <span class="">return</span> <span class="" style="font-weight:bold">$</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">Response</span> <span class="">(</span><span class="" style="color:rgb(0,153,153)">4</span><span class="">,</span><span class="" style="color:rgb(0,153,153)">0</span><span class="">,</span><span class="" style="color:rgb(0,153,153)">0</span><span class="">)</span> <span class="" style="color:rgb(221,17,68)">"Bad Request"</span> <span class="">[</span><span class="">mkHeader</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">HdrConnection</span> <span class="" style="color:rgb(221,17,68)">"close"</span><span class="">]</span></div>

<div class="" id="LC24" style="padding-left:10px">                                                  <span class="">(</span><span class="">encodeUtf8</span> <span class="" style="font-weight:bold">$</span> <span class="">pack</span> <span class="" style="font-weight:bold">$</span> <span class="">show</span> <span class="">failure</span><span class="">)</span></div>

<div class="" id="LC25" style="padding-left:10px"><br></div><div class="" id="LC26" style="padding-left:10px"><span class="" style="color:rgb(153,0,0);font-weight:bold">handle_valid_request</span> <span class="">request</span> <span class="" style="font-weight:bold">=</span> <span class="" style="font-weight:bold">do</span></div>

<div class="" id="LC27" style="padding-left:10px">    <span class="" style="font-weight:bold">let</span> <span class="">request_body</span> <span class="" style="font-weight:bold">=</span> <span class="">rqBody</span> <span class="">request</span></div>

<div class="" id="LC28" style="padding-left:10px">    <span class="">return</span> <span class="" style="font-weight:bold">$</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">Response</span> <span class="">(</span><span class="" style="color:rgb(0,153,153)">2</span><span class="">,</span><span class="" style="color:rgb(0,153,153)">0</span><span class="">,</span><span class="" style="color:rgb(0,153,153)">0</span><span class="">)</span> <span class="" style="color:rgb(221,17,68)">"OK"</span> <span class="">[</span><span class="">mkHeader</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">HdrConnection</span> <span class="" style="color:rgb(221,17,68)">"close"</span><span class="">]</span></div>

<div class="" id="LC29" style="padding-left:10px">                      <span class="">(</span><span class="">encodeUtf8</span> <span class="" style="font-weight:bold">$</span> <span class="">pack</span> <span class="" style="font-weight:bold">$</span> <span class="">show</span> <span class="">request_body</span><span class="">)</span></div>

<div class="" id="LC30" style="padding-left:10px"><br></div><div class="" id="LC31" style="padding-left:10px"><span class="" style="color:rgb(153,0,0);font-weight:bold">dispatch_on_accept</span> <span class="">http_socket</span> <span class="">handler</span> <span class="" style="font-weight:bold">=</span> <span class="">forever</span> <span class="" style="font-weight:bold">$</span> <span class="">accept</span> <span class="">http_socket</span> <span class="" style="font-weight:bold">>>=</span> <span class="">forkIO</span> <span class="" style="font-weight:bold">.</span> <span class="">httpHandler</span> <span class="" style="font-weight:bold">.</span> <span class="">fst</span></div>

<div class="" id="LC32" style="padding-left:10px">    <span class="" style="font-weight:bold">where</span></div><div class="" id="LC33" style="padding-left:10px">        <span class="">httpHandler</span> <span class="">client_socket</span> <span class="" style="font-weight:bold">=</span> <span class="">bracket</span> <span class="">(</span><span class="">socketConnection</span> <span class="" style="color:rgb(221,17,68)">"client"</span> <span class="" style="color:rgb(0,153,153)">0</span> <span class="">client_socket</span><span class="">)</span></div>

<div class="" id="LC34" style="padding-left:10px">                                            <span class="" style="color:rgb(68,85,136);font-weight:bold">Network</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(68,85,136);font-weight:bold">HTTP</span><span class="" style="font-weight:bold">.</span><span class="">close</span></div>

<div class="" id="LC35" style="padding-left:10px">                                            <span class="">client_interact</span></div><div class="" id="LC36" style="padding-left:10px">        <span class="">client_interact</span> <span class="" style="font-weight:bold">::</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">HandleStream</span> <span class="" style="color:rgb(68,85,136);font-weight:bold">BS</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(68,85,136);font-weight:bold">ByteString</span> <span class="" style="font-weight:bold">-></span> <span class="" style="color:rgb(68,85,136);font-weight:bold">IO</span> <span class="" style="color:rgb(0,134,179)">()</span></div>

<div class="" id="LC37" style="padding-left:10px">        <span class="">client_interact</span> <span class="">byte_stream</span> <span class="" style="font-weight:bold">=</span> <span class="">receiveHTTP</span> <span class="">byte_stream</span> <span class="" style="font-weight:bold">>>=</span> <span class="">handler</span> <span class="" style="font-weight:bold">>>=</span> <span class="">respondHTTP</span> <span class="">byte_stream</span></div>

</pre></div></pre></div><div class="gmail_default" style="font-family:'courier new',monospace"><br></div><div><div dir="ltr"><font face="courier new, monospace">-Corey O'Connor<br><a href="mailto:coreyoconnor@gmail.com" target="_blank">coreyoconnor@gmail.com</a><br>

<a href="http://corebotllc.com/" target="_blank">http://corebotllc.com/</a></font></div></div>
</div>