on master help sign in
runOnException, serveConnections
commitf0f3409c4bd7b7bc6452eb6241c70387780f67d9 authorMichael Snoyman <michael@snoyman.com> dateTue 25 Jan 2011, 21:00 (16 years ago) parent164fed3
2 files changed+38−15
Network/Wai/Handler/Warp.hs+37−14 warp.cabal+1−1
Network/Wai/Handler/Warp.hs+37−14
@@ -4,7 +4,7 @@
44{-# LANGUAGE CPP #-}
55{-# LANGUAGE BangPatterns #-}
66---------------------------------------------------------
7−-- |
7+--
88-- Module : Network.Wai.Handler.Warp
99-- Copyright : Michael Snoyman
1010-- License : BSD3
@@ -16,15 +16,26 @@
1616-- A fast, light-weight HTTP server handler for WAI.
1717--
1818---------------------------------------------------------
19+
20+-- | A fast, light-weight HTTP server handler for WAI. Some random notes (a FAQ, if you will):
21+--
22+-- * When a 'ResponseFile' indicates a file which does not exist, an exception
23+-- is thrown. This will close the connection to the client as well. You should
24+-- handle file existance checks at the application level.
1925module Network.Wai.Handler.Warp
20− ( run
26+ ( -- * Run a Warp server
27+ run
28+ , runOnException
29+ , serveConnections
30+ -- * Datatypes
2131 , Port
32+ , InvalidRequest (..)
33+ -- * Utility functions for other packages
2234 , sendResponse
2335 , parseRequest
2436#if TEST
2537 , takeLineMax
2638 , takeHeaders
27− , InvalidRequest (..)
2839#endif
2940 ) where
3041
@@ -69,30 +80,42 @@ import System.Timeout (timeout)
6980import Data.Word (Word8)
7081import Data.List (foldl')
7182
83+-- | Run an 'Application' on the given port, ignoring all exceptions.
7284run :: Port -> Application -> IO ()
73−run port = withSocketsDo . -- FIXME should this be called by client user instead?
85+run = runOnException print
86+
87+-- | Run an 'Application' on the given port, with the given exception handler.
88+-- Please note that you will also receive 'InvalidRequest' exceptions.
89+runOnException :: (SomeException -> IO ()) -> Port -> Application -> IO ()
90+runOnException onE port = withSocketsDo . -- FIXME should this be called by client user instead?
7491 bracket
7592 (listenOn $ PortNumber $ fromIntegral port)
7693 sClose .
77− serveConnections port
94+ serveConnections onE port
95+
7896type Port = Int
7997
80−serveConnections :: Port -> Application -> Socket -> IO ()
81−serveConnections port app socket = do
98+-- | Runs a server, listening on the given socket. The user is responsible for
99+-- closing the socket after 'runWithSocket' completes. You must also supply a
100+-- 'Port' argument for use in the 'serverPort' record; however, this field is
101+-- only used for informational purposes. If you are in fact listening on a
102+-- non-TCP socket, this can be a ficticious value.
103+serveConnections :: (SomeException -> IO ())
104+ -> Port -> Application -> Socket -> IO ()
105+serveConnections onE port app socket = do
82106 (conn, sa) <- accept socket
83− _ <- forkIO $ serveConnection port app conn sa
84− serveConnections port app socket
107+ _ <- forkIO $ serveConnection onE port app conn sa
108+ serveConnections onE port app socket
85109
86−serveConnection :: Port -> Application -> Socket -> SockAddr -> IO ()
87−serveConnection port app conn remoteHost' = do
110+serveConnection :: (SomeException -> IO ())
111+ -> Port -> Application -> Socket -> SockAddr -> IO ()
112+serveConnection onException port app conn remoteHost' = do
88113 catch
89114 (finally
90115 (E.run_ $ fromClient $$ serveConnection')
91116 (sClose conn))
92− ignoreAll
117+ onException
93118 where
94− ignoreAll :: SomeException -> IO ()
95− ignoreAll _ = return ()
96119 fromClient = enumSocket bytesPerRead conn
97120 serveConnection' = do
98121 (enumeratee, env) <- parseRequest port remoteHost'
warp.cabal+1−1
@@ -1,5 +1,5 @@
11Name: warp
2−Version: 0.3.0.1
2+Version: 0.3.1
33Synopsis: A fast, light-weight web server for WAI applications.
44License: BSD3
55License-file: LICENSE