Skip to content

Commit

Permalink
Reduced the number of calls to send on the socket.
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbe committed Sep 15, 2008
1 parent 5430f45 commit 0ae73e9
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions Hyena/Http.hs
Expand Up @@ -88,10 +88,10 @@ blockSize = 4 * 1024
sendResponse :: Socket -> Response -> IO () sendResponse :: Socket -> Response -> IO ()
sendResponse sock resp = do sendResponse sock resp = do
-- TODO: Check if all data was sent. -- TODO: Check if all data was sent.
send sock $ (C.pack $ "HTTP/1.1 ") send sock $ S.concat [C.pack $ "HTTP/1.1 "
send sock $ (C.pack $ show (statusCode resp) ++ " " ,(C.pack $ show (statusCode resp) ++ " "
++ (C.unpack $ reasonPhrase resp)) ++(C.unpack $ reasonPhrase resp))
send sock $ C.pack "\r\n" ,C.pack "\r\n"]
sendHeaders sock (responseHeaders resp) sendHeaders sock (responseHeaders resp)
send sock $ C.pack "\r\n" send sock $ C.pack "\r\n"
(responseBody resp) (sendMessageBody sock) () (responseBody resp) (sendMessageBody sock) ()
Expand All @@ -100,17 +100,24 @@ sendResponse sock resp = do
-- TODO: Check if all bytes were sent, otherwise retry. -- TODO: Check if all bytes were sent, otherwise retry.


-- | Iteratee used for sending message body over socket. -- | Iteratee used for sending message body over socket.
sendMessageBody :: Socket -> () -> S.ByteString -> IO (Either() ()) sendMessageBody :: Socket -> () -> S.ByteString -> IO (Either () ())
sendMessageBody sock _ bs = send sock bs >> return (Right ()) sendMessageBody sock _ bs = send sock bs >> return (Right ())


-- | Send headers over socket. -- | Send headers over socket.
sendHeaders :: Socket -> Headers -> IO () sendHeaders :: Socket -> Headers -> IO ()
sendHeaders sock headers = sendHeaders sock headers = do
forM_ headers $ \(k, v) -> do send sock $ S.concat $ map go headers
send sock k return ()
send sock $ C.pack ": " where go (k, v) = S.concat [k, C.pack ": "
send sock v ,v, C.pack "\r\n"]
send sock $ C.pack "\r\n"
-- sendHeaders :: Socket -> Headers -> IO ()
-- sendHeaders sock headers =
-- forM_ headers $ \(k, v) -> do
-- send sock k
-- send sock $ C.pack ": "
-- send sock v
-- send sock $ C.pack "\r\n"


-- | Receive request from socket. If the request is malformed -- | Receive request from socket. If the request is malformed
-- 'Nothing' is returned. -- 'Nothing' is returned.
Expand All @@ -121,7 +128,6 @@ receiveRequest sock = do
Nothing -> return Nothing Nothing -> return Nothing
Just (req, bs) -> Just (req, bs) ->
let len = contentLength req let len = contentLength req
-- TODO: Add length?
rest = bytesEnum bs rest = bytesEnum bs
enum = case len of enum = case len of
Just n -> partialSocketEnum sock n Just n -> partialSocketEnum sock n
Expand Down

0 comments on commit 0ae73e9

Please sign in to comment.