@@ -16,7 +16,8 @@ var fs = require('fs'),
16
16
auth = require ( './auth' ) ,
17
17
cookies = require ( './cookies' ) ,
18
18
parsers = require ( './parsers' ) ,
19
- decoder = require ( './decoder' ) ;
19
+ decoder = require ( './decoder' ) ,
20
+ utils = require ( './utils' ) ;
20
21
21
22
//////////////////////////////////////////
22
23
// variabilia
@@ -127,99 +128,13 @@ Object.keys(aliased.options).map(function(k) {
127
128
//////////////////////////////////////////
128
129
// helpers
129
130
130
- function get_env_var ( keys , try_lower ) {
131
- var val , i = - 1 , env = process . env ;
132
- while ( ! val && i < keys . length - 1 ) {
133
- val = env [ keys [ ++ i ] ] ;
134
- if ( ! val && try_lower ) {
135
- val = env [ keys [ i ] . toLowerCase ( ) ] ;
136
- }
137
- }
138
- return val ;
139
- }
140
-
141
131
function keys_by_type ( type ) {
142
132
return Object . keys ( defaults ) . map ( function ( el ) {
143
133
if ( defaults [ el ] !== null && defaults [ el ] . constructor == type )
144
134
return el ;
145
135
} ) . filter ( function ( el ) { return el } )
146
136
}
147
137
148
- function parse_content_type ( header ) {
149
- if ( ! header || header === '' ) return { } ;
150
-
151
- var found , charset = 'utf8' , arr = header . split ( ';' ) ;
152
-
153
- if ( arr . length > 1 && ( found = arr [ 1 ] . match ( / c h a r s e t = ( .+ ) / ) ) )
154
- charset = found [ 1 ] ;
155
-
156
- return { type : arr [ 0 ] , charset : charset } ;
157
- }
158
-
159
- function is_stream ( obj ) {
160
- return typeof obj . pipe === 'function' ;
161
- }
162
-
163
- function get_stream_length ( stream , given_length , cb ) {
164
- if ( given_length > 0 )
165
- return cb ( given_length ) ;
166
-
167
- if ( stream . end !== void 0 && stream . end !== Infinity && stream . start !== void 0 )
168
- return cb ( ( stream . end + 1 ) - ( stream . start || 0 ) ) ;
169
-
170
- fs . stat ( stream . path , function ( err , stat ) {
171
- cb ( stat ? stat . size - ( stream . start || 0 ) : null ) ;
172
- } ) ;
173
- }
174
-
175
- function resolve_url ( href , base ) {
176
- if ( url . URL )
177
- return new url . URL ( href , base ) ;
178
-
179
- // older Node version (< v6.13)
180
- return base ? url . resolve ( base , href ) : href ;
181
- }
182
-
183
- function host_and_ports_match ( url1 , url2 ) {
184
- if ( url1 . indexOf ( 'http' ) < 0 ) url1 = 'http://' + url1 ;
185
- if ( url2 . indexOf ( 'http' ) < 0 ) url2 = 'http://' + url2 ;
186
- var a = url . parse ( url1 ) , b = url . parse ( url2 ) ;
187
-
188
- return a . host == b . host
189
- && String ( a . port || ( a . protocol == 'https:' ? 443 : 80 ) )
190
- == String ( b . port || ( b . protocol == 'https:' ? 443 : 80 ) ) ;
191
- }
192
-
193
- // returns false if a no_proxy host matches given url
194
- function should_proxy_to ( url ) {
195
- var no_proxy = get_env_var ( [ 'NO_PROXY' ] , true ) ;
196
- if ( ! no_proxy ) return true ;
197
-
198
- var host , hosts = no_proxy . split ( ',' ) ;
199
- for ( var i in hosts ) {
200
- host = hosts [ i ] ;
201
- if ( host_and_ports_match ( host , url ) ) {
202
- return false ;
203
- }
204
- }
205
-
206
- return true ;
207
- }
208
-
209
- function pump_streams ( streams , cb ) {
210
- if ( stream . pipeline )
211
- return stream . pipeline . apply ( null , streams . concat ( cb ) ) ;
212
-
213
- var tmp = streams . shift ( ) ;
214
- while ( streams . length ) {
215
- tmp = tmp . pipe ( streams . shift ( ) ) ;
216
- tmp . once ( 'error' , function ( e ) {
217
- cb && cb ( e ) ;
218
- cb = null ;
219
- } )
220
- }
221
- }
222
-
223
138
//////////////////////////////////////////
224
139
// the main act
225
140
@@ -340,12 +255,12 @@ Needle.prototype.setup = function(uri, options) {
340
255
}
341
256
}
342
257
343
- var env_proxy = get_env_var ( [ 'HTTP_PROXY' , 'HTTPS_PROXY' ] , true ) ;
258
+ var env_proxy = utils . get_env_var ( [ 'HTTP_PROXY' , 'HTTPS_PROXY' ] , true ) ;
344
259
if ( ! config . proxy && env_proxy ) config . proxy = env_proxy ;
345
260
346
261
// if proxy is present, set auth header from either url or proxy_user option.
347
262
if ( config . proxy ) {
348
- if ( should_proxy_to ( uri ) ) {
263
+ if ( utils . should_proxy_to ( uri ) ) {
349
264
if ( config . proxy . indexOf ( 'http' ) === - 1 )
350
265
config . proxy = 'http://' + config . proxy ;
351
266
@@ -402,7 +317,7 @@ Needle.prototype.start = function() {
402
317
next ( parts ) ;
403
318
} ) ;
404
319
405
- } else if ( is_stream ( data ) ) {
320
+ } else if ( utils . is_stream ( data ) ) {
406
321
407
322
if ( method == 'get' )
408
323
throw new Error ( 'Refusing to pipe() a stream via GET. Did you mean .post?' ) ;
@@ -411,7 +326,7 @@ Needle.prototype.start = function() {
411
326
// ok, let's get the stream's length and set it as the content-length header.
412
327
// this prevents some servers from cutting us off before all the data is sent.
413
328
waiting = true ;
414
- get_stream_length ( data , config . stream_length , function ( length ) {
329
+ utils . get_stream_length ( data , config . stream_length , function ( length ) {
415
330
data . length = length ;
416
331
next ( data ) ;
417
332
} )
@@ -609,7 +524,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
609
524
610
525
// if follow_set_cookies is true, insert cookies in the next request's headers.
611
526
// we set both the original request cookies plus any response cookies we might have received.
612
- if ( config . follow_set_cookies && host_and_ports_match ( headers . location , uri ) ) {
527
+ if ( config . follow_set_cookies && utils . host_and_ports_match ( headers . location , uri ) ) {
613
528
var request_cookies = cookies . read ( config . headers [ 'cookie' ] ) ;
614
529
config . previous_resp_cookies = resp . cookies ;
615
530
if ( Object . keys ( request_cookies ) . length || Object . keys ( resp . cookies || { } ) . length ) {
@@ -625,7 +540,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
625
540
626
541
config . headers [ 'host' ] = null ; // clear previous Host header to avoid conflicts.
627
542
628
- var redirect_url = resolve_url ( headers . location , uri ) ;
543
+ var redirect_url = utils . resolve_url ( headers . location , uri ) ;
629
544
debug ( 'Redirecting to ' + redirect_url . toString ( ) ) ;
630
545
return self . send_request ( ++ count , method , redirect_url . toString ( ) , config , post_data , out , callback ) ;
631
546
} else if ( config . follow_max > 0 ) {
@@ -650,7 +565,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
650
565
out . emit ( 'headers' , headers ) ;
651
566
652
567
var pipeline = [ ] ,
653
- mime = parse_content_type ( headers [ 'content-type' ] ) ,
568
+ mime = utils . parse_content_type ( headers [ 'content-type' ] ) ,
654
569
text_response = mime . type && ( mime . type . indexOf ( 'text/' ) != - 1 || ! ! mime . type . match ( / ( \/ | \+ ) ( x m l | j s o n ) $ / ) ) ;
655
570
656
571
// To start, if our body is compressed and we're able to inflate it, do it.
@@ -689,7 +604,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
689
604
pipeline . push ( out ) ;
690
605
691
606
// Now, release the kraken!
692
- pump_streams ( [ resp ] . concat ( pipeline ) , function ( err ) {
607
+ utils . pump_streams ( [ resp ] . concat ( pipeline ) , function ( err ) {
693
608
if ( err ) debug ( err )
694
609
695
610
// on node v8.x, if an error ocurrs on the receiving end,
@@ -745,7 +660,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
745
660
}
746
661
} )
747
662
748
- pump_streams ( [ resp , clean_pipe ] , function ( err ) {
663
+ utils . pump_streams ( [ resp , clean_pipe ] , function ( err ) {
749
664
if ( err ) debug ( err ) ;
750
665
} ) ;
751
666
@@ -836,8 +751,8 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
836
751
} )
837
752
838
753
if ( post_data ) {
839
- if ( is_stream ( post_data ) ) {
840
- pump_streams ( [ post_data , request ] , function ( err ) {
754
+ if ( utils . is_stream ( post_data ) ) {
755
+ utils . pump_streams ( [ post_data , request ] , function ( err ) {
841
756
if ( err ) debug ( err ) ;
842
757
} ) ;
843
758
} else {
0 commit comments