Permalink
Browse files

lots of testing

  • Loading branch information...
1 parent c8879eb commit 25d7612f3889335ea2572353a7d00830689563fd @billywhizz committed Feb 19, 2011
Showing with 370 additions and 207 deletions.
  1. +83 −0 README.md
  2. +100 −84 examples/client.js
  3. +95 −55 examples/php-server.js
  4. +75 −48 examples/server.js
  5. +17 −20 lib/fastcgi.js
View
83 README.md 100644 → 100755
@@ -29,3 +29,86 @@ A very basic FastCGI parser for low level parsing of the FastCGI protocol. Can b
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
}
+# API
+
+fastcgi.parser
+
+parser.encoding = ["utf8"|"ascii"|"binary"]
+
+default is utf8. this determines the encoding used when reading the body of an STDIN/STDOUT/STDERR record. utf8 or ascii mean no onBody callback will be fired and the record in onRecord will have a body property set to the correctly encoded string.
+binary will mean no body property is set on the record returned in the onRecord callback and chunks of the body will be emitted in the onBody callback as they arrive.
+
+parser.reset = function()
+
+resets the parser so it can be executed on a new stream. you should use this aftaer an error or when re-using an already existing parser on a new stream.
+
+parser.init = function()
+
+completely reinitialises the parser. calls parser.reset as well as setting encoding back to default (utf8) and clearing all callback handlers
+
+parser.execute = function(buffer)
+
+parses the buffer. calls callback.
+
+fastcgi.constants
+
+ "version": 1,
+ "record": {
+ "FCGI_BEGIN": 1,
+ "FCGI_ABORT": 2,
+ "FCGI_END": 3,
+ "FCGI_PARAMS": 4,
+ "FCGI_STDIN": 5,
+ "FCGI_STDOUT": 6,
+ "FCGI_STDERR": 7,
+ "FCGI_DATA": 8,
+ "FCGI_GET_VALUES": 9,
+ "FCGI_GET_VALUES_RESULT": 10,
+ "FCGI_UNKNOWN_TYPE": 11
+ },
+ "keepalive": {
+ "OFF": 0,
+ "ON": 1
+ },
+ "parser": {
+ "state": {
+ "HEADER": 0,
+ "BODY": 1,
+ "PADDING": 2
+ }
+ },
+ "general": {
+ "FCGI_HEADER_LEN": 8,
+ "FCGI_MAX_BODY": Math.pow(2,16)
+ },
+ "errors": {
+ "BUFFER_OVERFLOW": {
+ "err": 1,
+ "description": "buffer overflow"
+ },
+ "MAX_BODY_EXCEEDED": {
+ "err": 2,
+ "description": "a body greater than maximum body size was read/written"
+ }
+ },
+ "flags": {
+ "FCGI_KEEP_CONN": 1
+ },
+ "role": {
+ "FCGI_RESPONDER": 1,
+ "FCGI_AUTHORIZER": 2,
+ "FCGI_FILTER": 3
+ },
+ "protocol": {
+ "status": {
+ "FCGI_REQUEST_COMPLETE": 0,
+ "FCGI_CANT_MPX_CONN": 1,
+ "FCGI_OVERLOADED": 2,
+ "FCGI_UNKNOWN_ROLE": 3
+ }
+ },
+ "values": {
+ "FCGI_MAX_CONNS": "FCGI_MAX_CONNS",
+ "FCGI_MAX_REQS": "FCGI_MAX_REQS",
+ "FCGI_MPXS_CONNS": "FCGI_MPXS_CONNS"
+ }
View
@@ -7,109 +7,125 @@ var params = [
["HTTP_ACCEPT_ENCODING", "none"],
["HTTP_CONNECTION", "Keep-Alive"],
["HTTP_ACCEPT", "*/*"],
+ ["HTTP_METHOD", "GET"],
["HTTP_HOST", "shuttle.owner.net:82"]
];
+var payload = new Buffer("hello");
var bytesin = 0;
var bytesout = 0;
var reqid = 0;
+var keepalive = true;
+
+var clients = parseInt(process.ARGV[2] || 1);
+var host = null;
+var port = "/tmp/nginx.sock";
function writeSocket(socket, buffer) {
bytesout += buffer.length;
socket.write(buffer);
}
-function sendRequest(connection) {
- reqid++;
- connection.writer.writeHeader({
- "version": fastcgi.constants.version,
- "type": fastcgi.constants.record.FCGI_BEGIN,
- "recordId": reqid,
- "contentLength": 8,
- "paddingLength": 0
- });
- connection.writer.writeBegin({
- "role": fastcgi.constants.role.FCGI_RESPONDER,
- "flags": fastcgi.constants.keepalive.ON
+var count = 0;
+var recordId = 0;
+
+function client() {
+ var connection = new net.Stream();
+ connection.setNoDelay(false);
+ connection.setTimeout(0);
+
+ connection.addListener("connect", function() {
+ var writer = new fastcgi.writer();
+ writer.encoding = "binary";
+ var parser = new fastcgi.parser();
+ parser.encoding = "binary";
+
+ var header = {
+ "version": fastcgi.constants.version,
+ "type": fastcgi.constants.record.FCGI_BEGIN,
+ "recordId": 0,
+ "contentLength": 0,
+ "paddingLength": 0
+ };
+
+ var begin = {
+ "role": fastcgi.constants.role.FCGI_RESPONDER,
+ "flags": keepalive?fastcgi.constants.keepalive.ON:fastcgi.constants.keepalive.OFF
+ }
+
+ var paramlen = fastcgi.getParamLength(params);
+ var FCGI_BEGIN = fastcgi.constants.record.FCGI_BEGIN;
+ var FCGI_PARAMS = fastcgi.constants.record.FCGI_PARAMS;
+ var FCGI_STDIN = fastcgi.constants.record.FCGI_STDIN;
+ var FCGI_END = fastcgi.constants.record.FCGI_END;
+
+ connection.ondata = function (buffer, start, end) {
+ bytesin += (end-start);
+ parser.execute(buffer, start, end);
+ };
+
+ function sendRequest() {
+ header.type = FCGI_BEGIN;
+ header.recordId = reqid++;
+ header.contentLength = 8;
+ writer.writeHeader(header);
+ writer.writeBegin(begin);
+ writeSocket(connection, writer.tobuffer());
+ header.type = FCGI_PARAMS;
+ header.contentLength = paramlen;
+ writer.writeHeader(header);
+ writer.writeParams(params);
+ writeSocket(connection, writer.tobuffer());
+ header.contentLength = 0;
+ writer.writeHeader(header);
+ writeSocket(connection, writer.tobuffer());
+ header.type = FCGI_STDIN;
+ //header.contentLength = 5;
+ //writer.writeHeader(header);
+ //writer.writeBody(payload);
+ //writeSocket(connection, writer.tobuffer());
+ header.contentLength = 0;
+ writer.writeHeader(header);
+ writeSocket(connection, writer.tobuffer());
+ if(!keepalive) connection.end();
+ }
+
+ parser.onRecord = function(record) {
+ recordId = record.header.recordId;
+ if(record.header.type == FCGI_END) {
+ count++;
+ if(keepalive) sendRequest(connection);
+ }
+ };
+ parser.onError = function(err) {
+ console.log(JSON.stringify(err, null, "\t"));
+ };
+ sendRequest(connection);
});
- writeSocket(connection, connection.writer.tobuffer());
- connection.writer.writeHeader({
- "version": fastcgi.constants.version,
- "type": fastcgi.constants.record.FCGI_PARAMS,
- "recordId": reqid,
- "contentLength": fastcgi.getParamLength(params),
- "paddingLength": 0
+
+ connection.addListener("timeout", function() {
+ connection.end();
});
- connection.writer.writeParams(params);
- writeSocket(connection, connection.writer.tobuffer());
- connection.writer.writeHeader({
- "version": fastcgi.constants.version,
- "type": fastcgi.constants.record.FCGI_PARAMS,
- "recordId": reqid,
- "contentLength": 0,
- "paddingLength": 0
+
+ connection.addListener("close", function() {
+ setTimeout(function() {
+ connection.connect(port, host);
+ }, 500);
});
- writeSocket(connection, connection.writer.tobuffer());
- connection.writer.writeHeader({
- "version": fastcgi.constants.version,
- "type": fastcgi.constants.record.FCGI_STDIN,
- "recordId": reqid,
- "contentLength": 5,
- "paddingLength": 0
+
+ connection.addListener("end", function() {
});
- connection.writer.writeBody("hello");
- writeSocket(connection, connection.writer.tobuffer());
- connection.writer.writeHeader({
- "version": fastcgi.constants.version,
- "type": fastcgi.constants.record.FCGI_STDIN,
- "recordId": reqid,
- "contentLength": 0,
- "paddingLength": 0
+
+ connection.addListener("error", function(exception) {
+ console.log(JSON.stringify(exception));
});
- writeSocket(connection, connection.writer.tobuffer());
+
+ connection.connect(port, host);
}
-var count = 0;
-var recordId = 0;
-
-var connection = new net.Stream();
-connection.setNoDelay(true);
-connection.setTimeout(0);
-
-connection.ondata = function (buffer, start, end) {
- bytesin += (end-start);
- connection.parser.execute(buffer.slice(start, end));
-};
-
-connection.addListener("connect", function() {
- connection.writer = new fastcgi.writer();
- connection.parser = new fastcgi.parser();
- connection.parser.onRecord = function(record) {
- recordId = record.header.recordId;
- count++;
- if(record.header.type == fastcgi.constants.record.FCGI_END) {
- sendRequest(connection);
- }
- };
- connection.parser.onError = function(err) {
- console.log(JSON.stringify(err, null, "\t"));
- };
- sendRequest(connection);
-});
-
-connection.addListener("timeout", function() {
- connection.end();
-});
-
-connection.addListener("close", function() {
- connection.end();
-});
-
-connection.addListener("error", function(exception) {
- console.log(JSON.stringify(exception));
-});
-
-connection.connect("/tmp/nginx.sock");
+while(clients--) {
+ setTimeout(client, 200);
+}
var then = new Date().getTime();
var last = 0;
Oops, something went wrong.

0 comments on commit 25d7612

Please sign in to comment.