diff --git a/jsgi-node.js b/jsgi-node.js index e89c0cb..c6bd1e1 100644 --- a/jsgi-node.js +++ b/jsgi-node.js @@ -1,7 +1,7 @@ /* JSGI 0.3 Adapter for Node -To use provide a JSGI application (can be application stack) to the start +To use provide a JSGI application (can be application stack) to the start function: require("jsgi-node").start(function(request){ @@ -14,7 +14,7 @@ function: }); }); -This adapter should conform to the JSGI 0.3 (with promises) for full +This adapter should conform to the JSGI 0.3 (with promises) for full asynchronous support. For example: var fs = require("promised-io/fs"); @@ -37,6 +37,7 @@ var function Request( request ) { var url = request.url; var questionIndex = url.indexOf("?"); + this._env = {}; this.method = request.method; this.nodeRequest = request; this.headers = request.headers; @@ -51,7 +52,7 @@ function Request( request ) { if(this.method != "GET"){ // optimize GET this.body = new Input( request ); } - + } Request.prototype = { @@ -70,14 +71,17 @@ Request.prototype = { return this._env || (this._env = {}); }, scriptName: "", - scheme:"http", + get scheme(){ + return this.nodeRequest.connection.encrypted ? "https" : "http"; + }, get host(){ var host = this.headers.host; return host ? host.split(":")[0] : ""; }, get port(){ var host = this.headers.host; - return host ? (host.split(":")[1] || 80) : 80; + var isSsl = !!this.nodeRequest.connection.encrypted; + return host ? (host.split(":")[1] || (isSsl ? 443 : 80)) : 80; }, get remoteAddr(){ return this.nodeRequest.connection.remoteAddress; @@ -89,11 +93,11 @@ Request.prototype = { function Input( request ) { - var + var inputBuffer = [], waitingForLength = Infinity; function callback(data){ - inputBuffer.push(data); + inputBuffer.push(data); } var deferred = defer(); request @@ -103,7 +107,7 @@ function Input( request ) { .addListener( "end", function() { deferred.resolve(); }); - + this.forEach = function (each) { if (this.encoding) { request.setBodyEncoding( this.encoding ); @@ -160,6 +164,7 @@ function Response( response, stream ) { try { if ( typeof data.body === "string" ) { response.write(data.body); + response.end(); } else if ( typeof data.body.forEach !== "function" ) { throw new Error("The body does not have a forEach function"); @@ -208,7 +213,7 @@ function Response( response, stream ) { }catch(e3){ sys.puts(e3.stack); } - } + } } } @@ -226,7 +231,7 @@ function Listener( app ) { jsgiResponse = app( request ) } catch( error ) { jsgiResponse = { status:500, headers:{}, body:[error.stack] }; - } + } respond( jsgiResponse ); }); } @@ -238,10 +243,10 @@ start.start = start; function start( app, options ) { app = new Listener( app ); options = options || {}; - + var port = options.port || 8080, http; - + if ( options.ssl ) { http = require( "https" ).createServer( options.ssl, app ).listen( port ); } else { @@ -249,6 +254,6 @@ function start( app, options ) { } sys.puts( "Server running on port " + port ); - return http; + return http; }; module.exports = start; diff --git a/package.json b/package.json index 1f19e84..674c70a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "jsgi-node", - "version": "0.3.0", + "name": "jsgi", + "version": "1.0.1", "directories": { "lib": "." }, "main": "./jsgi-node", "description": "JSGI middleware server for NodeJS",