hxt-http (empty) → 9.0.0
raw patch · 6 files changed
+391/−0 lines, 6 filesdep +HTTPdep +basedep +bytestringsetup-changed
Dependencies added: HTTP, base, bytestring, haskell98, hxt, network, parsec
Files
- LICENSE +9/−0
- Setup.hs +3/−0
- hxt-http.cabal +39/−0
- src/Text/XML/HXT/Arrow/LibHTTPInput.hs +87/−0
- src/Text/XML/HXT/HTTP.hs +27/−0
- src/Text/XML/HXT/IO/GetHTTPNative.hs +226/−0
+ LICENSE view
@@ -0,0 +1,9 @@+The MIT License++Copyright (c) 2005 Uwe Schmidt, Martin Schmidt, Torben Kuseler++Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,3 @@+#!/usr/bin/env runhaskell+import Distribution.Simple+main = defaultMain
+ hxt-http.cabal view
@@ -0,0 +1,39 @@+-- arch-tag: Haskell XML Toolbox main description file+Name: hxt-http+Version: 9.0.0+Synopsis: Interface to native Haskell HTTP package HTTP+Description: Interface to native Haskell HTTP package HTTP.+ This package can be used as alternative for the hxt-curl package+ for accessing documents via HTTP+License: OtherLicense+License-file: LICENSE+Author: Uwe Schmidt+Maintainer: Uwe Schmidt <uwe@fh-wedel.de>+Stability: Experimental+Category: XML+Homepage: http://www.fh-wedel.de/~si/HXmlToolbox/index.html+Copyright: Copyright (c) 2010 Uwe Schmidt+Build-type: Simple+Cabal-version: >=1.6++library+ exposed-modules:+ Text.XML.HXT.HTTP++ other-modules:+ Text.XML.HXT.Arrow.LibHTTPInput,+ Text.XML.HXT.IO.GetHTTPNative++ hs-source-dirs: src++ ghc-options: -Wall++ extensions:++ build-depends: base >= 4 && < 5,+ haskell98 >= 1 && < 2,+ parsec >= 2.1 && < 4,+ bytestring >= 0.9 && < 1,+ network >= 2.1 && < 3,+ HTTP >= 4000 && < 5000,+ hxt >= 9 && < 10
+ src/Text/XML/HXT/Arrow/LibHTTPInput.hs view
@@ -0,0 +1,87 @@+-- ------------------------------------------------------------++{- |+ Module : Text.XML.HXT.Arrow.LibCurlInput+ Copyright : Copyright (C) 2005 Uwe Schmidt+ License : MIT++ Maintainer : Uwe Schmidt (uwe@fh-wedel.de)+ Stability : experimental+ Portability: portable++ libcurl input+-}++-- ------------------------------------------------------------++module Text.XML.HXT.Arrow.LibHTTPInput+ ( getHTTPNativeContents+ , withHTTP+ , httpOptions+ )+where++import Control.Arrow -- arrow classes+import Control.Arrow.ArrowList+import Control.Arrow.ArrowTree+import Control.Arrow.ArrowIO++import System.Console.GetOpt++import Text.XML.HXT.Arrow.DocumentInput ( addInputError )+import Text.XML.HXT.IO.GetHTTPNative ( getCont )++import Text.XML.HXT.DOM.Interface++import Text.XML.HXT.Arrow.XmlArrow+import Text.XML.HXT.Arrow.XmlState+import Text.XML.HXT.Arrow.XmlState.TypeDefs++-- ----------------------------------------------------------++getHTTPNativeContents :: IOSArrow XmlTree XmlTree+getHTTPNativeContents+ = getC+ $<<+ ( getAttrValue transferURI+ &&&+ getSysVar (theInputOptions .&&&.+ theProxy .&&&.+ theStrictInput .&&&.+ theRedirect+ )+ )+ where+ getC uri (options, (proxy, (strictInput, redirect)))+ = applyA ( ( traceMsg 2 ( "get HTTP via native HTTP interface, uri=" ++ show uri ++ " options=" ++ show options )+ >>>+ arrIO0 (getCont strictInput proxy uri redirect options)+ )+ >>>+ ( arr (uncurry addInputError)+ |||+ arr addContent+ )+ )++addContent :: (Attributes, String) -> IOSArrow XmlTree XmlTree+addContent (al, c)+ = replaceChildren (txt c) -- add the contents+ >>>+ seqA (map (uncurry addAttr) al) -- add the meta info (HTTP headers, ...)++-- ------------------------------------------------------------++a_use_http :: String+a_use_http = "use-http"++withHTTP :: Attributes -> SysConfig+withHTTP httpOpts = setS theHttpHandler getHTTPNativeContents+ >>>+ withInputOptions httpOpts++httpOptions :: [OptDescr SysConfig]+httpOptions = [ Option "" [a_use_http] (NoArg (withHTTP [])) "enable HTTP input with native Haskell HTTP package" ]++-- ------------------------------------------------------------+
+ src/Text/XML/HXT/HTTP.hs view
@@ -0,0 +1,27 @@+-- ------------------------------------------------------------++{- |+ Module : Text.XML.HXT.Curl+ Copyright : Copyright (C) 2010 Uwe Schmidt+ License : MIT++ Maintainer : Uwe Schmidt (uwe@fh-wedel.de)+ Stability : stable+ Portability: portable++ libcurl input+-}++-- ------------------------------------------------------------++module Text.XML.HXT.HTTP+ ( getHTTPNativeContents+ , withHTTP+ , httpOptions+ )+where++import Text.XML.HXT.Arrow.LibHTTPInput++-- ----------------------------------------------------------+
+ src/Text/XML/HXT/IO/GetHTTPNative.hs view
@@ -0,0 +1,226 @@+-- ------------------------------------------------------------++{- |+ Module : Text.XML.HXT.IO.GetHTTPNative+ Copyright : Copyright (C) 2010 Uwe Schmidt+ License : MIT++ Maintainer : Uwe Schmidt (uwe@fh-wedel.de)+ Stability : experimental+ Portability: portable++ HXT interface for native HTTP access via package HTTP+-}++-- ------------------------------------------------------------++module Text.XML.HXT.IO.GetHTTPNative+ ( module Text.XML.HXT.IO.GetHTTPNative+ )++where++import Control.Arrow++import Text.XML.HXT.DOM.XmlKeywords+import Text.XML.HXT.DOM.TypeDefs ( Attributes )+import Text.XML.HXT.DOM.Util ( stringTrim )++import Text.XML.HXT.Arrow.XmlOptions ( a_if_modified_since+ , a_if_unmodified_since+ )++import Text.XML.HXT.Parser.ProtocolHandlerUtil ( parseContentType )++import Text.ParserCombinators.Parsec ( parse )++import Data.Char ( isDigit+ )+import Data.Maybe ( fromJust+ )+import System.IO ( hPutStrLn+ , stderr+ )+import System.IO.Error ( ioeGetErrorString+ , try+ )+import Network.Browser ( Proxy(..)+ , BrowserAction+ , browse+ , defaultGETRequest+ , request+ , setOutHandler+ , setErrHandler+ , setProxy+ , setAllowRedirects+ , setMaxRedirects+ )+import Network.HTTP ( Header(..)+ , HeaderName(..)+ , Request(..)+ , Response(..)+ , httpVersion+ , replaceHeader+ )+import Network.Socket ( withSocketsDo+ )+import Network.URI ( URI+ , parseURIReference+ )++-- ------------------------------------------------------------+--+-- the native http protocol handler++-- ------------------------------------------------------------+--+-- the http protocol handler, haskell implementation++getCont :: Bool -> String -> String -> Bool -> Attributes ->+ IO (Either ([(String, String)], String)+ ([(String, String)], String)+ )+getCont strictInput proxy uri redirect options+ = do+ res <- try (getHttp False uri1 proxy redirect options)+ either processError processResponse res+ where+ uri1 = fromJust (parseURIReference uri)++ processError e+ = return $+ Left ( [ (transferStatus, "999")+ , (transferMessage, "HTTP library error")+ ]+ , "http error when requesting URI "+ ++ show uri+ ++ ": "+ ++ ioeGetErrorString e+ ++ " (perhaps server does not understand HTTP/1.1) "+ )++ processResponse response+ | rc >= 200 && rc < 300+ = if strictInput+ then length cs `seq` return res+ else return res++ | otherwise+ = return $+ Left ( rs+ , "http error when accessing URI "+ ++ show uri+ ++ ": "+ ++ show rc+ ++ " "+ ++ rr+ )+ where+ rc = convertResponseStatus $ rspCode response+ rr = rspReason response+ res = Right (rs, cs)+ rs = rst ++ rsh+ rst = [ (transferStatus, show rc)+ , (transferMessage, rr)+ ]+ rsh = convertResponseHeaders response+ cs = rspBody response++ getHttp :: Bool -> URI -> String -> Bool -> Attributes -> IO (Response String)+ getHttp trc' uri' proxy' redirect' options'+ = withSocketsDo $+ browse ( do+ sequence_ configHttp+ (_ruri, rsp) <- request $ theRequest+ return rsp+ )+ where+ theRequest :: Request String+ theRequest+ = configHeaders $ defaultGETRequest uri' ++ configHeaders :: Request String -> Request String+ configHeaders+ = foldr (>>>) id . map (uncurry replaceHeader) . concatMap (uncurry setHOption) $ options++ configHttp+ = setOutHandler (trcFct)+ : setErrHandler (trcFct)+ : ( if null proxy'+ then return ()+ else setProxy (Proxy proxy' Nothing)+ )+ : setAllowRedirects redirect'+ : concatMap (uncurry setOption) options'++ trcFct s+ | trc'+ = hPutStrLn stderr ("-- (5) http: " ++ s)+ | otherwise+ = return ()++ convertResponseStatus :: (Int, Int, Int) -> Int+ convertResponseStatus (a, b, c)+ = 100 * a + 10 * b + c++ convertResponseHeaders :: Response String -> [(String, String)]+ convertResponseHeaders r'+ = cvResponseCode (rspCode r')+ +++ cvResponseReason (rspReason r')+ +++ cvResponseHeaders (rspHeaders r')+ where+ cvResponseCode :: (Int, Int, Int) -> [(String, String)]+ cvResponseCode st'+ = [ (transferStatus, show (convertResponseStatus st'))+ , (transferVersion, httpVersion)+ ]++ cvResponseReason :: String -> [(String, String)]+ cvResponseReason r''+ = [ (transferMessage, (stringTrim r'')) ]++ cvResponseHeaders :: [Header] -> [(String, String)]+ cvResponseHeaders+ = concatMap cvResponseHeader++ cvResponseHeader :: Header -> [(String, String)]+ cvResponseHeader (Header name value)+ | name == HdrContentType+ = ( case (parse parseContentType (show HdrContentType) value) of+ Right res -> res+ Left _ -> []+ )+ +++ addHttpAttr+ | otherwise+ = addHttpAttr+ where+ addHttpAttr = [ (httpPrefix ++ (show name), value) ]+++setOption :: String -> String -> [BrowserAction t ()]+setOption k v+ | k == "max-redirs"+ &&+ isIntArg v = [setMaxRedirects (Just $ read v)]+ | k == "max-redirs"+ &&+ null v = [setMaxRedirects Nothing]++ | otherwise = []++setHOption :: String -> String -> [(HeaderName, String)]+setHOption k v+ | k `elem` ["-A", "user-agent"] = [(HdrUserAgent, v)]+ | k `elem` ["-e", "referer"] = [(HdrReferer, v)]+ | k == a_if_modified_since = [(HdrIfModifiedSince, v)]+ | k == a_if_unmodified_since = [(HdrIfUnmodifiedSince, v)]+ | otherwise = []+++isIntArg :: String -> Bool+isIntArg s = not (null s) && all isDigit s++-- ------------------------------------------------------------