Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Reduced the number of calls to send on the socket.

  • Loading branch information...
commit 0ae73e9699e99a5bd94159b3670b7b8201f3d34b 1 parent 5430f45
@tibbe tibbe authored
Showing with 18 additions and 12 deletions.
  1. +18 −12 Hyena/Http.hs
View
30 Hyena/Http.hs
@@ -88,10 +88,10 @@ blockSize = 4 * 1024
sendResponse :: Socket -> Response -> IO ()
sendResponse sock resp = do
-- TODO: Check if all data was sent.
- send sock $ (C.pack $ "HTTP/1.1 ")
- send sock $ (C.pack $ show (statusCode resp) ++ " "
- ++ (C.unpack $ reasonPhrase resp))
- send sock $ C.pack "\r\n"
+ send sock $ S.concat [C.pack $ "HTTP/1.1 "
+ ,(C.pack $ show (statusCode resp) ++ " "
+ ++(C.unpack $ reasonPhrase resp))
+ ,C.pack "\r\n"]
sendHeaders sock (responseHeaders resp)
send sock $ C.pack "\r\n"
(responseBody resp) (sendMessageBody sock) ()
@@ -100,17 +100,24 @@ sendResponse sock resp = do
-- TODO: Check if all bytes were sent, otherwise retry.
-- | 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 ())
-- | Send headers over socket.
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"
+sendHeaders sock headers = do
+ send sock $ S.concat $ map go headers
+ return ()
+ where go (k, v) = S.concat [k, C.pack ": "
+ ,v, 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
-- 'Nothing' is returned.
@@ -121,7 +128,6 @@ receiveRequest sock = do
Nothing -> return Nothing
Just (req, bs) ->
let len = contentLength req
- -- TODO: Add length?
rest = bytesEnum bs
enum = case len of
Just n -> partialSocketEnum sock n
Please sign in to comment.
Something went wrong with that request. Please try again.