Skip to content

Commit

Permalink
Handle ab's broken HTTP 1.0 "keepalive".
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Mar 24, 2010
1 parent 44eb979 commit a9caaf1
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions benchmarks/StaticHttp.hs
Expand Up @@ -2,7 +2,7 @@

import Control.Concurrent (forkIO, runInUnboundThread)
import Control.Exception (bracket, finally)
import Control.Monad (unless)
import Control.Monad (unless, when)
import Control.Monad.Fix (fix)
import qualified Data.Attoparsec as A
import qualified Data.ByteString.Char8 as B
Expand Down Expand Up @@ -66,13 +66,25 @@ client sock = (`finally` sClose sock) loop
(bs, ereq) <- parseM (recv sock 4096) request
case ereq of
Right (req,hdrs) | requestMethod req == "GET" -> do
let http10 = requestVersion req == "1.0"
connection = lookupHeader "Connection" hdrs
keepAlive = (http10 && connection == ["Keep-Alive"]) ||
connection /= ["Close"]
bracket (openFd (B.unpack (requestUri req)) ReadOnly Nothing
defaultFileFlags{nonBlock=True}) closeFd $ \fd -> do
st <- getFdStatus fd
let fixedHeaders = B.intercalate "\r\n" [
"HTTP/1.1 200 OK"
, "Content-type: application/octet-stream"
]
let fixedHeaders
| http10 && keepAlive =
B.intercalate "\r\n" [
"HTTP/1.0 200 OK"
, "Content-type: application/octet-stream"
, "Connection: Keep-Alive"
]
| otherwise =
B.intercalate "\r\n" [
"HTTP/1.1 200 OK"
, "Content-type: application/octet-stream"
]
withNoPush sock $ do
sendAll sock $! (`B.append` "\r\n\r\n") $ B.intercalate "\r\n" [
fixedHeaders
Expand All @@ -81,6 +93,6 @@ client sock = (`finally` sClose sock) loop
fix $ \sendLoop -> do
s <- F.read fd 16384
unless (B.null s) $ sendAll sock s >> sendLoop
unless (requestVersion req == "1.0") loop
when keepAlive loop
err | B.null bs -> return ()
| otherwise -> print err >> sendAll sock "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n"

0 comments on commit a9caaf1

Please sign in to comment.