@@ -164,6 +164,40 @@ function ClientRequest(options, cb) {
164
164
this . once ( 'response' , cb ) ;
165
165
}
166
166
167
+ if ( method === 'GET' ||
168
+ method === 'HEAD' ||
169
+ method === 'DELETE' ||
170
+ method === 'OPTIONS' ||
171
+ method === 'CONNECT' ) {
172
+ this . useChunkedEncodingByDefault = false ;
173
+ } else {
174
+ this . useChunkedEncodingByDefault = true ;
175
+ }
176
+
177
+ this . _ended = false ;
178
+ this . res = null ;
179
+ this . aborted = undefined ;
180
+ this . timeoutCb = null ;
181
+ this . upgradeOrConnect = false ;
182
+ this . parser = null ;
183
+ this . maxHeadersCount = null ;
184
+
185
+ var called = false ;
186
+
187
+ if ( this . agent ) {
188
+ // If there is an agent we should default to Connection:keep-alive,
189
+ // but only if the Agent will actually reuse the connection!
190
+ // If it's not a keepAlive agent, and the maxSockets==Infinity, then
191
+ // there's never a case where this socket will actually be reused
192
+ if ( ! this . agent . keepAlive && ! Number . isFinite ( this . agent . maxSockets ) ) {
193
+ this . _last = true ;
194
+ this . shouldKeepAlive = false ;
195
+ } else {
196
+ this . _last = false ;
197
+ this . shouldKeepAlive = true ;
198
+ }
199
+ }
200
+
167
201
var headersArray = Array . isArray ( options . headers ) ;
168
202
if ( ! headersArray ) {
169
203
if ( options . headers ) {
@@ -173,6 +207,7 @@ function ClientRequest(options, cb) {
173
207
this . setHeader ( key , options . headers [ key ] ) ;
174
208
}
175
209
}
210
+
176
211
if ( host && ! this . getHeader ( 'host' ) && setHost ) {
177
212
var hostHeader = host ;
178
213
@@ -191,45 +226,25 @@ function ClientRequest(options, cb) {
191
226
}
192
227
this . setHeader ( 'Host' , hostHeader ) ;
193
228
}
194
- }
195
229
196
- if ( options . auth && ! this . getHeader ( 'Authorization' ) ) {
197
- this . setHeader ( 'Authorization' , 'Basic ' +
198
- Buffer . from ( options . auth ) . toString ( 'base64' ) ) ;
199
- }
230
+ if ( options . auth && ! this . getHeader ( 'Authorization' ) ) {
231
+ this . setHeader ( 'Authorization' , 'Basic ' +
232
+ Buffer . from ( options . auth ) . toString ( 'base64' ) ) ;
233
+ }
200
234
201
- if ( method === 'GET' ||
202
- method === 'HEAD' ||
203
- method === 'DELETE' ||
204
- method === 'OPTIONS' ||
205
- method === 'CONNECT' ) {
206
- this . useChunkedEncodingByDefault = false ;
207
- } else {
208
- this . useChunkedEncodingByDefault = true ;
209
- }
235
+ if ( this . getHeader ( 'expect' ) ) {
236
+ if ( this . _header ) {
237
+ throw new errors . Error ( 'ERR_HTTP_HEADERS_SENT' , 'render' ) ;
238
+ }
210
239
211
- if ( headersArray ) {
212
- this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
213
- options . headers ) ;
214
- } else if ( this . getHeader ( 'expect' ) ) {
215
- if ( this . _header ) {
216
- throw new errors . Error ( 'ERR_HTTP_HEADERS_SENT' , 'render' ) ;
240
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
241
+ this [ outHeadersKey ] ) ;
217
242
}
218
-
243
+ } else {
219
244
this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
220
- this [ outHeadersKey ] ) ;
245
+ options . headers ) ;
221
246
}
222
247
223
- this . _ended = false ;
224
- this . res = null ;
225
- this . aborted = undefined ;
226
- this . timeoutCb = null ;
227
- this . upgradeOrConnect = false ;
228
- this . parser = null ;
229
- this . maxHeadersCount = null ;
230
-
231
- var called = false ;
232
-
233
248
var oncreate = ( err , socket ) => {
234
249
if ( called )
235
250
return ;
@@ -242,18 +257,8 @@ function ClientRequest(options, cb) {
242
257
this . _deferToConnect ( null , null , ( ) => this . _flush ( ) ) ;
243
258
} ;
244
259
260
+ // initiate connection
245
261
if ( this . agent ) {
246
- // If there is an agent we should default to Connection:keep-alive,
247
- // but only if the Agent will actually reuse the connection!
248
- // If it's not a keepAlive agent, and the maxSockets==Infinity, then
249
- // there's never a case where this socket will actually be reused
250
- if ( ! this . agent . keepAlive && ! Number . isFinite ( this . agent . maxSockets ) ) {
251
- this . _last = true ;
252
- this . shouldKeepAlive = false ;
253
- } else {
254
- this . _last = false ;
255
- this . shouldKeepAlive = true ;
256
- }
257
262
this . agent . addRequest ( this , options ) ;
258
263
} else {
259
264
// No agent, default to Connection:close.
0 commit comments