FastCGI implementation for Ruby

Document written by Michael Neumann (neumann@s-direktnet.de).

Classes

Exception Classes

FCGI

Synopsis

require "fcgi"

FGCI.each_request do |req|
  puts "Content-type: text/html\n"
  puts "<html><body><ul>"
  ENV.each {|k,v| puts "<li><b>#{k}: </b> #{v}</li>" }
  puts "</ul></body></html>"
end

Used in conjunction with class CGI:

require "fcgi"
require "cgi"

FCGI.each_request do |req|
  $stdin = req.in     # to handle POST requests
  cgi = CGI.new  

  puts "Content-type: text/html\n"
  puts "<html><body>"
  puts cgi.params.inspect
  puts "</body></html>"

  # remove CGI::CGI_PARAMS and CGI::CGI_COOKIES, so that
  # class CGI will reread the parameters.
  class CGI
    remove_const :CGI_PARAMS
    remove_const :CGI_COOKIES
  end 
end    

Description

The class for handling FastCGI requests.

Unlike CGI-scripts, FastCGI-programs are (normally) executed only once and are hold in memory. So the overhead of process creation is eleminated which results in (often great) speed improvements.

Due to the fact that FastCGI applications do not (normally) exit, all variables declared in a FastCGI application are accessible for each request. There is no need (like in CGIs) to store variables which should be persistent between requests to disk. Also, database connections can be shared, i.e. they must only be connected once.

All CGI environment variables are also existent in FastCGI, plus some more.

Please see <URL:http://www.fastcgi.com/> for more informations.

Class Methods

FCGI.accept

Waits for a new FastCGI request. When successful, the methods returns an object of type FCGI, and sets $stdout automatically to FCGI#out of the returned object. If an error occured, the method retuns nil. The environment variables (ENV)are also changed!

FCGI.new

Synonym for FCGI.accept.

FCGI.each_request {|req| block }

Calls the block block for each FastCGI request. req is an object of type FCGI and represents the actual request. Sets automatically $stdout to the value of FCGI#out of the object represented by req. The environment variables (ENV)are also changed!

FCGI.each {|req| block }

Synonym for FCGI.each_request.

Instance Methods

FCGI#in

Returns an object of type FCGI::Stream which represents the standard input.

FCGI#out

Returns an object of type FCGI::Stream which represents the standard output.

FCGI#err

Returns an object of type FCGI::Stream which represents the standard error.

FCGI::Stream

Instance Methods

FCGI::Stream#putc( char )

Writes char, which must be of type Numerical into the stream. Returns the written character.

FCGI::Stream#write( str )

Writes the string str into the stream and returns the length of the written string.

FCGI::Stream#print

Works like Kernel#print.

FCGI::Stream#printf

Works like Kernel#printf.

FCGI::Stream#puts

Works like Kernel#puts.

FCGI::Stream#<<( str )

Synonym for FCGI::Stream#write.

FCGI::Stream#flush

Flushes the stream.

FCGI::Stream#getc

Returns the next character from the stream. Returns nil on EOF.

FCGI::Stream#ungetc( char )

Puts char back to the stream.

FCGI::Stream#gets

Works like Kernel#gets.

FCGI::Stream#read ( num )

Reads num bytes from the stream and returns a string, if more than zero bytes were read, otherwise returns nil.

FCGI::Stream#eof

Returns true on EOF, otherwise false.

FCGI::Stream#eof?

Synonym for FCGI::Stream#eof.

FCGI::Stream#close

Closes the stream.

FCGI::Stream#closed?

Returns true if the stream was already closed, otherwise false.

FCGI::Stream::Error

Superclass

StandardError

Description

Unknown error.

FCGI::Stream::UnsupportedVersionError

Superclass

FCGI::Stream::Error

Description

Unsupported version.

FCGI::Stream::ProtocolError

Superclass

FCGI::Stream::Error

Description

Protocol error.

FCGI::Stream::ParamsError

Superclass

FCGI::Stream::Error

Description

Parameter error.

FCGI::Stream::CallSeqError

Superclass

FCGI::Stream::Error

Description

Preconditions are not met.

History

$Id: fcgi.rd,v 1.3 2001/02/12 16:51:35 michael Exp $