@@ -9,10 +9,13 @@ import Data.Text as T
9import Data.Text (Text)
9import Data.Text (Text)
10import Data.Text.Encoding (decodeUtf8, encodeUtf8)
10import Data.Text.Encoding (decodeUtf8, encodeUtf8)
11import Network.HTTP.Client (defaultManagerSettings, httpLbs,
11import Network.HTTP.Client (defaultManagerSettings, httpLbs,
12− method, newManager, parseUrl)
12+ method, newManager, parseUrl,
13+ managerRawConnection)
13import Network.HTTP.Types (Query, encodePath,
14import Network.HTTP.Types (Query, encodePath,
14 encodePathSegments)
15 encodePathSegments)
15−
16+import Network.HTTP.Client.Internal (makeConnection)
17+import qualified Network.Socket as S
18+import qualified Network.Socket.ByteString as SBS
16import Docker.Types
19import Docker.Types
18-- The reason we return Maybe Request is because the parseURL function
21-- The reason we return Maybe Request is because the parseURL function
@@ -35,12 +38,33 @@ defaultHttpHandler request = do
35 response <- httpLbs request manager
38 response <- httpLbs request manager
36 return response
39 return response
41+-- | Connect to a unix domain socket (the default docker socket is
42+-- at \/var\/run\/docker.sock)
44+-- Docker seems to ignore the hostname in requests sent over unix domain
45+-- sockets (and the port obviously doesn't matter either)
46+unixHttpHandler :: FilePath -- ^ The socket to connect to
48+unixHttpHandler fp request= do
49+ let mSettings = defaultManagerSettings
50+ { managerRawConnection = return $ openUnixSocket fp}
51+ manager <- newManager mSettings
52+ httpLbs request manager
54+ openUnixSocket filePath _ _ _ = do
55+ s <- S.socket S.AF_UNIX S.Stream S.defaultProtocol
56+ S.connect s (S.SockAddrUnix filePath)
57+ makeConnection (SBS.recv s 8096)
38encodeURL :: [Text] -> Text
61encodeURL :: [Text] -> Text
39encodeURL ps = decodeUtf8 $ toByteString $ encodePathSegments ps
62encodeURL ps = decodeUtf8 $ toByteString $ encodePathSegments ps
41encodeURLWithQuery :: [Text] -> Query -> Text
64encodeURLWithQuery :: [Text] -> Query -> Text
42encodeURLWithQuery ps q = decodeUtf8 $ toByteString $ encodePath ps q
65encodeURLWithQuery ps q = decodeUtf8 $ toByteString $ encodePath ps q
67+encodeQ :: String -> ByteString
44encodeQ = encodeUtf8 . T.pack
68encodeQ = encodeUtf8 . T.pack
46getEndpoint :: Endpoint -> Text
70getEndpoint :: Endpoint -> Text
@@ -74,4 +98,3 @@ getEndpoint (ContainerLogsEndpoint cid (LogOpts follow stdout stderr since times
74getEndpoint (DeleteContainerEndpoint (DeleteOpts removeVolumes force) cid) =
98getEndpoint (DeleteContainerEndpoint (DeleteOpts removeVolumes force) cid) =
75 encodeURLWithQuery ["containers", cid] query
99 encodeURLWithQuery ["containers", cid] query
76 where query = [("v", Just (encodeQ $ show removeVolumes)), ("force", Just (encodeQ $ show force))]
100 where query = [("v", Just (encodeQ $ show removeVolumes)), ("force", Just (encodeQ $ show force))]