@@ -140,10 +140,6 @@ function OutgoingMessage () {
140
140
141
141
this . output = [ ] ;
142
142
143
- this . sent_connection_header = false ;
144
- this . sent_content_length_header = false ;
145
- this . sent_transfer_encoding_header = false ;
146
-
147
143
this . closeOnFinish = false ;
148
144
this . chunked_encoding = false ;
149
145
this . should_keep_alive = true ;
@@ -159,6 +155,10 @@ OutgoingMessage.prototype.send = function (data, encoding) {
159
155
} ;
160
156
161
157
OutgoingMessage . prototype . sendHeaderLines = function ( first_line , header_lines ) {
158
+ var sent_connection_header = false ;
159
+ var sent_content_length_header = false ;
160
+ var sent_transfer_encoding_header = false ;
161
+
162
162
header_lines = header_lines || [ ] ;
163
163
164
164
// first_line in the case of request is: "GET /index.html HTTP/1.1\r\n"
@@ -172,21 +172,21 @@ OutgoingMessage.prototype.sendHeaderLines = function (first_line, header_lines)
172
172
header += field + ": " + value + CRLF ;
173
173
174
174
if ( connection_expression . exec ( field ) ) {
175
- this . sent_connection_header = true ;
175
+ sent_connection_header = true ;
176
176
if ( close_expression . exec ( value ) ) this . closeOnFinish = true ;
177
177
178
178
} else if ( transfer_encoding_expression . exec ( field ) ) {
179
- this . sent_transfer_encoding_header = true ;
179
+ sent_transfer_encoding_header = true ;
180
180
if ( chunk_expression . exec ( value ) ) this . chunked_encoding = true ;
181
181
182
182
} else if ( content_length_expression . exec ( field ) ) {
183
- this . sent_content_length_header = true ;
183
+ sent_content_length_header = true ;
184
184
185
185
}
186
186
}
187
187
188
188
// keep-alive logic
189
- if ( this . sent_connection_header == false ) {
189
+ if ( sent_connection_header == false ) {
190
190
if ( this . should_keep_alive ) {
191
191
header += "Connection: keep-alive\r\n" ;
192
192
} else {
@@ -195,7 +195,7 @@ OutgoingMessage.prototype.sendHeaderLines = function (first_line, header_lines)
195
195
}
196
196
}
197
197
198
- if ( this . sent_content_length_header == false && this . sent_transfer_encoding_header == false ) {
198
+ if ( sent_content_length_header == false && sent_transfer_encoding_header == false ) {
199
199
if ( this . use_chunked_encoding_by_default ) {
200
200
header += "Transfer-Encoding: chunked\r\n" ;
201
201
this . chunked_encoding = true ;
@@ -251,7 +251,11 @@ function ClientRequest (method, uri, header_lines) {
251
251
OutgoingMessage . call ( this ) ;
252
252
253
253
this . should_keep_alive = false ;
254
- this . use_chunked_encoding_by_default = false ;
254
+ if ( method === "GET" || method === "HEAD" ) {
255
+ this . use_chunked_encoding_by_default = false ;
256
+ } else {
257
+ this . use_chunked_encoding_by_default = true ;
258
+ }
255
259
this . closeOnFinish = true ;
256
260
257
261
this . sendHeaderLines ( method + " " + uri + " HTTP/1.1\r\n" , header_lines ) ;
@@ -329,7 +333,7 @@ function createIncomingMessageStream (connection, incoming_listener) {
329
333
/* Returns true if the message queue is finished and the connection
330
334
* should be closed. */
331
335
function flushMessageQueue ( connection , queue ) {
332
- if ( connection . readyState === "closed" || connection . readyState === "readOnly ") {
336
+ if ( connection . readyState !== "open" && connection . readyState !== "writeOnly ") {
333
337
return false ;
334
338
}
335
339
@@ -374,18 +378,17 @@ function connectionListener (connection) {
374
378
}
375
379
} ) ;
376
380
377
- var flushResponse = function ( ) {
378
- if ( flushMessageQueue ( connection , responses ) ) {
379
- connection . fullClose ( ) ;
380
- }
381
- } ;
382
381
383
382
createIncomingMessageStream ( connection , function ( incoming , should_keep_alive ) {
384
383
var req = incoming ;
385
384
386
385
var res = new ServerResponse ( connection ) ;
387
386
res . should_keep_alive = should_keep_alive ;
388
- res . addListener ( "flush" , flushResponse ) ;
387
+ res . addListener ( "flush" , function ( ) {
388
+ if ( flushMessageQueue ( connection , responses ) ) {
389
+ connection . fullClose ( ) ;
390
+ }
391
+ } ) ;
389
392
responses . push ( res ) ;
390
393
391
394
connection . server . emit ( "request" , [ req , res ] ) ;
@@ -405,6 +408,7 @@ node.http.createClient = function (port, host) {
405
408
client . connect ( port , host ) ; // reconnect
406
409
return ;
407
410
}
411
+ //node.debug("client flush readyState = " + client.readyState);
408
412
if ( req == requests [ 0 ] ) flushMessageQueue ( client , [ req ] ) ;
409
413
} ) ;
410
414
requests . push ( req ) ;
@@ -437,7 +441,7 @@ node.http.createClient = function (port, host) {
437
441
} ) ;
438
442
439
443
createIncomingMessageStream ( client , function ( res ) {
440
- //node.debug("incoming response!");
444
+ //node.debug("incoming response!");
441
445
442
446
res . addListener ( "complete" , function ( ) {
443
447
//node.debug("request complete disconnecting. readyState = " + client.readyState);
0 commit comments