New class conditional stream
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
				
			|||||||
var cl = global.botanLoader;
 | 
					var cl = global.botanLoader;
 | 
				
			||||||
var Dragonfly = global.Dragonfly;
 | 
					var Dragonfly = global.Dragonfly;
 | 
				
			||||||
 | 
					var CondStream = cl.load( "botanss.utils.CondStream" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var FatalError = cl.load( "botanss.errors.FatalError" );
 | 
					var FatalError = cl.load( "botanss.errors.FatalError" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -63,14 +64,15 @@ Framework.prototype.run = function()
 | 
				
			|||||||
	var method = "GET";
 | 
						var method = "GET";
 | 
				
			||||||
	if( this.HTTP.request.isPost )
 | 
						if( this.HTTP.request.isPost )
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		this.HTTP.request.raw.addListener( "data", function( data )
 | 
							_self.requestStr = new CondStream( "/tmp/", 2048 );
 | 
				
			||||||
		{
 | 
					
 | 
				
			||||||
			_self.requestStr += data.toString();
 | 
							this.HTTP.request.raw.addListener(
 | 
				
			||||||
		})
 | 
								"data" , ( x ) => _self.requestStr.write( x )
 | 
				
			||||||
		this.HTTP.request.raw.addListener( "end", function()
 | 
							);
 | 
				
			||||||
		{
 | 
					
 | 
				
			||||||
			_self.parseResult();
 | 
							this.HTTP.request.raw.addListener(
 | 
				
			||||||
		});
 | 
								"end", () => _self.requestStr.end( () => _self.parseResult() )
 | 
				
			||||||
 | 
							);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		method = "POST";
 | 
							method = "POST";
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,48 +1,16 @@
 | 
				
			|||||||
var encodeCookie = function( cookie )
 | 
					var cl = global.botanLoader;
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	var cookieStr = "";
 | 
					 | 
				
			||||||
	var p = "";
 | 
					 | 
				
			||||||
	var e = "";
 | 
					 | 
				
			||||||
	for( var i in cookie )
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		switch( i.toLowerCase() )
 | 
					 | 
				
			||||||
		{
 | 
					 | 
				
			||||||
			case "path":
 | 
					 | 
				
			||||||
				p = cookie[i];
 | 
					 | 
				
			||||||
				continue;
 | 
					 | 
				
			||||||
			case "expires":
 | 
					 | 
				
			||||||
				e = cookie[i];
 | 
					 | 
				
			||||||
				continue;
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		cookieStr += i + "=" + cookie[i] + ";";
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Path at tail
 | 
					var util = require( "util" );
 | 
				
			||||||
	cookieStr += "Path=" + p + ";" + " Expires=" + e + ";";
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return cookieStr; 
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var WebParam = cl.load( "botanss.utils.WebParam" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var Cookie = function( cookieStr, HTTP )
 | 
					var Cookie = function( cookieStr, HTTP )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	var list = {};
 | 
						WebParam.call( this, cookieStr );
 | 
				
			||||||
 | 
					 | 
				
			||||||
	cookieStr && cookieStr.split( ";" ).forEach( function( cookie )
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		var parts = cookie.split( "=" );
 | 
					 | 
				
			||||||
		list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
 | 
					 | 
				
			||||||
	} );
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	this.__cookie = list;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	this.HTTP = HTTP;
 | 
						this.HTTP = HTTP;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Cookie.prototype.set = function( name, value )
 | 
					util.inherits( Cookie, WebParam );
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	this.__cookie[ name ] = value;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Cookie.prototype.seth = function( name, value )
 | 
					Cookie.prototype.seth = function( name, value )
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -50,15 +18,26 @@ Cookie.prototype.seth = function( name, value )
 | 
				
			|||||||
	this.HTTP.response.headers[ "Set-Cookie" ] = this.toString();
 | 
						this.HTTP.response.headers[ "Set-Cookie" ] = this.toString();
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Cookie.prototype.get = function( name )
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	return this.__cookie[ name ];
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Cookie.prototype.toString = function()
 | 
					Cookie.prototype.toString = function()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return encodeCookie( this.__cookie );
 | 
						var cookieStr = "";
 | 
				
			||||||
 | 
						var p = "";
 | 
				
			||||||
 | 
						var e = "";
 | 
				
			||||||
 | 
						for( var i in this.param )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							switch( i.toLowerCase() )
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								case "path":
 | 
				
			||||||
 | 
									p = this.param[i];
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
								case "expires":
 | 
				
			||||||
 | 
									e = this.param[i];
 | 
				
			||||||
 | 
									continue;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							cookieStr += i + "=" + this.param[i] + ";";
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						cookieStr += "Path=" + p + ";" + " Expires=" + e + ";";
 | 
				
			||||||
 | 
						return cookieStr;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
module.exports = Cookie;
 | 
					module.exports = Cookie;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										130
									
								
								utils/CondStream.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								utils/CondStream.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,130 @@
 | 
				
			|||||||
 | 
					var fs = require( "fs" );
 | 
				
			||||||
 | 
					var crypto = require( "crypto" );
 | 
				
			||||||
 | 
					var util = require( "util" );
 | 
				
			||||||
 | 
					var ReadStream = require( "stream" ).Readable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var ConditionalStream = function( tmpPath, triggerLimit )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if( !tmpPath )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							throw new Error( "Temp path is not defined" );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.size = 0;
 | 
				
			||||||
 | 
						this.limit = triggerLimit * 1024;
 | 
				
			||||||
 | 
						this.stream = false;
 | 
				
			||||||
 | 
						this.hexData = "";
 | 
				
			||||||
 | 
						this.tmpPath = tmpPath;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.file = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.__discard = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.__ended = false;
 | 
				
			||||||
 | 
						this.__finished = false;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					util.inherits( ConditionalStream, String );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ConditionalStream.prototype.write = function( data )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var _self = this;
 | 
				
			||||||
 | 
						this.size += data.length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( this.stream )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							this.hexData = false;
 | 
				
			||||||
 | 
							this.stream.write( data );
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.hexData += data.toString( "hex" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Trigger
 | 
				
			||||||
 | 
						if( this.limit < this.size )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							this.file = this.tmpPath + "ss_" + crypto.randomBytes( 8 ).toString( "hex" );
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
							this.stream = fs.createWriteStream( this.file );
 | 
				
			||||||
 | 
							this.stream.addListener( "finish", this.__end.bind( this ) );
 | 
				
			||||||
 | 
							this.stream.write( this.hexData, "hex" );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ConditionalStream.prototype.end = function( handler )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var _self = this;
 | 
				
			||||||
 | 
						if( this.stream )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							this.stream.addListener( "close", function() {
 | 
				
			||||||
 | 
								_self.__finished = true;
 | 
				
			||||||
 | 
							   	handler( _self );
 | 
				
			||||||
 | 
							} );
 | 
				
			||||||
 | 
							this.stream.end();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							setTimeout( function()
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								_self.__finished = true;
 | 
				
			||||||
 | 
								handler( _self )
 | 
				
			||||||
 | 
							} , 0 );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ConditionalStream.prototype.discard = function()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var _self = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.__discard = true;
 | 
				
			||||||
 | 
						if( this.__finished )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							fs.unlink( this.file, function()
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								if( _self.__error ) throw new Error( _self.__error );
 | 
				
			||||||
 | 
							} );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ConditionalStream.prototype.__end = function()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						this.__finished = true;
 | 
				
			||||||
 | 
						if( this.__discard ) this.discard();
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ConditionalStream.prototype.toString = function( enc )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if( this.stream )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							this.discard();
 | 
				
			||||||
 | 
							this.__error = "Received data is too large to process";
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return new Buffer( this.hexData, "hex" ).toString( enc );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ConditionalStream.prototype.resultStream = function()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var _self = this;
 | 
				
			||||||
 | 
						if( !this.__finished ) throw new Error( "Data is not finished yet" );
 | 
				
			||||||
 | 
						if( this.__discard ) throw new Error( "Data is discarded" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if( this.stream )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							var rt = fs.createReadStream( this.file );
 | 
				
			||||||
 | 
							rt.addListener( "close", () => _self.discard() );
 | 
				
			||||||
 | 
							return rt;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var st = new ReadStream();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						setTimeout( function() {
 | 
				
			||||||
 | 
							st.push( this.hexData, "hex" );
 | 
				
			||||||
 | 
							st.push( null );
 | 
				
			||||||
 | 
						}, 0 );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return st;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = ConditionalStream;
 | 
				
			||||||
							
								
								
									
										47
									
								
								utils/WebParam.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								utils/WebParam.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					var WebParam = function( paramStr )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var list = {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						paramStr && paramStr.split( ";" ).forEach( function( param )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							var parts = param.split( "=" );
 | 
				
			||||||
 | 
							list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
 | 
				
			||||||
 | 
						} );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						this.param = list;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WebParam.prototype.set = function( name, value )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						this.param[ name ] = value;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WebParam.prototype.get = function( name )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return this.param[ name ];
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WebParam.prototype.toString = function()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var paramStr = "";
 | 
				
			||||||
 | 
						for( var i in param )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							paramStr += i + "=" + param[i] + ";";
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return paramStr;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WebParam.ExtractHeader = function( hstr )
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						var headers = {};
 | 
				
			||||||
 | 
						hstr.split( "\r\n" ).forEach( function( v )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if( !v ) return;
 | 
				
			||||||
 | 
							var colx = v.indexOf( ':' );
 | 
				
			||||||
 | 
							headers[ v.substring( 0, colx ) ] = new WebParam( v.substr( colx + 1 ) );
 | 
				
			||||||
 | 
						} );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return headers;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = WebParam;
 | 
				
			||||||
		Reference in New Issue
	
	Block a user