Re: read_mime_header safety

Pierre Habouzit <[email protected]> Thu, 2 Jun 2005 16:28:15 +0200
Newsgroups gmane.comp.lang.ocaml.lib.net.devel
Message-ID <[email protected]>
Le Jeudi 2 Juin 2005 16:02, Jesse Guardiani a écrit :
> Hello,
>
> I'm writing a mail filter for qmail-qfilter in OCaml that uses
> read_mime_header to parse just the header of the message and perform
> logic based on the header field values. qmail-qfilter passes incoming
> email messages to it's filter programs on stdin, and qmail-qfilter
> reads the message output from the filter program's stdout so the
> modified message can be passed to the next filter program or
> qmail-queue.
>
> My #1 concern with this OCaml filter program I'm writing is ultimate
> reliability. If read_mime_header fails to parse the header properly,
> I want to execute a default action. I will have two possible default
> actions at this point: 1.) Allow the message through
>   2.) Block the message entirely
>
> And I'll make this configurable at run time via a config file.
>
> So, given the following code to read the header:
>
>   let ch = new Netchannels.input_channel (stdin) in
>   let stream = new Netstream.input_stream ch in
>   let header = Netmime.read_mime_header stream in
>
> If read_mime_header throws an exception and I catch the exception and
> the program is configured to allow the message through on error
> conditions, how do I reset ch to the beginning of stdin so I can pass
> the message to stdout in it's entirety?


if the header parse fails, the data from the channel is not consumed : 
Netmime.read_mime_header wraps Mimestring.read_header that is roughly :

let read_header [args] =
  let headers = try_to_parse_the_headers stream in
  stream # skip_the_headers;
  headers

the 'try_to_parse...' part only works in the window of the stream, 
without really consuming the lines. so your are safe, if an exception 
occurs (most likely (Failure "Mimestring.read_header")), it won't loose 
any data.

AFAIK, if the parse has been successful, there is *no chance* that the 
'skip' call would result in any exception (at least using ocamlnet 
classes), so I think, you have nothing to do, ocamlnet already takes 
care of it.
  

-- 
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBCnxeBvGr7W6HudhwRAm8mAJwJ/nJVAdO7u59UQt6XILWU5Jc7zACeLgD7
VQvhgtHaKI5J807Pyqo7uzg=
=ASD0
-----END PGP SIGNATURE-----