Re: fastcgi 'filter' role

Yalcin Bozcaada (bozcaada@OpenMarket.com)
Fri, 10 Jan 1997 08:16:42 -0500

Message-Id: <1.5.4.32.19970110131642.008e52e0@mail-60.openmarket.com>
Date: Fri, 10 Jan 1997 08:16:42 -0500
To: mud@intervu.com
From: Yalcin Bozcaada <bozcaada@OpenMarket.com>
Subject: Re: fastcgi 'filter' role

yup. here you go.

/****************************************************************************
 filterit.c

 A FastCGI filter implementation

 yalcin Bozcaada 
 1-8-96
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <fcgi_stdio.h>		

void main(void)
{
  size_t   PageSize = 1024 * 25;
  char     *page;
  int count=0;
 
  page = malloc(PageSize);
  
  if (page == NULL) 
  {
     printf("Content-type: text/x-server-parsed-html\r\n");
     printf("<title>malloc failure</title>");
     printf("<h1>Cannot allocate memory to run filter. exiting </h1>");
     exit(5);
  }
  
  while(FCGI_Accept() >= 0)
  {
    int numchars = 0;
    char *tmp;
    char *execcgi;     
    count++;
    /* clear out stdin */
    while (EOF != getc(stdin));
    
    FCGI_StartFilterData();
    tmp = page; /** just in case fread or fwrite moves our pointer **/
    numchars =  fread((void *)(page), 1, PageSize-1, stdin);
    
    if( numchars == 0 ) /** what ???? **/ 
       continue; /** hopefuly this will just make the program transperent
eighk **/
    /** 
       ok. whatever filetered was passed into page pointer 
       lets see what is in the pointer really ..
    **/
   /**
    .
    .
    .
   **/
   /** 
      at this point your data is in page pointer, so do whatever you want
with it
      before sending it back to the server. 
      You can just send everything back , you don't need to send
content-type etc.
   **/
     
    /** now, write contents to client **/
    printf("Content-type: text/x-server-parsed-html\r\n"); 
    printf("\r\n");  /** send a new line at the beginning **/
    printf("<title>FILTERING</title>");
    printf("<h1>This page was Filtered by FILTERME FastCGI filter</h1>");
    page = tmp; /** restore page pointer **/
    fwrite(page, 1, (numchars), stdout);
    printf("\r\n"); /** send a new line at the end of transfer **/
    /** clearout the page here, because it is global. **/
    memset(page,NULL,numchars); 
  }
  free(page); 
}

You need to pay attention to few things:


1- make sure there is a \r\n before and after the page when you are sending
it to the server
2- filter definition in the config file.
  in OMI's SWS 2.0.5 the definition would be:

   MimeType application/x-sgml-filter sgml
   AppClass FilterIt /path/filterme
   Filter FilterIt application/x-sgml-filter

good luck.

-yalcin 
At 06:10 PM 1/9/97 -0800, you wrote:
>We had excellent sucesss get our fast-cgi responder to work but
>we'd like to write a 'filter' role type fastcgi program. Unfortunately
>the documentation is a little weak in regards to this topic.
>
>Does anyone have an working example they could share with us or at
>least point us to some detailed documentation on fast-cgi filters??
>
>thnx in advance,
>
>bob & chuck
>
>
*----------------------------------------------------------------*
*- yalcin Bozcaada						-*
*- Open Market Inc. "The Internet Commerce Company"		-*
*- 245 First Street, Cambridge, MA 02142  USA			-*
*- www.openmarket.com						-*
*- Phone: +1 617 949 7543 					-*
*----------------------------------------------------------------*