|
1 | 1 | --------------------------------------------------------------------------------
|
2 | 2 | -- | Converts HTTP requests to Socket.IO requests
|
3 | 3 | {-# LANGUAGE OverloadedStrings #-}
|
4 |
| -module Web.SocketIO.Request (parseHTTPRequest) where |
| 4 | +module Web.SocketIO.Request (sourceRequest, parseHTTPRequest) where |
5 | 5 |
|
6 | 6 | --------------------------------------------------------------------------------
|
7 | 7 | import Web.SocketIO.Types
|
8 | 8 | import Web.SocketIO.Protocol
|
9 | 9 |
|
10 | 10 | --------------------------------------------------------------------------------
|
11 | 11 | import Control.Applicative ((<$>))
|
| 12 | +import Data.Conduit |
12 | 13 | import qualified Network.Wai as Wai
|
13 | 14 | import Network.HTTP.Types (Method)
|
14 | 15 |
|
| 16 | +sourceRequest :: Wai.Request -> Source IO Request |
| 17 | +sourceRequest request = do |
| 18 | + let path = parsePath (Wai.rawPathInfo request) |
| 19 | + let method = Wai.requestMethod request |
| 20 | + |
| 21 | + case (method, path) of |
| 22 | + ("GET", (WithoutSession _ _)) -> yield Handshake |
| 23 | + ("GET", (WithSession _ _ _ sessionID)) -> yield (Connect sessionID) |
| 24 | + ("POST", (WithSession _ _ _ sessionID)) -> do |
| 25 | + Wai.requestBody request $= parseMessage =$= filterMsgEvent sessionID |
| 26 | + (_, (WithSession _ _ _ sessionID)) -> yield (Disconnect sessionID) |
| 27 | + _ -> error "error handling http request" |
| 28 | + where filterMsgEvent sessionID = do |
| 29 | + message <- await |
| 30 | + case message of |
| 31 | + Just (MsgEvent _ _ event) -> yield (Emit sessionID event) |
| 32 | + _ -> return () |
15 | 33 | --------------------------------------------------------------------------------
|
16 | 34 | -- | Information of a HTTP reqeust we need
|
17 | 35 | type RequestInfo = (Method, Path, Framed Message)
|
|
0 commit comments