Re: Problem with fastcgi hello world example /cgi/fcgi_hello.c

Brandon Long <[email protected]> Sun, 10 Feb 2008 23:32:21 -0800
Newsgroups gmane.text.clearsilver.general
Organization Fiction L Networks
Message-ID <20080211073221.GA7127@bl1>
On 02/10/08 Bjorn uttered the following other thing:
> Hi,
> 
> It's awesome that ClearSilver comes with a fastcgi example, because I
> would have never figured out I had to wrap output functions, I thought
> fastcgi took care of all of that on it's own. However the file has
> some problems. The file I'm talking about is /cgi/fcgi_hello.c
> 
> The problems are all in the same function:
> 
> static int cs_write(void *ctx, const char *s, int n) {
>   return fwrite(const_cast<char *>(s), n, 1, FCGI_stdout);
> }
> 
> The first problem is that .c is a C extension, but this function uses
> C++. There is no other C++ code in this file. The other problem is

Yeah, the example was originally from some C++ code someone gave me,
that one slipped through.  I don't have a fastcgi setup, so I tried to
edit it down, but apparently some things slipped through.

> that fwrite returns the number of objects written, and not the number
> of bytes/characters which is what cs_write is supposed to return.
> Since fwrite does not return the same value as n, cgi_display will
> always return an error, even though one had not occured. I have found
> that this small fix solves all of these problems (I think, I'm new to C):
> 
> static int cs_write(void *ctx, const char *s, int n) {
>   if(fwrite(s, n, 1, FCGI_stdout) < 1){
>       return 0;
>   }
>   return n;
> } 

Ah, the size and number of elements are backwards, it should probably
be:

static int cs_write(void *ctx, const char *s, int n) {
  return fwrite(s, 1, n, FCGI_stdout);
}

Thanks!

Brandon
-- 
 "Quantum Mechanics: The dreams stuff is made of" -- Steven Wright
                                           http://www.fiction.net/blong/