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
);
}
}

View File

@ -5,11 +5,20 @@ const cl = global.botanLoader;
const Cookie = cl.load( "botanss.net.components.Cookie" );
const STORE_RAW = 0;
const STORE_GZ = 1;
const STORE_DEFLATE = 2;
class PageCache
{
constructor()
{
this.cache = {};
this.cache = [];
this.cache[ STORE_RAW ] = {};
this.cache[ STORE_GZ ] = {};
this.cache[ STORE_DEFLATE ] = {};
this.excepts = {};
setInterval( () => {
@ -49,7 +58,10 @@ class PageCache
if( res.headers[ "Location" ] )
c.headers[ "Location" ] = res.headers[ "Location" ];
this.cache[ key ] = c;
if( res.headers[ "Content-Encoding" ] )
c.headers[ "Content-Encoding" ] = res.headers[ "Content-Encoding" ];
this.cache[ this.__encRes( res ) ][ key ] = c;
Dragonfly.Debug( "StoreCache: \"" + key + "\", expire " + new Date( expires ) );
}
@ -70,7 +82,9 @@ class PageCache
if( cookie.get( "sid" ) in this.excepts ) return false;
var url = req.url;
if( url in this.cache )
var store = this.cache[ this.__encReq( req ) ];
if( url in store )
{
Dragonfly.Info(
"[C] "
@ -80,7 +94,7 @@ class PageCache
, Dragonfly.Visibility.VISIBLE
);
var c = this.cache[ url ];
var c = store[ url ];
res.writeHead( c.statusCode, c.headers );
res.end( c.data );
@ -89,6 +103,20 @@ class PageCache
return false;
}
__encRes( res )
{
return { "deflate": STORE_DEFLATE, "gzip": STORE_GZ }[ res.headers[ "Content-Encoding" ] ] || STORE_RAW;
}
__encReq( req )
{
var enc = req.headers[ "accept-encoding" ] || "";
if( ~enc.indexOf( "deflate" ) ) return STORE_DEFLATE;
else if( ~enc.indexOf( "gzip" ) ) return STORE_GZ;
return STORE_RAW;
}
}
module.exports = PageCache;