Handling compressions

This commit is contained in:
2016-12-21 18:34:55 +08:00
parent a7edb55d4c
commit 3a3a22b37a
3 changed files with 69 additions and 13 deletions

View File

@@ -46,6 +46,7 @@ class CRequest
constructor( req, Http )
{
this.raw = req;
this.headers = req.headers;
this.uri = require('url').parse( req.url );
this.cookie = new Cookie( req.headers.cookie, Http );
}

View File

@@ -1,5 +1,7 @@
"use strict";
const zlib = require( "zlib" );
const cl = global.botanLoader;
const Dragonfly = global.Dragonfly;
const PageCache = global.botanPageCache;
@@ -129,12 +131,30 @@ class WebFrame
this.result = new Buffer( this.result + "" );
}
this.HTTP.response.headers["Content-Length"] = this.result.length;
this.HTTP.response.write( this.result );
this.HTTP.response.end();
Dragonfly.Debug( "Result Planted" );
this.__storeCache( cache, ttl );
var acceptEncoding = this.HTTP.request.headers[ "accept-encoding" ] || "";
if( ~acceptEncoding.indexOf( "deflate" ) )
{
zlib.deflate( this.result, ( e, buff ) => {
if( e ) throw e;
this.HTTP.response.headers[ "Content-Encoding" ] = "deflate";
this.__writeOut( buff );
this.__storeCache( buff, cache, ttl );
} );
}
else if( ~acceptEncoding.indexOf( "gzip" ) )
{
zlib.gzip( this.result, ( e, buff ) => {
if( e ) throw e;
this.HTTP.response.headers[ "Content-Encoding" ] = "gzip";
this.__writeOut( buff );
this.__storeCache( buff, cache, ttl );
} );
}
else
{
this.__writeOut( this.result );
this.__storeCache( this.result, cache, ttl );
}
}
// Release resources
@@ -144,7 +164,15 @@ class WebFrame
}
}
__storeCache( cache, ttl )
__writeOut( data )
{
this.HTTP.response.headers["Content-Length"] = data.length;
this.HTTP.response.write( data );
this.HTTP.response.end();
Dragonfly.Debug( "Result Planted" );
}
__storeCache( data, cache, ttl )
{
if( this.allowCache && cache && PageCache )
{
@@ -152,8 +180,7 @@ class WebFrame
PageCache.store(
this.HTTP.request.raw
, this.HTTP.response
, this.result
, ttl
, data, ttl
);
}
}