Http to es6 class
This commit is contained in:
		
							
								
								
									
										84
									
								
								net/Http.js
									
									
									
									
									
								
							
							
						
						
									
										84
									
								
								net/Http.js
									
									
									
									
									
								
							@@ -1,48 +1,68 @@
 | 
				
			|||||||
 | 
					"use strict";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var cl = global.botanLoader;
 | 
					var cl = global.botanLoader;
 | 
				
			||||||
var Dragonfly = global.Dragonfly;
 | 
					var Dragonfly = global.Dragonfly;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var Cookie = cl.load( "botanss.net.components.Cookie" );
 | 
					var Cookie = cl.load( "botanss.net.components.Cookie" );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var HTTP = function( req, res )
 | 
					class CResponse
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	var _self = this;
 | 
						constructor( res )
 | 
				
			||||||
	var canExit = true;
 | 
						{
 | 
				
			||||||
 | 
							this.raw = res;
 | 
				
			||||||
 | 
							this.canExit = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Simple HTTP Model
 | 
							this.statusCode = 200;
 | 
				
			||||||
	this.response = {
 | 
							this.headers = {
 | 
				
			||||||
		statusCode: 200
 | 
					 | 
				
			||||||
		, headers: {
 | 
					 | 
				
			||||||
			"Content-Type": "text/html; charset=utf-8"
 | 
								"Content-Type": "text/html; charset=utf-8"
 | 
				
			||||||
			, "Powered-By": "Botanical Framework (Node.js)"
 | 
								, "Powered-By": "Botanical Framework (Node.js)"
 | 
				
			||||||
		}
 | 
							};
 | 
				
			||||||
		, write: function( str ) { _self.response.content = str }
 | 
					
 | 
				
			||||||
		, writeLine: function( str ) { _self.response.content += str + "\n"; }
 | 
							this.content = "";
 | 
				
			||||||
		, end: function()
 | 
							this.cookie = new Cookie( "", this );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						end()
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							if( this.canExit )
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if( canExit )
 | 
								this.canExit = false;
 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				canExit = false;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
				var rc = _self.response;
 | 
								console.log( this.content );
 | 
				
			||||||
 | 
								this.raw.writeHead( this.statusCode, this.headers );
 | 
				
			||||||
				res.writeHead( rc.statusCode, rc.headers );
 | 
								this.raw.end( this.content );
 | 
				
			||||||
				res.end( rc.content );
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		, content: ''
 | 
						}
 | 
				
			||||||
		, cookie: new Cookie( "", this )
 | 
					 | 
				
			||||||
		, raw: res
 | 
					 | 
				
			||||||
	};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	this.request = {
 | 
						write( str ) { this.content = str }
 | 
				
			||||||
		uri: require('url').parse( req.url )
 | 
						writeLine( str ) { this.content += str + "\n"; }
 | 
				
			||||||
		, isPost: ( req.method == 'POST' )
 | 
					 | 
				
			||||||
		, cookie: new Cookie( req.headers.cookie, this )
 | 
					 | 
				
			||||||
		, remoteAddr: req.connection.remoteAddress
 | 
					 | 
				
			||||||
		, raw: req
 | 
					 | 
				
			||||||
	};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
};
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class CRequest
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						get isPost() { return this.raw.method == 'POST'; }
 | 
				
			||||||
 | 
						get remoteAddr() { return this.raw.connection.remoteAddress; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = HTTP;
 | 
						constructor( req )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							this.raw = req;
 | 
				
			||||||
 | 
							this.uri = require('url').parse( req.url );
 | 
				
			||||||
 | 
							this.cookie = new Cookie( req.headers.cookie, this )
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Http
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						constructor( req, res )
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							var _self = this;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Simple Http Model
 | 
				
			||||||
 | 
							this.response = new CResponse( res );
 | 
				
			||||||
 | 
							this.request = new CRequest( req );
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = Http;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user