module Debian.URI
( module Network.URI
, URI'
, toURI'
, fromURI'
, readURI'
, uriToString'
, fileFromURI
, fileFromURIStrict
, dirFromURI
) where
import Control.Exception (SomeException, try)
import Data.ByteString.Lazy.UTF8 as L
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Maybe (catMaybes, fromJust)
import Network.URI (URI(..), URIAuth(..), parseURI, uriToString)
import System.Directory (getDirectoryContents)
import System.Process.ByteString.Lazy (readProcessWithExitCode)
import Text.Regex (mkRegex, matchRegex)
newtype URI' = URI' String deriving (Read, Show, Eq, Ord)
readURI' :: String -> Maybe URI'
readURI' s = maybe Nothing (const (Just (URI' s))) (parseURI s)
fromURI' :: URI' -> URI
fromURI' (URI' s) = fromJust (parseURI s)
toURI' :: URI -> URI'
toURI' = URI' . show
uriToString' :: URI -> String
uriToString' uri = uriToString id uri ""
fileFromURI :: URI -> IO (Either SomeException L.ByteString)
fileFromURI uri = fileFromURIStrict uri
fileFromURIStrict :: URI -> IO (Either SomeException L.ByteString)
fileFromURIStrict uri = try $
case (uriScheme uri, uriAuthority uri) of
("file:", Nothing) -> L.readFile (uriPath uri)
("ssh:", Just auth) -> do
let cmd = "ssh"
args = [uriUserInfo auth ++ uriRegName auth ++ uriPort auth, "cat", uriPath uri]
(_code, out, _err) <- readProcessWithExitCode cmd args L.empty
return out
_ -> do
let cmd = "curl"
args = ["-s", "-g", uriToString' uri]
(_code, out, _err) <- readProcessWithExitCode cmd args L.empty
return out
webServerDirectoryContents :: L.ByteString -> [String]
webServerDirectoryContents text =
catMaybes . map (second . matchRegex re) . Prelude.lines . L.toString $ text
where
re = mkRegex "( <A HREF|<a href)=\"([^/][^\"]*)/\""
second (Just [_, b]) = Just b
second _ = Nothing
dirFromURI :: URI -> IO (Either SomeException [String])
dirFromURI uri = try $
case (uriScheme uri, uriAuthority uri) of
("file:", Nothing) -> getDirectoryContents (uriPath uri)
("ssh:", Just auth) ->
do let cmd = "ssh"
args = [uriUserInfo auth ++ uriRegName auth ++ uriPort auth, "ls", "-1", uriPath uri]
(_code, out, _err) <- readProcessWithExitCode cmd args L.empty
return . Prelude.lines . L.toString $ out
_ ->
do let cmd = "curl"
args = ["-s", "-g", uriToString' uri]
(_code, out, _err) <- readProcessWithExitCode cmd args L.empty
return . webServerDirectoryContents $ out