billywhizz
4/3/2011 - 1:54 AM

testing fastcgi post to php

testing fastcgi post to php

var net = require("net");
var fastcgi = require("fastcgi");

var reqid = 0;
var post = "fname=aaa&lname=fff";

var params = [
	["REQUEST_METHOD", "POST"],
	["SCRIPT_FILENAME", process.ARGV[2]],
	["CONTENT_TYPE", "application/x-www-form-urlencoded"],
	["CONTENT_LENGTH", post.length]
];

function sendRequest(connection) {
	try {
		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.OFF
		});
		connection.write(connection.writer.tobuffer());
		console.log(fastcgi.getParamLength(params));
		connection.writer.writeHeader({
			"version": fastcgi.constants.version,
			"type": fastcgi.constants.record.FCGI_PARAMS,
			"recordId": reqid,
			"contentLength": fastcgi.getParamLength(params),
			"paddingLength": 0
		});
		connection.writer.writeParams(params);
		connection.write(connection.writer.tobuffer());
		connection.writer.writeHeader({
			"version": fastcgi.constants.version,
			"type": fastcgi.constants.record.FCGI_PARAMS,
			"recordId": reqid,
			"contentLength": 0,
			"paddingLength": 0
		});
		connection.write(connection.writer.tobuffer());
		connection.writer.writeHeader({
			"version": fastcgi.constants.version,
			"type": fastcgi.constants.record.FCGI_STDIN,
			"recordId": reqid,
			"contentLength": post.length,
			"paddingLength": 0
		});
		connection.writer.writeBody(post);
		connection.write(connection.writer.tobuffer());
		connection.writer.writeHeader({
			"version": fastcgi.constants.version,
			"type": fastcgi.constants.record.FCGI_STDIN,
			"recordId": reqid,
			"contentLength": 0,
			"paddingLength": 0
		});
		connection.write(connection.writer.tobuffer());
	}
	catch(ex) {
		connection.end();
	}
}

var count = 0;
var recordId = 0;

function client() {
	var connection = new net.Stream();
	connection.setNoDelay(true);
	connection.setTimeout(5000);
	
	connection.ondata = function (buffer, start, end) {
		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) {
			console.log(JSON.stringify(record, null, "\t"));
			if(record.header.type == fastcgi.constants.record.FCGI_STDOUT) {
				console.log(record.body);
			}
			count++;
			recordId = record.header.recordId;
		};

		connection.parser.onHeader = function(header) {
			if(header.type == fastcgi.constants.record.FCGI_STDOUT) {

			}
		};
		
		connection.parser.onError = function(err) {
			console.log(JSON.stringify(err, null, "\t"));
		};
		sendRequest(connection);
	});
	
	connection.addListener("timeout", function() {
		connection.end();
	});
	
	connection.addListener("end", function() {
	});
	
	connection.addListener("close", function() {
	});
	
	connection.addListener("error", function(exception) {
		console.log(JSON.stringify(exception));
		connection.end();
	});
	
	connection.connect("/tmp/php.sock");
}

client();