@@ -4,7 +4,7 @@
4{-# LANGUAGE CPP #-}
4{-# LANGUAGE CPP #-}
5{-# LANGUAGE BangPatterns #-}
5{-# LANGUAGE BangPatterns #-}
6---------------------------------------------------------
6---------------------------------------------------------
8-- Module : Network.Wai.Handler.Warp
8-- Module : Network.Wai.Handler.Warp
9-- Copyright : Michael Snoyman
9-- Copyright : Michael Snoyman
10-- License : BSD3
10-- License : BSD3
@@ -16,15 +16,26 @@
16-- A fast, light-weight HTTP server handler for WAI.
16-- A fast, light-weight HTTP server handler for WAI.
18---------------------------------------------------------
18---------------------------------------------------------
20+-- | A fast, light-weight HTTP server handler for WAI. Some random notes (a FAQ, if you will):
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.
19module Network.Wai.Handler.Warp
25module Network.Wai.Handler.Warp
26+ ( -- * Run a Warp server
32+ , InvalidRequest (..)
33+ -- * Utility functions for other packages
22 , sendResponse
34 , sendResponse
23 , parseRequest
35 , parseRequest
25 , takeLineMax
37 , takeLineMax
26 , takeHeaders
38 , takeHeaders
27− , InvalidRequest (..)
@@ -69,30 +80,42 @@ import System.Timeout (timeout)
69import Data.Word (Word8)
80import Data.Word (Word8)
70import Data.List (foldl')
81import Data.List (foldl')
83+-- | Run an 'Application' on the given port, ignoring all exceptions.
72run :: Port -> Application -> IO ()
84run :: Port -> Application -> IO ()
85+run = runOnException print
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 ()
73−run port = withSocketsDo . -- FIXME should this be called by client user instead?
90+runOnException onE port = withSocketsDo . -- FIXME should this be called by client user instead?
75 (listenOn $ PortNumber $ fromIntegral port)
92 (listenOn $ PortNumber $ fromIntegral port)
77− serveConnections port
94+ serveConnections onE port
78type Port = Int
96type Port = Int
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 ())
80−serveConnections :: Port -> Application -> Socket -> IO ()
104+ -> Port -> Application -> Socket -> IO ()
81−serveConnections port app socket = do
105+serveConnections onE port app socket = do
82 (conn, sa) <- accept socket
106 (conn, sa) <- accept socket
83− _ <- forkIO $ serveConnection port app conn sa
107+ _ <- forkIO $ serveConnection onE port app conn sa
84− serveConnections port app socket
108+ serveConnections onE port app socket
110+serveConnection :: (SomeException -> IO ())
86−serveConnection :: Port -> Application -> Socket -> SockAddr -> IO ()
111+ -> Port -> Application -> Socket -> SockAddr -> IO ()
87−serveConnection port app conn remoteHost' = do
112+serveConnection onException port app conn remoteHost' = do
90 (E.run_ $ fromClient $$ serveConnection')
115 (E.run_ $ fromClient $$ serveConnection')
91 (sClose conn))
116 (sClose conn))
92− ignoreAll
117+ onException
94− ignoreAll :: SomeException -> IO ()
95− ignoreAll _ = return ()
96 fromClient = enumSocket bytesPerRead conn
119 fromClient = enumSocket bytesPerRead conn
97 serveConnection' = do
120 serveConnection' = do
98 (enumeratee, env) <- parseRequest port remoteHost'
121 (enumeratee, env) <- parseRequest port remoteHost'