1 {-# LANGUAGE CPP #-} 2 3 module Snap.Internal.Http.Server.ListenHelpers where 4 5 6 ------------------------------------------------------------------------------ 7 import Data.ByteString (ByteString) 8 import Foreign.C 9 import Network.Socket (Socket, sClose) 10 import Snap.Internal.Http.Server.Backend 11 import qualified Snap.Internal.Http.Server.HttpPort as Http 12 import qualified Snap.Internal.Http.Server.GnuTLS as TLS 13 14 15 ------------------------------------------------------------------------------ 16 listenSocket :: ListenSocket -> Socket 17 listenSocket (ListenHttp s) = s 18 listenSocket (ListenHttps s _ _) = s 19 20 21 ------------------------------------------------------------------------------ 22 isSecure :: ListenSocket -> Bool 23 isSecure (ListenHttp _) = False 24 isSecure (ListenHttps _ _ _) = True 25 26 27 ------------------------------------------------------------------------------ 28 closeSocket :: ListenSocket -> IO () 29 closeSocket (ListenHttp s) = sClose s 30 closeSocket p@(ListenHttps s _ _) = do TLS.freePort p 31 sClose s 32 33 34 ------------------------------------------------------------------------------ 35 createSession :: ListenSocket -> Int -> CInt -> IO () -> IO NetworkSession 36 createSession (ListenHttp _) = Http.createSession 37 createSession p@(ListenHttps _ _ _) = TLS.createSession p 38 39 40 ------------------------------------------------------------------------------ 41 endSession :: ListenSocket -> NetworkSession -> IO () 42 endSession (ListenHttp _) = Http.endSession 43 endSession (ListenHttps _ _ _) = TLS.endSession 44 45 46 #ifdef PORTABLE 47 48 49 -- For portable builds, we can't call read/write directly so we need the 50 -- original haskell socket to use with network-bytestring package. 51 -- Only the simple backend creates sockets in haskell so the following 52 -- functions only work with the simple backend. 53 54 55 ------------------------------------------------------------------------------ 56 recv :: ListenSocket -> Socket -> IO () -> NetworkSession 57 -> IO (Maybe ByteString) 58 recv (ListenHttp _) s = Http.recv s 59 recv (ListenHttps _ _ _) _ = TLS.recv 60 61 62 ------------------------------------------------------------------------------ 63 send :: ListenSocket -> Socket -> IO () -> IO () -> NetworkSession 64 -> ByteString -> IO () 65 send (ListenHttp _) s = Http.send s 66 send (ListenHttps _ _ _) _ = TLS.send 67 68 69 #else 70 71 72 ------------------------------------------------------------------------------ 73 recv :: ListenSocket -> IO () -> NetworkSession -> IO (Maybe ByteString) 74 recv (ListenHttp _) = Http.recv 75 recv (ListenHttps _ _ _) = TLS.recv 76 77 78 ------------------------------------------------------------------------------ 79 send :: ListenSocket -> IO () -> IO () -> NetworkSession -> ByteString 80 -> IO () 81 send (ListenHttp _) = Http.send 82 send (ListenHttps _ _ _) = TLS.send 83 84 #endif