BotanSS/net/Http.js

66 lines
1.2 KiB
JavaScript
Raw Normal View History

2016-02-12 20:23:18 +00:00
"use strict";
2016-10-15 03:28:55 +00:00
const cl = global.botanLoader;
const Dragonfly = global.Dragonfly;
2015-04-18 14:14:36 +00:00
2016-10-15 03:28:55 +00:00
const Cookie = cl.load( "botanss.net.components.Cookie" );
2014-10-10 08:38:06 +00:00
2016-02-12 20:23:18 +00:00
class CResponse
2014-10-10 08:38:06 +00:00
{
2016-02-12 21:03:21 +00:00
constructor( res, Http )
2016-02-12 20:23:18 +00:00
{
this.raw = res;
this.canExit = true;
2014-10-10 08:38:06 +00:00
2016-02-12 20:23:18 +00:00
this.statusCode = 200;
this.headers = {
2015-01-06 07:16:35 +00:00
"Content-Type": "text/html; charset=utf-8"
, "Powered-By": "Botanical Framework (Node.js)"
2016-02-12 20:23:18 +00:00
};
2014-10-10 08:38:06 +00:00
2016-02-12 20:23:18 +00:00
this.content = "";
2016-02-12 21:03:21 +00:00
this.cookie = new Cookie( "", Http );
2016-02-12 20:23:18 +00:00
}
end()
{
if( this.canExit )
{
this.canExit = false;
2014-10-10 08:38:06 +00:00
2016-02-12 20:23:18 +00:00
this.raw.writeHead( this.statusCode, this.headers );
this.raw.end( this.content );
2014-10-10 08:38:06 +00:00
}
2016-02-12 20:23:18 +00:00
}
2014-10-10 08:38:06 +00:00
2016-02-12 20:23:18 +00:00
write( str ) { this.content = str }
writeLine( str ) { this.content += str + "\n"; }
2015-04-15 04:17:21 +00:00
2016-02-12 20:23:18 +00:00
}
class CRequest
{
get isPost() { return this.raw.method == 'POST'; }
2022-04-04 05:56:36 +00:00
get remoteAddr() { return this.raw.socket.remoteAddress; }
2016-02-12 20:23:18 +00:00
2016-02-12 21:03:21 +00:00
constructor( req, Http )
2016-02-12 20:23:18 +00:00
{
this.raw = req;
2016-12-21 10:34:55 +00:00
this.headers = req.headers;
2016-02-12 20:23:18 +00:00
this.uri = require('url').parse( req.url );
2016-06-17 01:26:33 +00:00
this.cookie = new Cookie( req.headers.cookie, Http );
2016-02-12 20:23:18 +00:00
}
}
class Http
{
constructor( req, res )
{
// Simple Http Model
2016-02-12 21:03:21 +00:00
this.response = new CResponse( res, this );
this.request = new CRequest( req, this );
2016-02-12 20:23:18 +00:00
}
}
2014-10-10 08:38:06 +00:00
2016-02-12 20:23:18 +00:00
module.exports = Http;