summaryrefslogtreecommitdiff
path: root/Utility
diff options
context:
space:
mode:
authorGravatar Joey Hess <joeyh@joeyh.name>2015-11-09 12:19:10 -0400
committerGravatar Joey Hess <joeyh@joeyh.name>2015-11-09 12:19:23 -0400
commit7e9c52ef3975128798caafaafc5fb0ecd5dd2107 (patch)
tree7fceb2a6e923ddd5cfc83262ab2245afc8794369 /Utility
parent7f572b6830b81fff96f36f2ca88d5255d48f4ba1 (diff)
quvi may output utf-8 encoded data when the conifigured locale doesn't support that; avoid crashing on such invalid encoding.
Diffstat (limited to 'Utility')
-rw-r--r--Utility/Quvi.hs29
1 files changed, 21 insertions, 8 deletions
diff --git a/Utility/Quvi.hs b/Utility/Quvi.hs
index 2aacfaea2..09f74968b 100644
--- a/Utility/Quvi.hs
+++ b/Utility/Quvi.hs
@@ -14,7 +14,8 @@ import Common
import Utility.Url
import Data.Aeson
-import Data.ByteString.Lazy.UTF8 (fromString)
+import qualified Data.ByteString as B
+import qualified Data.ByteString.Lazy as BL
import qualified Data.Map as M
import Network.URI (uriAuthority, uriRegName)
import Data.Char
@@ -77,8 +78,8 @@ type Query a = QuviVersion -> [CommandParam] -> URLString -> IO a
forceQuery :: Query (Maybe Page)
forceQuery v ps url = query' v ps url `catchNonAsync` onerr
where
- onerr _ = ifM (inPath "quvi")
- ( error "quvi failed"
+ onerr e = ifM (inPath "quvi")
+ ( error ("quvi failed: " ++ show e)
, error "quvi is not installed"
)
@@ -89,9 +90,11 @@ query v ps url = flip catchNonAsync (const $ return Nothing) (query' v ps url)
query' :: Query (Maybe Page)
query' Quvi09 ps url = parseEnum
- <$> readProcess "quvi" (toCommand $ [Param "dump", Param "-p", Param "enum"] ++ ps ++ [Param url])
-query' Quvi04 ps url = decode . fromString
- <$> readProcess "quvi" (toCommand $ ps ++ [Param url])
+ <$> readQuvi (toCommand $ [Param "dump", Param "-p", Param "enum"] ++ ps ++ [Param url])
+query' Quvi04 ps url = do
+ let p = proc "quvi" (toCommand $ ps ++ [Param url])
+ decode . BL.fromStrict
+ <$> withHandle StdoutHandle createProcessSuccess p B.hGetContents
query' NoQuvi _ _ = return Nothing
queryLinks :: Query [URLString]
@@ -131,8 +134,7 @@ listdomains :: QuviVersion -> IO [String]
listdomains Quvi09 = concatMap (split ",")
. concatMap (drop 1 . words)
. filter ("domains: " `isPrefixOf`) . lines
- <$> readProcess "quvi"
- (toCommand [Param "info", Param "-p", Param "domains"])
+ <$> readQuvi (toCommand [Param "info", Param "-p", Param "domains"])
listdomains _ = return []
type QuviParams = QuviVersion -> [CommandParam]
@@ -150,3 +152,14 @@ httponly :: QuviParams
-- No way to do it with 0.9?
httponly Quvi04 = [Param "-c", Param "http"]
httponly _ = [] -- No way to do it with 0.9?
+
+{- Both versions of quvi will output utf-8 encoded data even when
+ - the locale doesn't support it. -}
+readQuvi :: [String] -> IO String
+readQuvi ps = withHandle StdoutHandle createProcessSuccess p $ \h -> do
+ fileEncoding h
+ r <- hGetContentsStrict h
+ hClose h
+ return r
+ where
+ p = proc "quvi" ps