@@ -27,13 +27,14 @@ module.exports = http.createServer(function(req, res) {
27
27
dom . add ( res ) ;
28
28
}
29
29
30
- // URL format: /<type>/<length>/<chunks>/<responseBehavior>
30
+ // URL format: /<type>/<length>/<chunks>/<responseBehavior>/chunkedEnc
31
31
var params = req . url . split ( '/' ) ;
32
32
var command = params [ 1 ] ;
33
33
var body = '' ;
34
34
var arg = params [ 2 ] ;
35
35
var n_chunks = parseInt ( params [ 3 ] , 10 ) ;
36
36
var resHow = ( params . length >= 5 ? params [ 4 ] : 'normal' ) ;
37
+ var chunkedEnc = ( params . length >= 6 && params [ 5 ] === 'false' ? false : true ) ;
37
38
var status = 200 ;
38
39
39
40
var n , i ;
@@ -95,48 +96,43 @@ module.exports = http.createServer(function(req, res) {
95
96
96
97
// example: http://localhost:port/bytes/512/4
97
98
// 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 )
103
105
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 )
107
112
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 ) {
110
118
res . writeHead ( status , {
111
119
'Content-Type' : 'text/plain' ,
112
120
'Transfer-Encoding' : 'chunked'
113
121
} ) ;
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 ) {
117
131
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 )
120
133
res . write ( body . slice ( i * step , i * step + step ) ) ;
121
- }
122
134
res . end ( body . slice ( ( n_chunks - 1 ) * step ) ) ;
123
135
} 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
- }
140
136
res . end ( body ) ;
141
137
}
142
138
} ) ;
0 commit comments