3

Which library should I use to parse HTTP requests from a socket stream? I'd like something that I can give a lazy ByteString which may contain partial or multiple HTTP requests.

I'm also interested in something similar for responding to HTTP requests (pass it a response object and get the lazy ByteString to write to the socket)

Edited to add more info: This interface would be ideal, but of course not required:

ByteString {- the initial buffer -} -> (Maybe Request, ByteString {- remaining buffer -})

In case of a partial request, the Maybe Request is Nothing and the ByteString is the same as the input.

Thanks :-)

1
  • What output do you want? (A list containing parsed requests?) and how do you want partial requests to be handled? Commented Apr 11, 2012 at 6:20

3 Answers 3

1

To handle this type of streaming data the current solution is to use one of the iteratee or iteratee like libraries. Conduit, Pipes(might be renamed proxies, because it can handle bidirectional data among other unique benefits), Iteratee, enumerator, iterIO and others. The conduit library seems to have a significate lead on the number libraries that depend on it.

You should take a look at the Conduit section on hackage.haskell.org and at http-conduit in particular. One of the first examples gets a http response and then writes it to a file in constant space.

import Data.Conduit.Binary (sinkFile)
import Network.HTTP.Conduit
import qualified Data.Conduit as C

main :: IO ()
main = do
     request <- parseUrl "http://google.com/"
     withManager $ \manager -> do
         Response _ _ _ src <- http request manager
         src C.$$+- sinkFile "google.html"
Sign up to request clarification or add additional context in comments.

Comments

0

I think you can just Network.HTTP for the basic implementation, but I don't think it natively supports ByteStrings.

Comments

0

Pipes has a different strategy than conduit regarding features. Conduit packages all its features together, while Pipes prefers to package them separately.

pipes-bytestring is the package I believe you were looking for.

It can be installed by running:

cabal install pipes-bytestring

To find other pipes packages you can run:

cabal list pipes

or you could also search hackage

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.