@@ -27,13 +27,14 @@ module.exports = http.createServer(function(req, res) {
2727 dom . add ( res ) ;
2828 }
2929
30- // URL format: /<type>/<length>/<chunks>/<responseBehavior>
30+ // URL format: /<type>/<length>/<chunks>/<responseBehavior>/chunkedEnc
3131 var params = req . url . split ( '/' ) ;
3232 var command = params [ 1 ] ;
3333 var body = '' ;
3434 var arg = params [ 2 ] ;
3535 var n_chunks = parseInt ( params [ 3 ] , 10 ) ;
3636 var resHow = ( params . length >= 5 ? params [ 4 ] : 'normal' ) ;
37+ var chunkedEnc = ( params . length >= 6 && params [ 5 ] === 'false' ? false : true ) ;
3738 var status = 200 ;
3839
3940 var n , i ;
@@ -95,48 +96,43 @@ module.exports = http.createServer(function(req, res) {
9596
9697 // example: http://localhost:port/bytes/512/4
9798 // sends a 512 byte body in 4 chunks of 128 bytes
98- if ( n_chunks > 0 ) {
99- switch ( resHow ) {
100- case 'setHeader' :
101- res . statusCode = status ;
102- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
99+ var len = body . length ;
100+ switch ( resHow ) {
101+ case 'setHeader' :
102+ res . statusCode = status ;
103+ res . setHeader ( 'Content-Type' , 'text/plain' ) ;
104+ if ( chunkedEnc )
103105 res . setHeader ( 'Transfer-Encoding' , 'chunked' ) ;
104- break ;
105- case 'setHeaderWH' :
106- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
106+ else
107+ res . setHeader ( 'Content-Length' , len . toString ( ) ) ;
108+ break ;
109+ case 'setHeaderWH' :
110+ res . setHeader ( 'Content-Type' , 'text/plain' ) ;
111+ if ( chunkedEnc )
107112 res . writeHead ( status , { 'Transfer-Encoding' : 'chunked' } ) ;
108- break ;
109- default :
113+ else
114+ res . writeHead ( status , { 'Content-Length' : len . toString ( ) } ) ;
115+ break ;
116+ default :
117+ if ( chunkedEnc ) {
110118 res . writeHead ( status , {
111119 'Content-Type' : 'text/plain' ,
112120 'Transfer-Encoding' : 'chunked'
113121 } ) ;
114- }
115- // send body in chunks
116- var len = body . length ;
122+ } else {
123+ res . writeHead ( status , {
124+ 'Content-Type' : 'text/plain' ,
125+ 'Content-Length' : len . toString ( )
126+ } ) ;
127+ }
128+ }
129+ // send body in chunks
130+ if ( n_chunks > 1 ) {
117131 var step = Math . floor ( len / n_chunks ) || 1 ;
118-
119- for ( i = 0 , n = ( n_chunks - 1 ) ; i < n ; ++ i ) {
132+ for ( i = 0 , n = ( n_chunks - 1 ) ; i < n ; ++ i )
120133 res . write ( body . slice ( i * step , i * step + step ) ) ;
121- }
122134 res . end ( body . slice ( ( n_chunks - 1 ) * step ) ) ;
123135 } else {
124- switch ( resHow ) {
125- case 'setHeader' :
126- res . statusCode = status ;
127- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
128- res . setHeader ( 'Content-Length' , body . length . toString ( ) ) ;
129- break ;
130- case 'setHeaderWH' :
131- res . setHeader ( 'Content-Type' , 'text/plain' ) ;
132- res . writeHead ( status , { 'Content-Length' : body . length . toString ( ) } ) ;
133- break ;
134- default :
135- res . writeHead ( status , {
136- 'Content-Type' : 'text/plain' ,
137- 'Content-Length' : body . length . toString ( )
138- } ) ;
139- }
140136 res . end ( body ) ;
141137 }
142138} ) ;
0 commit comments