Added PostFrame, extract eventargs
This commit is contained in:
parent
dcdd941f3b
commit
cd2cce7e98
@ -5,34 +5,7 @@ var EventEmitter = require( "events" ).EventEmitter;
|
||||
var http = require( "http" );
|
||||
var https = require( "https" );
|
||||
|
||||
class HttpRequestCompleteEventArgs
|
||||
{
|
||||
constructor( Response, ResponseData )
|
||||
{
|
||||
if( ResponseData === undefined )
|
||||
{
|
||||
this.statusCode = -1;
|
||||
this.Data = new Buffer( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.statusCode = Response.statusCode;
|
||||
this.Data = ResponseData;
|
||||
}
|
||||
|
||||
this.Response = Response;
|
||||
}
|
||||
|
||||
get Headers()
|
||||
{
|
||||
return this.Response.headers;
|
||||
}
|
||||
|
||||
get ResponseString()
|
||||
{
|
||||
return this.Data.toString( "utf-8" );
|
||||
}
|
||||
}
|
||||
var CompleteEventArgs = require( "./events/HttpRequestComplete" );
|
||||
|
||||
class HttpRequest extends EventEmitter
|
||||
{
|
||||
@ -96,7 +69,7 @@ class HttpRequest extends EventEmitter
|
||||
.request( this.Options, this.OnResponseReceived.bind( this ) );
|
||||
|
||||
req.addListener( "error", ( err ) => {
|
||||
this.emit( "RequestComplete", this, new HttpRequestCompleteEventArgs( err ) )
|
||||
this.emit( "RequestComplete", this, new CompleteEventArgs( err ) )
|
||||
} );
|
||||
|
||||
req.end( this.RawPostData );
|
||||
@ -123,12 +96,12 @@ class HttpRequest extends EventEmitter
|
||||
|
||||
Response.addListener( "end", () => {
|
||||
this.emit( "RequestComplete"
|
||||
, this, new HttpRequestCompleteEventArgs( Response, ResponseData )
|
||||
, this, new CompleteEventArgs( Response, ResponseData )
|
||||
);
|
||||
} );
|
||||
}
|
||||
}
|
||||
|
||||
HttpRequest.HttpRequestCompleteEventArgs = HttpRequestCompleteEventArgs;
|
||||
HttpRequest.HttpRequestCompleteEventArgs = CompleteEventArgs;
|
||||
|
||||
module.exports = HttpRequest;
|
||||
|
91
net/PostFrame.js
Normal file
91
net/PostFrame.js
Normal file
@ -0,0 +1,91 @@
|
||||
"use strict";
|
||||
|
||||
var cl = global.botanLoader;
|
||||
var Dragonfly = global.Dragonfly;
|
||||
|
||||
var PostRequestEventArgs = cl.load( "botanss.net.events.PostRequest" );
|
||||
var EventEmitter = require( "events" ).EventEmitter;
|
||||
|
||||
class PostFrame extends EventEmitter
|
||||
{
|
||||
constructor( Http )
|
||||
{
|
||||
super();
|
||||
this.HTTP = Http;
|
||||
this.result = "Error: PostFrame is unhandled";
|
||||
this.planted = false;
|
||||
}
|
||||
|
||||
run()
|
||||
{
|
||||
var _self = this;
|
||||
var requestStr = "";
|
||||
|
||||
if( this.HTTP.request.isPost )
|
||||
{
|
||||
var Req = this.HTTP.request.raw;
|
||||
|
||||
var ReceiveData = function( data )
|
||||
{
|
||||
requestStr += data + "";
|
||||
if( 51200 < requestStr.length )
|
||||
{
|
||||
_self.result = "The size of request is too big ( 500KB < )";
|
||||
Req.removeListener( "data", ReceiveData );
|
||||
Req.removeListener( "end", ReceiveEnd );
|
||||
|
||||
_self.plantResult();
|
||||
}
|
||||
};
|
||||
|
||||
var ReceiveEnd = function()
|
||||
{
|
||||
var EventArgs = new PostRequestEventArgs( requestStr );
|
||||
|
||||
_self.emit( "PostRequest", this, EventArgs );
|
||||
if( !EventArgs.Handled )
|
||||
{
|
||||
_self.result = "Error: Unhandled Request";
|
||||
_self.plantResult();
|
||||
}
|
||||
};
|
||||
|
||||
Req.addListener( "data", ReceiveData );
|
||||
Req.addListener( "end", ReceiveEnd );
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Dragonfly.Info(
|
||||
"GET: " + encodeURI( this.HTTP.request.raw.url )
|
||||
+ " - " + this.HTTP.request.raw.headers["user-agent"]
|
||||
, Dragonfly.Visibility.VISIBLE
|
||||
);
|
||||
}
|
||||
|
||||
this.plantResult();
|
||||
}
|
||||
|
||||
plantResult()
|
||||
{
|
||||
if( !this.planted )
|
||||
{
|
||||
this.planted = true;
|
||||
if( this.HTTP )
|
||||
{
|
||||
if( !( this.result instanceof Buffer ) )
|
||||
{
|
||||
this.result = String( this.result );
|
||||
}
|
||||
|
||||
this.HTTP.response.headers["Content-Type"] = "text/plain";
|
||||
this.HTTP.response.headers["Content-Length"] = this.result.length;
|
||||
this.HTTP.response.write( this.result );
|
||||
this.HTTP.response.end();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PostFrame;
|
11
net/events/EventArgs.js
Normal file
11
net/events/EventArgs.js
Normal file
@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
class EventArgs
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
this.Handled = false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EventArgs;
|
36
net/events/HttpRequestComplete.js
Normal file
36
net/events/HttpRequestComplete.js
Normal file
@ -0,0 +1,36 @@
|
||||
"use strict";
|
||||
|
||||
var EventArgs = require( "./EventArgs" );
|
||||
|
||||
class HttpRequestComplete extends EventArgs
|
||||
{
|
||||
constructor( Response, ResponseData )
|
||||
{
|
||||
super();
|
||||
|
||||
if( ResponseData === undefined )
|
||||
{
|
||||
this.statusCode = -1;
|
||||
this.Data = new Buffer( 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
this.statusCode = Response.statusCode;
|
||||
this.Data = ResponseData;
|
||||
}
|
||||
|
||||
this.Response = Response;
|
||||
}
|
||||
|
||||
get Headers()
|
||||
{
|
||||
return this.Response.headers;
|
||||
}
|
||||
|
||||
get ResponseString()
|
||||
{
|
||||
return this.Data.toString( "utf-8" );
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HttpRequestComplete;
|
20
net/events/PostRequest.js
Normal file
20
net/events/PostRequest.js
Normal file
@ -0,0 +1,20 @@
|
||||
"use strict";
|
||||
|
||||
var qstr = require( "querystring" );
|
||||
var EventArgs = require( "./EventArgs" );
|
||||
|
||||
class PostRequestEventArgs extends EventArgs
|
||||
{
|
||||
constructor( QueryString )
|
||||
{
|
||||
super();
|
||||
this.Raw = QueryString;
|
||||
}
|
||||
|
||||
get Data()
|
||||
{
|
||||
return qstr.parse( this.Raw );
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PostRequestEventArgs;
|
Loading…
Reference in New Issue
Block a user