@@ -43,9 +43,9 @@ module Network.Wai.Handler.WarpTLS (
4343 ) where
4444
4545import Control.Applicative ((<|>))
46−import Control.Exception (Exception, throwIO, bracket, finally, handle, fromException, try, IOException, onException, SomeException(..))
46+import Control.Exception (Exception, throwIO, bracket, finally, handle, fromException, try, IOException, onException, SomeException(..), handleJust)
4747import qualified Control.Exception as E
48−import Control.Monad (void)
48+import Control.Monad (void, guard)
4949import qualified Data.ByteString as S
5050import qualified Data.ByteString.Lazy as L
5151import Data.Default.Class (def)
@@ -344,9 +344,17 @@ httpOverTls TLSSettings{..} s bs0 params = do
344344 sendfile fid offset len hook headers =
345345 readSendFile writeBuf bufferSize sendall fid offset len hook headers
346346
347− close' = void (tryIO $ TLS.bye ctx) `finally`
347+ close' = void (tryIO sendBye) `finally`
348348 TLS.contextClose ctx
349349
350+ sendBye =
351+ -- It's fine if the connection was closed by the other side before
352+ -- receiving close_notify, see RFC 5246 section 7.2.1.
353+ handleJust
354+ (\e -> guard (e == ConnectionClosedByPeer) >> return e)
355+ (const (return ()))
356+ (TLS.bye ctx)
357+
350358 -- TLS version of recv with a cache for leftover input data.
351359 -- The cache is shared with recvBuf.
352360 recv cref = do