]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - Network/HTTP/Lucu/SocketLike.hs
lots of bugfixes regarding SSL support
[Lucu.git] / Network / HTTP / Lucu / SocketLike.hs
index 998e4490761bfb6324f6d5bb03a47073ec5c25c4..580ee529fdd139d2726a5a5d79435e713b252a3f 100644 (file)
@@ -10,10 +10,14 @@ module Network.HTTP.Lucu.SocketLike
     ( SocketLike(..)
     )
     where
+#if defined(HAVE_SSL)
+import Control.Exception
+#endif
 import qualified Network.Socket as So
 import Network.HTTP.Lucu.HandleLike
 #if defined(HAVE_SSL)
 import qualified OpenSSL.Session as SSL
+import Prelude hiding (catch)
 import Prelude.Unicode
 #endif
 import qualified System.IO as I
@@ -40,8 +44,26 @@ instance SocketLike (SSL.SSLContext, So.Socket) where
     accept (ctx, soSelf)
         = do (soPeer, addr) ← So.accept soSelf
              ssl            ← SSL.connection ctx soPeer
-             SSL.accept ssl
-             return (ssl, addr)
+             handshake ssl addr `catch` next ssl addr
+        where
+          handshake ∷ SSL.SSL → So.SockAddr → IO (SSL.SSL, So.SockAddr)
+          handshake ssl addr
+              = do SSL.accept ssl
+                   return (ssl, addr)
+
+          next ∷ SSL.SSL
+               → So.SockAddr
+               → SSL.SomeSSLException
+               → IO (SSL.SSL, So.SockAddr)
+          next ssl addr e
+              = do I.hPutStrLn I.stderr
+                       $ "Lucu: failed to accept an SSL connection from "
+                       ⧺ show addr
+                       ⧺ ":"
+                   I.hPutStrLn I.stderr
+                       $ show e
+                   SSL.shutdown ssl SSL.Bidirectional
+                   accept (ctx, soSelf)
 
     socketPort = So.socketPort ∘ snd
 #endif