Skip to content

Commit 579c434

Browse files
committed
Add method to detect FastCGI environment
Closes #13
1 parent 1418646 commit 579c434

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This module is a drop-in replacement for node's http module (server only). It ca
1313

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

16+
1617
Example
1718
-------
1819

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

34+
3335
Server constructor
3436
------------------
3537

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

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

64+
6265
FastCGI values
6366
--------------
6467

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

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

85+
86+
Listening for connections
87+
-------------------------
88+
89+
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.
90+
91+
This is done automatically when you call the `listen` method on the server object without arguments, or with a callback as the only argument.
92+
93+
The `isService` function is provided to check if the current script is being run as a FastCGI service.
94+
95+
```js
96+
if (fcgi.isService()) {
97+
fcgi.createServer(/* ... */).listen();
98+
} else {
99+
console.log("This script must be run as a FastCGI service");
100+
}
101+
```
102+
103+
82104
Request URL components
83105
----------------------
84106

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

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

117+
95118
Authorizer and filter requests
96119
------------------------------
97120

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

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

129+
106130
The socket object
107131
-----------------
108132

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

142+
118143
http module compatibility
119144
-------------------------
120145

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

157+
132158
License
133159
=======
134160

example/cgiFallback.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ function log(msg) {
3737

3838
var createServer = fcgi.createServer;
3939

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

4444
createServer(function (req, res) {

index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
'use strict';
2323

24-
var http = require('http');
24+
var fs = require('fs'),
25+
http = require('http');
2526

2627
var server = require('./lib/server.js'),
2728
response = require('./lib/response.js');
@@ -48,3 +49,7 @@ exports.AuthorizerResponse = response.AuthorizerResponse;
4849
exports.createServer = function (responder, authorizer, filter, config) {
4950
return new server.Server(responder, authorizer, filter, config);
5051
};
52+
53+
exports.isService = function () {
54+
return fs.fstatSync(0).isSocket();
55+
}

0 commit comments

Comments
 (0)