|
|
jsfastcgi
jsfastcgi module
jsfastcgi module
home > JSLibs > jsfastcgi -![]()
Description
jsfastcgi is a wrapper to FastCGI, an open extension to CGI that provides high performance without the limitations of server specific APIs.
Static Functions
- integer Accept()
Accepts a new request from the HTTP server. This function wait for a new request, an error or an end signal. This is a blocking function.If the return value is < 0, the program have to end its execution.
- string|object GetParam( key )
Obtain value of FCGI parameter. For CGI/1.1 parameters, see. http://hoohoo.ncsa.uiuc.edu/cgi/env.htmlIf the key argument is ommited, the whole list of parameter is returned in an object.
example 1:
var userAgent = GetParam('HTTP_USER_AGENT');example 2:
var params = GetParam(); var userAgent = params.HTTP_USER_AGENT'; // or params['HTTP_USER_AGENT'];
- string Read( amount )
Reads up to amount consecutive bytes from the input stream. If the resulting string is empty, the end of input has been reached.
- string Write( data )
Write data to the output stream. If all data cannot be sent, the function returns the unsent data. If the function returns an empty string, everything has been sent so everything goes well.beware:
In javascript, an empty string is evaluated as false.
- Flush()
Flushes any buffered output. useful for Server-push.note:
Accept function does an implicit flush.note 2:
Flush may reduce performance by increasing the total number of operating system calls the application makes.
- ShutdownPending()
Informs the module that it should finish after handling the current request.
- Log( message )
Send a message to the web browser log.
- string URLEncode( string )
URL encode a string.
- string URLDecode( string )
URL decode a string.
Examples
This program is the request loop of the fastcgi. Its rule is to accept a request and execute the requested script.
LoadModule('jsio');
LoadModule('jsstd');
LoadModule('jsfastcgi');
while (Accept() >= 0) {
try {
var res = Exec( GetParam('SCRIPT_FILENAME') );
} catch(ex) {
Log( ex.line );
}
}This is the script to execute
var queryData = {};
for each ( var item in GetParam('QUERY_STRING').split('&') ) {
var [key,value] = item.split('=');
queryData[key] = URLDecode(value);
}
function CGIVariableList() {
var fcgiParams = GetParam();
var list = <ul/>;
for ( var k in fcgiParams )
list += <li><b>{k}</b><pre>{fcgiParams[k]}</pre></li>;
return list;
}
Write( "Content-type: text/html; charset=iso-8859-1\r\n\r\n" ); // cgi response: http://www.ietf.org/rfc/rfc3875
Write( '<?xml version="1.0" encoding="iso-8859-1"?>' );
Write( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' );
Write(
<html>
<head>
<title>teswt</title>
<style>
body {
background-color: {queryData.background_color};
}
</style>
</head>
<body>
<form action={GetParam('SCRIPT_NAME')}>
<textarea name="text">{queryData.text} </textarea>
<hr/>
<input type="submit"/>
<input type="submit" name="background_color" value="red"/>
<input type="submit" name="background_color" value="green"/>
<input type="submit" name="background_color" value="blue"/>
</form>
<H1>Expanded query string</H1>
{queryData.toSource()}
<H1>CGI/1.1 variables</H1>
{CGIVariableList()}
</body>
</html>
);
Sign in to add a comment


It seems very cool. Expecting...