The new HTTP library

Written by Jeff Heard on January 16th, 2009

I pulled down the new HTTP library the other day to see how much of a difference the ByteString enabled downoading would make to my performance.  Wow.  I went from 1.503 seconds for a 5mb image download from the apache server on my own machine to 0.053 seconds with the new HTTP library.  Previously, I’d been using HTTP-Simple, but now I’ve rewritten the HTTP-Simple’s single export function httpGet to use bytestrings.

It’s not pretty, but I didn’t have to change a lot of code to get all the benefit.  Note, openURI a function exported by Network.Download.Curl, and httpGet is a function exported by Network.HTTP.Simple.  These were the only two functions I used in the program I’ve been working on, so I just wrote the following code to replace them:

module HTTPSimple where

import Network.Stream
import Network.HTTP
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as CS
import System.Environment
import Network.URI
import Data.Maybe
import Control.Monad

openURI :: String -> IO (Either String BS.ByteString)
openURI url = do
    result <- simpleHTTP $ Request (fromJust . parseURI $ url) GET [] BS.empty

    case result of
        Right (Response _ _ _ body) -> return (Right body)
        Left err -> return (Left . show $ err)                                                    

httpGet :: URI -> IO (Maybe String)
httpGet url = do
    result <- simpleHTTP $ Request url GET [] CS.empty

    case result of
        Right (Response _ _ _ body) -> return (Just . CS.unpack $ body)
        Left err -> return Nothing

Leave a Comment