[FASTCGI] FastCGI as a Filter?

Rob Lemley rclemley at booksys.com
Tue Oct 27 15:01:32 EDT 2009


Date: Sat, 24 Oct 2009 23:52:55 -0400
> From: tbandrowsky <tbandrowsky at treatyist.com>
> Subject: [FASTCGI] FastCGI as a Filter?
>
> I'm trying to make heads or tales of whether or not Apache 2 supports
> FastCGI as a filter.  Basically, I want to have a bunch of files with a
> particular extension in a directory all get passed through to my FastCGI
> application rather than get spewed out directly.  I've read that one
> does not need filters to do this, although clearly, filters look like
> they would be a pretty elegant approach. Can I do this sort of a thing
> with Apache, or am I just banged and should use something like Sun's web
> server?
>   
Apparently mod_fastcgi (the apache fastcgi module) does not support
FastCGI filter role:

"mod_fastcgi does not implement the Authorizer or Filter roles described
in the FastCGI specification. However, you can approximate the Filter
role using Apache's Action  directive to route requests to a FastCGI
Responder. See the documentation for mod_actions for information on the
Action directive."
(http://www.fastcgi.com/om_archive/servers/apache/apache-fastcgi/mod_fastcgi.html)

The mod_action solution (which I have no experience with) seems to catch
all files with a given mime-type or, in this case, extension:

    # Files of a particular file extension
    AddHandler my-file-type .xyz
    Action my-file-type /cgi-bin/program.cgi

which, translated to FastCGI, I think would look something like (untested):

    # Files of a particular file extension
    AddHandler my-file-type .xyz
    Action my-file-type /myfastcgiapp
    FastCgiServer /myfastcgiapp ....

I believe the way to set this up with apache  AliasMatch directive,
which allows you to capture input requests based on a more general
regular expression pattern, to set the Apache2 "AliasMatch" directive to
pass only the files you want to filter to your FastCGI server:

    #APACHE CONFIG:

    # NameVirtualHost *:80

    <VirtualHost *:80>
      ServerName yourserver.example.com
      ServerAlias yourserver
      AliasMatch ^/(path/to/yourfiles/*.ext) /yourfastcgiserver
    </VirtualHost>

    FastCgiExternalServer /yourfastcgiserver  -socket
    /var/run/yourfastcgiserver/socket

    #END OF APACHE CONFIG

Next, you would implement the FastCGI server to read the files and
filter them as necessary.  This usage of FastCGI is fairly well known
and I don't have a good concise example in my own code.  To filter these
files, generally all you would do is open the filename found in the
FastCGI Environment (CGI) variable "SCRIPT_NAME."  You might need to
adjust the directory or something depending on how your server namespace
is set up.

    while accept_fastcgi_request(request)                    
    #FCGX_Accept(), etc
        script_name = lookup(request.envp, "SCRIPT_NAME")
        filter(script_name)                                   # writes
    using FCGX_PutStr(), may need to Put the HTTP response header too.
    endwhile


All of the information about the request will come through the FastCGI
request.  You'll look at various fields in the FastCGI environment to
determine the file to open and filter.  Generally your program might
look at REQUEST_URI, QUERY_STRING and SCRIPT_NAME.  You may also want to
check for POST request data in some case (depends on having
"CONTENT_TYPE=multipart/form-data; boundary.....).  Or you can just
ignore any query string and post data and just filter the SCRIPT_NAME.

NOTE: usually (always?) REQUEST_URI == SCRIPT_NAME + "?" + QUERY_STRING

Rob

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.pins.net/mailman/private.cgi/fastcgi-developers/attachments/20091027/51de45cd/attachment.html>


More information about the FastCGI-developers mailing list