Short reads in CGI applications: A solution

Scott Raney raney at metacard.com
Thu Oct 24 23:08:01 EDT 2002


We've recently received several reports of short reads from stdin in
CGI applications but have never been able to reproduce or understand
this until our own (recently upgraded) server started to show the
problem.  Not sure if it's a newer version of the HTTP server software
or faster hardware or what, but sometimes "read from stdin until
empty" doesn't get all the data it should.  Sometimes it doesn't get
anything at all.

Now "read .. until empty" was never a good idea, merely a quick and
dirty way to read whatever should be incoming in the first bufferfull
from the HTTP server.  Unfortunately the easy workaround, "read from
stdin for $CONTENT_LENGTH" also doesn't work if the process on the
other end of stdin doesn't keep up.  So what you have to do instead is
something like:
  put empty into buffer # this is where your data will end up
  repeat until length(buffer) >= $CONTENT_LENGTH
    read from stdin until empty
    put it after buffer
  end repeat

We'll look into supporting a simpler way to do this for 2.5 (not sure
whether to fix it so "read .. for $CONTENT_LENGTH" works, or to
support "read .. until eof" on pipes), but in the mean time something
like the above should solve the problem.
  Regards,
    Scott

********************************************************
Scott Raney  raney at metacard.com  http://www.metacard.com
MetaCard: You know, there's an easier way to do that...




More information about the metacard mailing list