# Sending FCGI requests \*from\* Ruby

**URL:** <https://www.ruby-forum.com/t/sending-fcgi-requests-from-ruby/84103>\
**Category:** Ruby\
**Created:** [February 1, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/sending-fcgi-requests-from-ruby/84103 "2007-02-01T13:21:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![stickstone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6f9a4e/32.png) [@stickstone](https://www.ruby-forum.com/u/stickstone)\
**Post date:** [February 1, 2007, 1:21pm UTC](https://www.ruby-forum.com/t/sending-fcgi-requests-from-ruby/84103/1 "2007-02-01T13:21:14Z")

</div>

Writing FCGI apps in Ruby, which are spawned from Apache or lighttpd, is  
straightforward enough.

What I want to know is, what’s the best way which a Ruby program can  
invoke  
other FCGI apps and send requests _to_ them? And has anyone had success  
doing this?

The application I have in mind is to make use of some existing perl  
code. I  
don’t want to do

rc = system(“/usr/bin/perl”, “foo.pl”, \*args)

for every operation, as this spawns a whole perl interpreter each time.

So what I wanted to do was to make minimal changes to the perl code so  
that  
it could accept multiple requests in a loop.

I could just popen() each perl script and chat down stdin/stdout, but  
this  
limits me to one instance of this application per Ruby process. In  
practice  
there will be 10 or 20 different perl scripts that I want to persist,  
but  
each Ruby process will only invoke one or two of them before it  
finishes.

However, if I use FCGI, I can have shared pools of Perl instances to be  
invoked by other Ruby processes as they come and go, and the size of  
each  
pool can be tuned statically or dynamically. This is very attractive.

So how to achieve this? I could run those perl FCGI’s under Apache, and  
then  
use Ruby’s Net::HTTP to send requests to Apache which in turn sends them  
to  
those FCGIs. But I wondered if there was a more lightweight way to do  
this,  
formatting the FCGI requests and/or starting the pool of FCGI slave  
processes directly from Ruby.

(Having said that, using HTTP as the glue, optionally with XMLRPC or  
SOAP  
running over it, would arguably be a “standard” solution)

I can use the standalone program cgi-fcgi to start the pool members:  
[http://www.fastcgi.com/devkit/doc/cgi-fcgi.1](http://www.fastcgi.com/devkit/doc/cgi-fcgi.1)

So what I’m really looking for is something like the cgi-fcgi’s “-bind  
-connect” option, to pass a CGI request down the socket, but written in  
or wrapped for Ruby. But I’m also open to suggestions for other ways to  
achieve what I’m trying to do.

Thanks,

Brian.

P.S. Searching around, I did notice there’s a webrick-fcgi module, which  
I  
thought maybe I could rip some code from.

The website pointed to by [http://raa.ruby-lang.org/project/webrick-fcgi/](http://raa.ruby-lang.org/project/webrick-fcgi/)  
is [http://theinternetco.net/projects/ruby/webrick-fcgi/](http://theinternetco.net/projects/ruby/webrick-fcgi/)

This returns Content-Type: application/octet-stream, meaning the page  
does  
not display in a browser!

Furthermore, if you try to download the source from  
[http://theinternetco.net/projects/ruby/webrick-fcgi/webrick-fcgi-0.1.0.tar.gz](http://theinternetco.net/projects/ruby/webrick-fcgi/webrick-fcgi-0.1.0.tar.gz)  
you get an HTML page rather than a tarball. So I can’t download it, and  
I imagine it’s unlikely that many people are using it.

$ telnet [theinternetco.net](http://theinternetco.net) 80  
Trying 204.10.124.77…  
Connected to [theinternetco.net](http://theinternetco.net).  
Escape character is ‘^]’.  
GET /projects/ruby/webrick-fcgi/ HTTP/1.0  
Host: [theinternetco.net](http://theinternetco.net)

HTTP/1.0 200 OK  
Connection: close  
Content-Type: application/octet-stream \<\<\<\<\< !  
ETag: “2004059686”  
Accept-Ranges: bytes  
Last-Modified: Sat, 11 Jun 2005 22:35:45 GMT  
Content-Length: 582  
Date: Thu, 01 Feb 2007 12:01:21 GMT  
Server: lighttpd/1.4.13

 webrick-fcgi 
# webrick-fcgi

## Abstract

webrick-fcgi is a WEBrick-compatible API for the FCGI library.

## Downloads

Version 0.1.0: webrick-fcgi-0.1.0.tar.gz

## Documentation

Version 0.1.0: rdoc

 Connection closed by foreign host.
