-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathSocket.hs
More file actions
201 lines (183 loc) · 8.07 KB
/
Socket.hs
File metadata and controls
201 lines (183 loc) · 8.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Snap.Internal.Http.Server.Socket
( bindSocket
, bindSocketImpl
, bindUnixSocket
, httpAcceptFunc
, haProxyAcceptFunc
, sendFileFunc
, acceptAndInitialize
) where
------------------------------------------------------------------------------
import Control.Exception (bracketOnError, finally, throwIO)
import Control.Monad (when)
import Data.Bits (complement, (.&.))
import Data.ByteString.Char8 (ByteString)
import Network.Socket (Socket, SocketOption (NoDelay, ReuseAddr), accept, close, getSocketName, setSocketOption, socket)
import qualified Network.Socket as N
#ifdef HAS_SENDFILE
import Network.Socket (fdSocket)
import System.Posix.IO (OpenMode (..), closeFd, defaultFileFlags, openFd)
import System.Posix.Types (Fd (..))
import System.SendFile (sendFile, sendHeaders)
#else
import Data.ByteString.Builder (byteString)
import Data.ByteString.Builder.Extra (flush)
import Network.Socket.ByteString (sendAll)
#endif
#ifdef HAS_UNIX_SOCKETS
import Control.Exception (bracket)
import qualified Control.Exception as E (catch)
import System.FilePath (isRelative)
import System.IO.Error (isDoesNotExistError)
import System.Posix.Files (accessModes, removeLink, setFileCreationMask)
#endif
------------------------------------------------------------------------------
import qualified System.IO.Streams as Streams
------------------------------------------------------------------------------
import Snap.Internal.Http.Server.Address (AddressNotSupportedException (..), getAddress, getSockAddr)
import Snap.Internal.Http.Server.Types (AcceptFunc (..), SendFileHandler)
import qualified System.IO.Streams.Network.HAProxy as HA
------------------------------------------------------------------------------
bindSocket :: ByteString -> Int -> IO Socket
bindSocket = bindSocketImpl setSocketOption bind N.listen
where
#if MIN_VERSION_network(2,7,0)
bind = N.bind
#else
bind = N.bindSocket
#endif
{-# INLINE bindSocket #-}
------------------------------------------------------------------------------
bindSocketImpl
:: (Socket -> SocketOption -> Int -> IO ()) -- ^ mock setSocketOption
-> (Socket -> N.SockAddr -> IO ()) -- ^ bindSocket
-> (Socket -> Int -> IO ()) -- ^ listen
-> ByteString
-> Int
-> IO Socket
bindSocketImpl _setSocketOption _bindSocket _listen bindAddr bindPort = do
(family, addr) <- getSockAddr bindPort bindAddr
bracketOnError (socket family N.Stream 0) N.close $ \sock -> do
_setSocketOption sock ReuseAddr 1
_setSocketOption sock NoDelay 1
_bindSocket sock addr
_listen sock 150
return $! sock
bindUnixSocket :: Maybe Int -> String -> IO Socket
#if HAS_UNIX_SOCKETS
bindUnixSocket mode path = do
when (isRelative path) $
throwIO $ AddressNotSupportedException
$! "Refusing to bind unix socket to non-absolute path: " ++ path
bracketOnError (socket N.AF_UNIX N.Stream 0) N.close $ \sock -> do
E.catch (removeLink path) $ \e -> when (not $ isDoesNotExistError e) $ throwIO e
case mode of
Nothing -> bind sock (N.SockAddrUnix path)
Just mode' -> bracket (setFileCreationMask $ modeToMask mode')
setFileCreationMask
(const $ bind sock (N.SockAddrUnix path))
N.listen sock 150
return $! sock
where
#if MIN_VERSION_network(2,7,0)
bind = N.bind
#else
bind = N.bindSocket
#endif
modeToMask p = accessModes .&. complement (fromIntegral p)
#else
bindUnixSocket _ path = throwIO (AddressNotSupportedException $ "unix:" ++ path)
#endif
------------------------------------------------------------------------------
-- TODO(greg): move buffer size configuration into config
bUFSIZ :: Int
bUFSIZ = 4064
------------------------------------------------------------------------------
acceptAndInitialize :: Socket -- ^ bound socket
-> (forall b . IO b -> IO b)
-> ((Socket, N.SockAddr) -> IO a)
-> IO a
acceptAndInitialize boundSocket restore f =
bracketOnError (restore $ accept boundSocket)
(close . fst)
f
------------------------------------------------------------------------------
haProxyAcceptFunc :: Socket -- ^ bound socket
-> AcceptFunc
haProxyAcceptFunc boundSocket =
AcceptFunc $ \restore ->
acceptAndInitialize boundSocket restore $ \(sock, saddr) -> do
(readEnd, writeEnd) <- Streams.socketToStreamsWithBufferSize
bUFSIZ sock
localPInfo <- HA.socketToProxyInfo sock saddr
pinfo <- HA.decodeHAProxyHeaders localPInfo readEnd
(localPort, localHost) <- getAddress $ HA.getDestAddr pinfo
(remotePort, remoteHost) <- getAddress $ HA.getSourceAddr pinfo
let cleanup = Streams.write Nothing writeEnd
`finally` close sock
return $! ( sendFileFunc sock
, localHost
, localPort
, remoteHost
, remotePort
, readEnd
, writeEnd
, cleanup
)
------------------------------------------------------------------------------
httpAcceptFunc :: Socket -- ^ bound socket
-> AcceptFunc
httpAcceptFunc boundSocket =
AcceptFunc $ \restore ->
acceptAndInitialize boundSocket restore $ \(sock, remoteAddr) -> do
localAddr <- getSocketName sock
(localPort, localHost) <- getAddress localAddr
(remotePort, remoteHost) <- getAddress remoteAddr
(readEnd, writeEnd) <- Streams.socketToStreamsWithBufferSize bUFSIZ
sock
let cleanup = Streams.write Nothing writeEnd
`finally` close sock
return $! ( sendFileFunc sock
, localHost
, localPort
, remoteHost
, remotePort
, readEnd
, writeEnd
, cleanup
)
------------------------------------------------------------------------------
sendFileFunc :: Socket -> SendFileHandler
#ifdef HAS_SENDFILE
sendFileFunc sock !_ builder fPath offset nbytes = bracket acquire closeFd go
where
acquire = openFd fPath ReadOnly Nothing defaultFileFlags
go fileFd = do
#if MIN_VERSION_network(3,0,0)
sockFd <- Fd `fmap` fdSocket sock
#else
let sockFd = Fd $ fdSocket sock
#endif
sendHeaders builder sockFd
sendFile sockFd fileFd offset nbytes
#else
sendFileFunc sock buffer builder fPath offset nbytes =
Streams.unsafeWithFileAsInputStartingAt (fromIntegral offset) fPath $
\fileInput0 -> do
fileInput <- Streams.takeBytes (fromIntegral nbytes) fileInput0 >>=
Streams.map byteString
input <- Streams.fromList [builder] >>=
flip Streams.appendInputStream fileInput
output <- Streams.makeOutputStream sendChunk >>=
Streams.unsafeBuilderStream (return buffer)
Streams.supply input output
Streams.write (Just flush) output
where
sendChunk (Just s) = sendAll sock s
sendChunk Nothing = return $! ()
#endif