@@ -132,6 +132,40 @@ function ClientRequest(options, cb) {
132
132
this . once ( 'response' , cb ) ;
133
133
}
134
134
135
+ if ( method === 'GET' ||
136
+ method === 'HEAD' ||
137
+ method === 'DELETE' ||
138
+ method === 'OPTIONS' ||
139
+ method === 'CONNECT' ) {
140
+ this . useChunkedEncodingByDefault = false ;
141
+ } else {
142
+ this . useChunkedEncodingByDefault = true ;
143
+ }
144
+
145
+ this . _ended = false ;
146
+ this . res = null ;
147
+ this . aborted = undefined ;
148
+ this . timeoutCb = null ;
149
+ this . upgradeOrConnect = false ;
150
+ this . parser = null ;
151
+ this . maxHeadersCount = null ;
152
+
153
+ var called = false ;
154
+
155
+ if ( this . agent ) {
156
+ // If there is an agent we should default to Connection:keep-alive,
157
+ // but only if the Agent will actually reuse the connection!
158
+ // If it's not a keepAlive agent, and the maxSockets==Infinity, then
159
+ // there's never a case where this socket will actually be reused
160
+ if ( ! this . agent . keepAlive && ! Number . isFinite ( this . agent . maxSockets ) ) {
161
+ this . _last = true ;
162
+ this . shouldKeepAlive = false ;
163
+ } else {
164
+ this . _last = false ;
165
+ this . shouldKeepAlive = true ;
166
+ }
167
+ }
168
+
135
169
var headersArray = Array . isArray ( options . headers ) ;
136
170
if ( ! headersArray ) {
137
171
if ( options . headers ) {
@@ -141,6 +175,7 @@ function ClientRequest(options, cb) {
141
175
this . setHeader ( key , options . headers [ key ] ) ;
142
176
}
143
177
}
178
+
144
179
if ( host && ! this . getHeader ( 'host' ) && setHost ) {
145
180
var hostHeader = host ;
146
181
@@ -159,45 +194,25 @@ function ClientRequest(options, cb) {
159
194
}
160
195
this . setHeader ( 'Host' , hostHeader ) ;
161
196
}
162
- }
163
197
164
- if ( options . auth && ! this . getHeader ( 'Authorization' ) ) {
165
- this . setHeader ( 'Authorization' , 'Basic ' +
166
- Buffer . from ( options . auth ) . toString ( 'base64' ) ) ;
167
- }
198
+ if ( options . auth && ! this . getHeader ( 'Authorization' ) ) {
199
+ this . setHeader ( 'Authorization' , 'Basic ' +
200
+ Buffer . from ( options . auth ) . toString ( 'base64' ) ) ;
201
+ }
168
202
169
- if ( method === 'GET' ||
170
- method === 'HEAD' ||
171
- method === 'DELETE' ||
172
- method === 'OPTIONS' ||
173
- method === 'CONNECT' ) {
174
- this . useChunkedEncodingByDefault = false ;
175
- } else {
176
- this . useChunkedEncodingByDefault = true ;
177
- }
203
+ if ( this . getHeader ( 'expect' ) ) {
204
+ if ( this . _header ) {
205
+ throw new errors . Error ( 'ERR_HTTP_HEADERS_SENT' , 'render' ) ;
206
+ }
178
207
179
- if ( headersArray ) {
180
- this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
181
- options . headers ) ;
182
- } else if ( this . getHeader ( 'expect' ) ) {
183
- if ( this . _header ) {
184
- throw new errors . Error ( 'ERR_HTTP_HEADERS_SENT' , 'render' ) ;
208
+ this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
209
+ this [ outHeadersKey ] ) ;
185
210
}
186
-
211
+ } else {
187
212
this . _storeHeader ( this . method + ' ' + this . path + ' HTTP/1.1\r\n' ,
188
- this [ outHeadersKey ] ) ;
213
+ options . headers ) ;
189
214
}
190
215
191
- this . _ended = false ;
192
- this . res = null ;
193
- this . aborted = undefined ;
194
- this . timeoutCb = null ;
195
- this . upgradeOrConnect = false ;
196
- this . parser = null ;
197
- this . maxHeadersCount = null ;
198
-
199
- var called = false ;
200
-
201
216
var oncreate = ( err , socket ) => {
202
217
if ( called )
203
218
return ;
@@ -210,18 +225,8 @@ function ClientRequest(options, cb) {
210
225
this . _deferToConnect ( null , null , ( ) => this . _flush ( ) ) ;
211
226
} ;
212
227
228
+ // initiate connection
213
229
if ( this . agent ) {
214
- // If there is an agent we should default to Connection:keep-alive,
215
- // but only if the Agent will actually reuse the connection!
216
- // If it's not a keepAlive agent, and the maxSockets==Infinity, then
217
- // there's never a case where this socket will actually be reused
218
- if ( ! this . agent . keepAlive && ! Number . isFinite ( this . agent . maxSockets ) ) {
219
- this . _last = true ;
220
- this . shouldKeepAlive = false ;
221
- } else {
222
- this . _last = false ;
223
- this . shouldKeepAlive = true ;
224
- }
225
230
this . agent . addRequest ( this , options ) ;
226
231
} else {
227
232
// No agent, default to Connection:close.
0 commit comments