Network.HTTP module, using simpleHTTP

Graham Klyne gk at ninebynine.org
Tue Jun 8 15:20:40 EDT 2004


Bjorn,

I'm trying to add an HTTP entity retrieval capability to HaXml using 
simpleHTTP as the basis of a new function, readHTTP [1], that works very 
similarly to prelude.readFile (except that its argument is a Network.URI 
value).  Function simpleHTTP still leaves a fair amount of result analysis 
to be done by the calling program.  I'm thinking that it might be 
convenient to provide a simple function, say:

     hasResponseData :: Response -> Bool

that can be used to drive a simple binary decision along the lines of:

     return $ if hasResponseData response
         then (Right $ rspBody response)
         else (Left  $ show response)

where

     hasResponseData rsp = case rspCode rsp of
         (2,_,_)   -> True
         otherwise -> False

I'm not sure if I've got the condition right here, but it seems about right.

Do you think this would be a reasonable addition to the HTTP module, to 
make it very easy for a program to issue a simple HTTP GET to retrieve a 
resource?

...

Another thought I wanted to raise with you concerns the URI authority 
parser that is currently part of the HTTP module.

My revised version of URI already does most of what this micro-parser does 
(apart from not separating the username and password in userinfo).  When 
the revised URI specification (successor to RFC2396) looks stable, I'm 
planning to update the Network.URI module in the hierarchical 
libraries.  It occurs to me that the added functionality here could mean 
that module HTTP might be simplified.

A copy of the current state of my Network.URI module can be found here:
   http://www.ninebynine.org/Software/HaskellUtils/Network/URI.hs
There is also a test program and a test suite, described here:
   http://www.ninebynine.org/Software/HaskellUtils/Network/URITestDescriptions.html
An earlier, stand-alone, test suite is here:
   http://www.ninebynine.org/Software/HaskellUtils/Network/URITest.hs

#g
--

[1] here's a simple readHTTP function I've cooked up... does it look workable?:

[[
readHTTP :: URI -> IO String
readHTTP uri = withSocketsDo $ do
     { res <- simpleHTTP (defaultGETRequest uri)
     ; case res of
         Left  err ->
             return ("\nError!  failed to read "++show uri++": "++show 
err++"\n")
         Right rsp -> return $ if hasResponseData rsp
             then rspBody rsp
             else show rsp
     }

hasResponseData :: Response -> Bool
hasResponseData rsp = case rspCode rsp of
     (2,_,_)   -> True
     otherwise -> False

defaultGETRequest uri =
     Request { rqURI=uri
             , rqBody=""
             , rqHeaders=[ Header HdrContentLength "0"
                         , Header HdrUserAgent "haskell-haxml/0.1"
                         ]
             , rqMethod=GET
             }
]]



------------
Graham Klyne
For email:
http://www.ninebynine.org/#Contact



More information about the Libraries mailing list