@@ -195,7 +195,7 @@ function Framework() {
195195
196196 this . id = null ;
197197 this . version = 1810 ;
198- this . version_header = '1.8.1-25 ' ;
198+ this . version_header = '1.8.1-26 ' ;
199199
200200 var version = process . version . toString ( ) . replace ( 'v' , '' ) . replace ( / \. / g, '' ) ;
201201 if ( version [ 1 ] === '0' )
@@ -3865,6 +3865,93 @@ Framework.prototype.responseRange = function(name, range, headers, req, res, don
38653865 return self ;
38663866} ;
38673867
3868+ /**
3869+ * Responses binary
3870+ * @param {ServerRequest } req
3871+ * @param {ServerResponse } res
3872+ * @param {String } contentType
3873+ * @param {Buffer } buffer
3874+ * @param {String } download Optional, download name.
3875+ * @param {Object } headers Optional
3876+ * @return {Framework }
3877+ */
3878+ Framework . prototype . responseBinary = function ( req , res , contentType , buffer , encoding , download , headers , done ) {
3879+
3880+ var self = this ;
3881+
3882+ if ( res . success || res . headersSent ) {
3883+ if ( done )
3884+ done ( ) ;
3885+ return self ;
3886+ }
3887+
3888+ req . clear ( true ) ;
3889+
3890+ if ( contentType . lastIndexOf ( '/' ) === - 1 )
3891+ contentType = utils . getContentType ( contentType ) ;
3892+
3893+ var accept = req . headers [ 'accept-encoding' ] || '' ;
3894+ var compress = self . config [ 'allow-gzip' ] && REQUEST_COMPRESS_CONTENTTYPE [ contentType ] && accept . lastIndexOf ( 'gzip' ) !== - 1 ;
3895+ var returnHeaders = { } ;
3896+
3897+ returnHeaders [ RESPONSE_HEADER_CACHECONTROL ] = 'public' ;
3898+ returnHeaders [ 'Vary' ] = 'Accept-Encoding' ;
3899+
3900+ if ( headers )
3901+ utils . extend ( returnHeaders , headers , true ) ;
3902+
3903+ download = download || '' ;
3904+
3905+ if ( download )
3906+ returnHeaders [ 'Content-Disposition' ] = 'attachment; filename=' + encodeURIComponent ( download ) ;
3907+
3908+ returnHeaders [ RESPONSE_HEADER_CONTENTTYPE ] = contentType ;
3909+
3910+ self . stats . response . binary ++ ;
3911+ self . _request_stats ( false , req . isStaticFile ) ;
3912+
3913+ if ( req . method === 'HEAD' ) {
3914+ if ( compress )
3915+ returnHeaders [ 'Content-Encoding' ] = 'gzip' ;
3916+ res . writeHead ( 200 , returnHeaders ) ;
3917+ res . end ( ) ;
3918+ if ( done )
3919+ done ( ) ;
3920+ if ( ! req . isStaticFile )
3921+ self . emit ( 'request-end' , req , res ) ;
3922+ return self ;
3923+ }
3924+
3925+ if ( compress ) {
3926+
3927+ returnHeaders [ 'Content-Encoding' ] = 'gzip' ;
3928+ res . writeHead ( 200 , returnHeaders ) ;
3929+
3930+ zlib . gzip ( buffer . toString ( encoding || 'binary' ) , function ( err , buffer ) {
3931+ res . end ( buffer ) ;
3932+ } ) ;
3933+
3934+ if ( done )
3935+ done ( ) ;
3936+
3937+ if ( ! req . isStaticFile )
3938+ self . emit ( 'request-end' , req , res ) ;
3939+
3940+ return self ;
3941+ }
3942+
3943+ res . writeHead ( 200 , returnHeaders ) ;
3944+ res . end ( buffer . toString ( encoding || 'binary' ) ) ;
3945+
3946+ if ( done )
3947+ done ( ) ;
3948+
3949+ if ( ! req . isStaticFile )
3950+ self . emit ( 'request-end' , req , res ) ;
3951+
3952+ return self ;
3953+ } ;
3954+
38683955/*
38693956 Set last modified header or Etag
38703957 @req {ServerRequest}
@@ -4587,31 +4674,35 @@ Framework.prototype._request = function(req, res) {
45874674 if ( self . restrictions . isAllowedIP ) {
45884675 if ( self . restrictions . allowedIP . indexOf ( req . ip ) === - 1 ) {
45894676 self . stats . response . restriction ++ ;
4590- req . connection . destroy ( ) ;
4677+ res . writeHead ( 403 ) ;
4678+ res . end ( ) ;
45914679 return self ;
45924680 }
45934681 }
45944682
45954683 if ( self . restrictions . isBlockedIP ) {
45964684 if ( self . restrictions . blockedIP . indexOf ( req . ip ) !== - 1 ) {
45974685 self . stats . response . restriction ++ ;
4598- req . connection . destroy ( ) ;
4686+ res . writeHead ( 403 ) ;
4687+ res . end ( ) ;
45994688 return self ;
46004689 }
46014690 }
46024691
46034692 if ( self . restrictions . isAllowedCustom ) {
46044693 if ( ! self . restrictions . _allowedCustom ( headers ) ) {
46054694 self . stats . response . restriction ++ ;
4606- req . connection . destroy ( ) ;
4695+ res . writeHead ( 403 ) ;
4696+ res . end ( ) ;
46074697 return self ;
46084698 }
46094699 }
46104700
46114701 if ( self . restrictions . isBlockedCustom ) {
46124702 if ( self . restrictions . _blockedCustom ( headers ) ) {
46134703 self . stats . response . restriction ++ ;
4614- req . connection . destroy ( ) ;
4704+ res . writeHead ( 403 ) ;
4705+ res . end ( ) ;
46154706 return self ;
46164707 }
46174708 }
@@ -4802,7 +4893,8 @@ Framework.prototype._request_continue = function(req, res, headers, protocol) {
48024893 self . emit ( 'request-end' , req , res ) ;
48034894 self . _request_stats ( false , false ) ;
48044895 self . stats . request . blocked ++ ;
4805- req . connection . destroy ( ) ;
4896+ res . writeHead ( 403 ) ;
4897+ res . end ( ) ;
48064898 return self ;
48074899} ;
48084900
@@ -4830,31 +4922,35 @@ Framework.prototype._upgrade = function(req, socket, head) {
48304922 if ( self . restrictions . isAllowedIP ) {
48314923 if ( self . restrictions . allowedIP . indexOf ( req . ip ) === - 1 ) {
48324924 self . stats . response . restriction ++ ;
4833- req . connection . destroy ( ) ;
4925+ res . writeHead ( 403 ) ;
4926+ res . end ( ) ;
48344927 return self ;
48354928 }
48364929 }
48374930
48384931 if ( self . restrictions . isBlockedIP ) {
48394932 if ( self . restrictions . blockedIP . indexOf ( req . ip ) !== - 1 ) {
48404933 self . stats . response . restriction ++ ;
4841- req . connection . destroy ( ) ;
4934+ res . writeHead ( 403 ) ;
4935+ res . end ( ) ;
48424936 return self ;
48434937 }
48444938 }
48454939
48464940 if ( self . restrictions . isAllowedCustom ) {
48474941 if ( ! self . restrictions . _allowedCustom ( headers ) ) {
48484942 self . stats . response . restriction ++ ;
4849- req . connection . destroy ( ) ;
4943+ res . writeHead ( 403 ) ;
4944+ res . end ( ) ;
48504945 return self ;
48514946 }
48524947 }
48534948
48544949 if ( self . restrictions . isBlockedCustom ) {
48554950 if ( self . restrictions . _blockedCustom ( headers ) ) {
48564951 self . stats . response . restriction ++ ;
4857- req . connection . destroy ( ) ;
4952+ res . writeHead ( 403 ) ;
4953+ res . end ( ) ;
48584954 return self ;
48594955 }
48604956 }
@@ -7466,7 +7562,8 @@ Subscribe.prototype.multipart = function(header) {
74667562 if ( self . route === null ) {
74677563 framework . _request_stats ( false , false ) ;
74687564 framework . stats . request . blocked ++ ;
7469- req . connection . destroy ( ) ;
7565+ self . res . writeHead ( 403 ) ;
7566+ self . res . end ( ) ;
74707567 return self ;
74717568 }
74727569
@@ -7484,7 +7581,8 @@ Subscribe.prototype.urlencoded = function() {
74847581 self . req . clear ( true ) ;
74857582 framework . stats . request . blocked ++ ;
74867583 framework . _request_stats ( false , false ) ;
7487- self . req . connection . destroy ( ) ;
7584+ self . res . writeHead ( 403 ) ;
7585+ self . res . end ( ) ;
74887586 return self ;
74897587 }
74907588
@@ -10656,30 +10754,32 @@ Controller.prototype.redirect = function(url, permanent) {
1065610754/**
1065710755 * A binary response
1065810756 * @param {Buffer } buffer
10757+ * @param {String } contentType
10758+ * @param {String } type Transformation type: `binary`, `utf8`, `ascii`.
10759+ * @param {String } download Optional, download name.
10760+ * @param {Object } headers Optional, additional headers.
1065910761 * @return {FrameworkController }
1066010762 */
10661- Controller . prototype . binary = function ( buffer ) {
10662-
10763+ Controller . prototype . binary = function ( buffer , contentType , type , download , headers ) {
1066310764 var self = this ;
1066410765 var res = self . res ;
10665-
10666- if ( res . success || ! self . isConnected )
10766+ if ( self . res . success || self . res . headersSent || ! self . isConnected )
1066710767 return self ;
1066810768
10669- var req = self . req ;
10670-
10671- self . subscribe . success ( ) ;
10672-
10673- req . clear ( true ) ;
10674-
10675- res . success = true ;
10676- res . write ( buffer . toString ( 'binary' ) , 'binary' ) ;
10677- res . end ( ) ;
10769+ if ( typeof ( type ) === OBJECT ) {
10770+ var tmp = type ;
10771+ type = download ;
10772+ download = headers ;
10773+ headers = tmp ;
10774+ }
1067810775
10679- framework . _request_stats ( false , false ) ;
10680- framework . emit ( 'request-end' , req , res ) ;
10681- framework . stats . response . binary ++ ;
10776+ if ( typeof ( download ) === OBJECT ) {
10777+ headers = download ;
10778+ download = headers ;
10779+ }
1068210780
10781+ self . subscribe . success ( ) ;
10782+ framework . responseBinary ( self . req , res , contentType , buffer , type , download , headers ) ;
1068310783 return self ;
1068410784} ;
1068510785
0 commit comments