Skip to content

Commit

Permalink
lots of testing
Browse files Browse the repository at this point in the history
  • Loading branch information
billywhizz committed Feb 19, 2011
1 parent c8879eb commit 25d7612
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 207 deletions.
83 changes: 83 additions & 0 deletions README.md 100644 → 100755
Expand Up @@ -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; 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"
}
184 changes: 100 additions & 84 deletions examples/client.js
Expand Up @@ -7,109 +7,125 @@ var params = [
["HTTP_ACCEPT_ENCODING", "none"], ["HTTP_ACCEPT_ENCODING", "none"],
["HTTP_CONNECTION", "Keep-Alive"], ["HTTP_CONNECTION", "Keep-Alive"],
["HTTP_ACCEPT", "*/*"], ["HTTP_ACCEPT", "*/*"],
["HTTP_METHOD", "GET"],
["HTTP_HOST", "shuttle.owner.net:82"] ["HTTP_HOST", "shuttle.owner.net:82"]
]; ];


var payload = new Buffer("hello");
var bytesin = 0; var bytesin = 0;
var bytesout = 0; var bytesout = 0;
var reqid = 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) { function writeSocket(socket, buffer) {
bytesout += buffer.length; bytesout += buffer.length;
socket.write(buffer); socket.write(buffer);
} }


function sendRequest(connection) { var count = 0;
reqid++; var recordId = 0;
connection.writer.writeHeader({
"version": fastcgi.constants.version, function client() {
"type": fastcgi.constants.record.FCGI_BEGIN, var connection = new net.Stream();
"recordId": reqid, connection.setNoDelay(false);
"contentLength": 8, connection.setTimeout(0);
"paddingLength": 0
}); connection.addListener("connect", function() {
connection.writer.writeBegin({ var writer = new fastcgi.writer();
"role": fastcgi.constants.role.FCGI_RESPONDER, writer.encoding = "binary";
"flags": fastcgi.constants.keepalive.ON 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({ connection.addListener("timeout", function() {
"version": fastcgi.constants.version, connection.end();
"type": fastcgi.constants.record.FCGI_PARAMS,
"recordId": reqid,
"contentLength": fastcgi.getParamLength(params),
"paddingLength": 0
}); });
connection.writer.writeParams(params);
writeSocket(connection, connection.writer.tobuffer()); connection.addListener("close", function() {
connection.writer.writeHeader({ setTimeout(function() {
"version": fastcgi.constants.version, connection.connect(port, host);
"type": fastcgi.constants.record.FCGI_PARAMS, }, 500);
"recordId": reqid,
"contentLength": 0,
"paddingLength": 0
}); });
writeSocket(connection, connection.writer.tobuffer());
connection.writer.writeHeader({ connection.addListener("end", function() {
"version": fastcgi.constants.version,
"type": fastcgi.constants.record.FCGI_STDIN,
"recordId": reqid,
"contentLength": 5,
"paddingLength": 0
}); });
connection.writer.writeBody("hello");
writeSocket(connection, connection.writer.tobuffer()); connection.addListener("error", function(exception) {
connection.writer.writeHeader({ console.log(JSON.stringify(exception));
"version": fastcgi.constants.version,
"type": fastcgi.constants.record.FCGI_STDIN,
"recordId": reqid,
"contentLength": 0,
"paddingLength": 0
}); });
writeSocket(connection, connection.writer.tobuffer());
connection.connect(port, host);
} }


var count = 0; while(clients--) {
var recordId = 0; setTimeout(client, 200);

}
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");


var then = new Date().getTime(); var then = new Date().getTime();
var last = 0; var last = 0;
Expand Down

0 comments on commit 25d7612

Please sign in to comment.