[Haskell-cafe] parsec, hGetContents

brian brianchina60221 at gmail.com
Sat Aug 23 19:41:52 EDT 2008


I'm writing an NNTP client API. When I connect with connectTo, I get a
Handle 'h'. I immediately hGetContents h to get a string 'c' that I
hope contains all the data the server will ever send. I put h and c in
a data Connection and pass it to the functions that send or receive.

I send a command and receive a response. The response might look like
"200 OK\r\n". Parsec parses c. No problem. But if I send another
command and receive another response, parsec parses the same string
because (I think) I haven't trimmed the part I parsed on the previous
command. Maybe c looks like "200 OK\r\n200 second thing\r\n".

So I was thinking I need to parse and return the unparsed part so that
the next parse will work. Parsec has a getInput that returns the
unparsed part, but it blocks if there is no more to parse. That is,
after I parse "200 OK\r\n", if I ask parsec for the "rest", it blocks
because there isn't any "rest" yet.

How should I be doing this? Before people suggest I hGetLine and parse
that, I don't like hGetLine for this because 1) it assumes '\n'
signifies the end of a line, whereas in my case the protocol specifies
"\r\n" and 2) if the "line" doesn't have an ending, I can't tell it
from what hGetLine returns. That is, hGetLine on a handle having
"abc\n" returns the same thing as on a handle having "abc".

Also, if I can parse c directly, I can write parsers (especially those
for multiline respones) much more naturally than I can if I have to
get lines separately.

Thanks very much for any help.


More information about the Haskell-Cafe mailing list