Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: asmadeus77@gmail.com
To: caml-list <caml-list@inria.fr>
Subject: [Caml-list] OCaml-ssl and input_string
Date: Mon, 5 May 2008 21:54:10 +0200	[thread overview]
Message-ID: <b79ece760805051254p65298945l58cac76535040bf4@mail.gmail.com> (raw)

Hello,

I'm making a simple mail client for gmail using IMAP, and I need to
use ssl sockets.
So far so good, there's a library called ocaml-ssl which works quite
well, untill you need to read what the server sends.

At first, the function input_string in the library is defined by
let input_string ssl =
  let bufsize = 1024 in
  let buf = String.create bufsize in
  let ret = ref "" in
  let r = ref 1 in
    while !r <> 0
    do
      r := read ssl buf 0 bufsize;
      ret := !ret ^ (String.sub buf 0 !r)
    done;
    !ret

Which doesn't work, since read will wait untill there is something to
read if there is nothing waiting to be read, thus will never return 0.
I've changed the loop condition to "while !r = bufsize", initializing
r with bufsize, and I've been satisfied with the result during my
early testing (discovering the IMAP protocol in toplevel)

Now, though, I've encountered a problem - it seems that the function
returns too early, meaning that there is still some text left to be
read after it has been invoked.
I've discovered that the size of the returned strings is quite often
1400 or 648, but it can be anything, and changing the variable
"bufsize" doesn't help (I guess it's the size of the packets sent by
the server, but couldn't the socket hold more characters ? There's
usually been a couple of seconds between two input_string calls, so
there should be more than just a couple of KBs)

I've tried adding a poll - (same function with "let fdescr_of_ssl =
file_descr_of_socket ssl in while Unix.select [fdescr_of_ssl] [] []
10. <> [],[],[] do") - but the result, though better, isn't granteed
unless I set the timeout to at least 5-10 seconds, and even there I've
had cases of uncomplete returns, even though they were rare enough to
bear with.

Anyway, I've decided to program this mail client to avoid waiting for
the slow gmail web view, so I'd like to not to use a select there :

Is it possible to read the socket untill a known string, to be sure
the request is done, without waiting after it ? (the string I need to
match is "known_identifier_without_space OK Success\r\n" eventually
leaving the "OK Success" as wildcards to check for (it could be "BAD
why it is bad\r\n"))

I can assume I'm not making two requests at once, thus being sure that
I can read the socket by large chunks untill the end matches, but I
would rather not do so if possible.

Any clue ?

Thanks,
Dominique Martinet

PS : A friend of mine is wondering if there's a regexp for ranges in
the Str module (like "aaaa?" would be "a{3-4}" in pcre), I'm sure
he'll be glad if someone could ensure him there isn't any :P


             reply	other threads:[~2008-05-05 19:54 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-05 19:54 asmadeus77 [this message]
2008-05-06 10:12 ` Berke Durak
2008-05-06 10:33   ` asmadeus77

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b79ece760805051254p65298945l58cac76535040bf4@mail.gmail.com \
    --to=asmadeus77@gmail.com \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox