Skip to content

Commit

Permalink
Add method to detect FastCGI environment
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
fbbdev committed Aug 24, 2016
1 parent 1418646 commit 579c434
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This module is a drop-in replacement for node's http module (server only). It ca

The implementation is fully compliant with the [FastCGI 1.0 Specification](https://fast-cgi.github.io/spec).


Example
-------

Expand All @@ -30,6 +31,7 @@ fcgi.createServer(function(req, res) {
}).listen();
```


Server constructor
------------------

Expand Down Expand Up @@ -59,6 +61,7 @@ The `createServer` function takes four **optional** parameters:

`valueMap` maps FastCGI value names to keys in the `config` object. For more information read [the next section](#fastcgi-values)


FastCGI values
--------------

Expand All @@ -79,6 +82,25 @@ fcgi.createServer(function (req, res) { /* ... */ }, {

**WARNING: This `valueMap` thing is complete nonsense and is definitely going to change in the next release.**


Listening for connections
-------------------------

When a FastCGI service is started, the stdin descriptor (fd 0) [is replaced by a bound socket](https://fast-cgi.github.io/spec#accepting-transport-connections). The service application can then start listening on that socket and accept connections.

This is done automatically when you call the `listen` method on the server object without arguments, or with a callback as the only argument.

The `isService` function is provided to check if the current script is being run as a FastCGI service.

```js
if (fcgi.isService()) {
fcgi.createServer(/* ... */).listen();
} else {
console.log("This script must be run as a FastCGI service");
}
```


Request URL components
----------------------

Expand All @@ -92,6 +114,7 @@ For more information read [section 4.1](https://tools.ietf.org/html/rfc3875#sect

Raw CGI variables can be accessed through the `params` property of the socket object. More information [here](#the-socket-object).


Authorizer and filter requests
------------------------------

Expand All @@ -103,6 +126,7 @@ Authorizer requests have no url. Response objects for the authorizer role expose

Filter requests have an additional data stream exposed by the `data` property of [the socket object](#the-socket-object) (`req.socket.data`).


The socket object
-----------------

Expand All @@ -115,6 +139,7 @@ The socket object exposes three additional properties:
- `dataStream` implements `stream.Readable`, exposes the FastCGI data stream for the filter role.
- `errorStream` implements `stream.Writable`, translates writes to stderr FastCGI Records.


http module compatibility
-------------------------

Expand All @@ -129,6 +154,7 @@ Differences:
- `req.trailers` will always be empty: CGI scripts never receive trailers
- `res.writeContinue()` works as expected but should not be used. See first item


License
=======

Expand Down
4 changes: 2 additions & 2 deletions example/cgiFallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ function log(msg) {

var createServer = fcgi.createServer;

// stat fd 0; if it's not a socket switch to plain CGI.
if (!(fs.fstatSync(0).mode & fs.constants.S_IFSOCK))
// check if we're running as a FastCGI service; if not, switch to plain CGI.
if (!fcgi.isService())
createServer = cgi.createServer;

createServer(function (req, res) {
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

'use strict';

var http = require('http');
var fs = require('fs'),
http = require('http');

var server = require('./lib/server.js'),
response = require('./lib/response.js');
Expand All @@ -48,3 +49,7 @@ exports.AuthorizerResponse = response.AuthorizerResponse;
exports.createServer = function (responder, authorizer, filter, config) {
return new server.Server(responder, authorizer, filter, config);
};

exports.isService = function () {
return fs.fstatSync(0).isSocket();
}

0 comments on commit 579c434

Please sign in to comment.