const nsIEventQueueService = Components.interfaces.nsIEventQueueService; const nsIEventQueue = Components.interfaces.nsIEventQueue;
var _eqs;
_eqs =
Components.classes["@mozilla.org/event-queue-service;1"].getService(nsIEventQueueService);
_eqs.createMonitoredThreadEventQueue();
startServer();
function startServer() {
var serv = null;
// async_listener waits for a connection and
// responds to input from the network.
// The order of action is listen, accept connection, wait
// for 'GET', deliver payload, quit
var async_listener = {
// called after connection established
onSocketAccepted: function(serv, transport) {
print("Accepted");
try {
var outstream = transport.openOutputStream(0,
0, 0);
var stream = transport.openInputStream(0, 0, 0);
var instream =
Components.classes["@mozilla.org/binaryinputstream;1"]
.createInstance(Components.interfaces.nsIBinaryInputStream);
instream.setInputStream(stream);
} catch (e) {
print("Error "+e);
}
var hexdigit = "0123456789ABCDEF";
function hex(d) {
var h = hexdigit.substr(d & 15, 1);
while (d > 15) {
d >>= 4;
h = hexdigit.substr(d & 15, 1) + h;
}
return h;
}
var requestid, role, params;
function decodeHeader() {
var buf, type, id, content, padding, read;
buf = instream.readBytes(8);
read = 8;
print("FCGI Version " + hex(buf.charCodeAt(0)));
type = buf.charCodeAt(1);
print("FCGI Type " + hex(type));
id = (buf.charCodeAt(2) << 8) +
buf.charCodeAt(3);
print("FCGI RequestID " + hex(id));
content = (buf.charCodeAt(4) << 8) +
buf.charCodeAt(5);
print("FCGI Content Length " + hex(content));
padding = buf.charCodeAt(6);
print("FCGI Padding Length " + hex(padding));
buf = instream.readBytes(content);
read += content;
switch (type) {
case 1: // begin
var flags;
requestid = id;
role = (buf.charCodeAt(0) << 8) +
buf.charCodeAt(1);
print("FCGI Role ", hex(role));
flags = buf.charCodeAt(2);
print("FCGI Flags ", hex(flags));
params = "";
break;
case 2: // abort
break;
case 4: // params
var i, name, value;
i = 0;
while (i < content) {
name = buf.charCodeAt(i++);
if (name & 0x80) {
name = ((name & 0x7F)
<< 24) + (buf.charCodeAt(i++) << 16) +
(buf.charCodeAt(i++) << 8) + buf.charCodeAt(i++);
}
value = buf.charCodeAt(i++);
if (value & 0x80) {
value = ((value & 0x7F)
<< 24) + (buf.charCodeAt(i++) << 16) +
(buf.charCodeAt(i++) << 8) + buf.charCodeAt(i++);
}
params[buf.substr(i, name)] =
buf.substr(i + name, value);
print("FCGI Param " +
buf.substr(i, name) + " : " + buf.substr(i
+ name, value));
i += name + value;
}
break;
case 5: // stdin
break;
case 8: // data
break;
case 9: // get_values
break;
default:
break;
}
buf = instream.readBytes(padding);
read += padding;
return read;
}
function sendStdout(content) {
var buf;
buf = String.fromCharCode(1, 6, requestid >> 8,
requestid & 0xff,
content.length >> 8, content.length & 0xff, 0, 0);
outstream.write(buf, buf.length);
outstream.write(content, content.length);
}
function sendEnd() {
var buf;
buf = String.fromCharCode(1, 3, requestid >> 8,
requestid & 0xff,
0, 8, 0, 0);
outstream.write(buf, buf.length);
buf = String.fromCharCode(0, 0, 0, 0, 0, 0, 0,
0);
outstream.write(buf, buf.length);
}
var dataListener = {
data : "",
return_count : 0,
found_get : 0,
onStartRequest: function(request, context){ },
onStopRequest: function(request, context,
status) {
instream.close();
outstream.close();
},
// called when there is new data
onDataAvailable: function(request, context,
inputStream, offset, count) {
print("count is \"" + count + "\"");
while (count > 0) {
count -= decodeHeader();
}
var webpage = "HTTP/1.0 200
OK\nContent-type: text/html\n\n" +
"<html>\n<head>\n<title>Hello
mozilla</title>\n</head>\n" +
"<body>\nhello
world!<br>\n</body>\n</html>\n";
// Send webpage to browser/telnet
sendStdout(webpage);
sendEnd();
instream.close();
outstream.close();
}
};
// pump takes in data in chunks asynchronously
var pump = Components.
classes["@mozilla.org/network/input-stream-pump;1"].
createInstance(Components.interfaces.nsIInputStreamPump);
pump.init(stream, -1, -1, 0, 0, false);
pump.asyncRead(dataListener, null);
},
onStopListening : function(serv, status) {
}
};
try{
var events = Components.classes["@mozilla.org/event-queue;1"];
var sock =
Components.classes["@mozilla.org/network/server-socket;1"];
serv = sock.createInstance();
serv =
serv.QueryInterface(Components.interfaces.nsIServerSocket);
serv.init(8869, true, -1);
serv.asyncListen(async_listener);
dump("*** running event loop\n");
var eq =
_eqs.getSpecialEventQueue(_eqs.CURRENT_THREAD_EVENT_QUEUE);
eq.eventLoop();
// process any remaining events before exiting
eq.processPendingEvents();
eq.stopAcceptingEvents();
eq.processPendingEvents();
} catch (e){
print("Ex: "+e);
}
print ("Exited");
}
--
Jon Smirl
[EMAIL PROTECTED]
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom
