X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=Network%2FHTTP%2FLucu%2FHttpd.hs;h=bab8d72ff5639362d1aee310ef7e189c5fc4bf84;hb=9ac730212cb361eb10e5fe4ad0eec6758e2b200a;hp=9632b298193e30a24b4711519896be32b3fa72dd;hpb=1e53b8533fa22640147cc4ca4ce5075c8e39b0d8;p=Lucu.git diff --git a/Network/HTTP/Lucu/Httpd.hs b/Network/HTTP/Lucu/Httpd.hs index 9632b29..bab8d72 100644 --- a/Network/HTTP/Lucu/Httpd.hs +++ b/Network/HTTP/Lucu/Httpd.hs @@ -13,6 +13,7 @@ import Network.HTTP.Lucu.Interaction import Network.HTTP.Lucu.RequestReader import Network.HTTP.Lucu.Resource.Tree import Network.HTTP.Lucu.ResponseWriter +import qualified OpenSSL.Session as SSL import System.IO import System.Posix.Signals @@ -52,22 +53,43 @@ import System.Posix.Signals runHttpd :: Config -> ResTree -> [FallbackHandler] -> IO () runHttpd cnf tree fbs = withSocketsDo $ - do installHandler sigPIPE Ignore Nothing - so <- listenOn (cnfServerPort cnf) - loop so + do _ <- installHandler sigPIPE Ignore Nothing + + case cnfSSLConfig cnf of + Nothing + -> return () + Just scnf + -> do so <- listenOn (sslServerPort scnf) + _loopTID <- forkIO $ httpsLoop (sslContext scnf) so + return () + + httpLoop =<< listenOn (cnfServerPort cnf) where - loop :: Socket -> IO () - loop so - -- 本當は Network.accept を使ひたいが、このアクションは勝手に - -- リモートのIPを逆引きするので、使へない。 - = do (h, addr) <- accept' so - tQueue <- newInteractionQueue - readerTID <- forkIO $ requestReader cnf tree fbs h addr tQueue - writerTID <- forkIO $ responseWriter cnf h tQueue readerTID - loop so + httpLoop :: Socket -> IO () + httpLoop so + = do (h, addr) <- acceptHTTP so + tQueue <- newInteractionQueue + readerTID <- forkIO $ requestReader cnf tree fbs h addr tQueue + _writerTID <- forkIO $ responseWriter cnf h tQueue readerTID + httpLoop so + + httpsLoop :: SSL.SSLContext -> Socket -> IO () + httpsLoop ctx so + = do (ssl, addr) <- acceptHTTPS ctx so + tQueue <- newInteractionQueue + readerTID <- forkIO $ requestReader cnf tree fbs ssl addr tQueue + _writerTID <- forkIO $ responseWriter cnf ssl tQueue readerTID + httpsLoop ctx so - accept' :: Socket -> IO (Handle, So.SockAddr) - accept' soSelf + acceptHTTP :: Socket -> IO (Handle, So.SockAddr) + acceptHTTP soSelf = do (soPeer, addr) <- So.accept soSelf hPeer <- So.socketToHandle soPeer ReadWriteMode return (hPeer, addr) + + acceptHTTPS :: SSL.SSLContext -> Socket -> IO (SSL.SSL, So.SockAddr) + acceptHTTPS ctx so + = do (so', addr) <- So.accept so + ssl <- SSL.connection ctx so' + SSL.accept ssl + return (ssl, addr)