@@ -72,6 +72,7 @@ const ALPHA_INDEX = { '<': '<', '>': '>', '"': '"', '&apos': '\'', '&
7272const EMPTYARRAY = [ ] ;
7373const EMPTYOBJECT = [ ] ;
7474const NODEVERSION = parseFloat ( process . version . toString ( ) . replace ( 'v' , '' ) . replace ( / \. / g, '' ) ) ;
75+ const STREAMPIPE = { end : false } ;
7576
7677Object . freeze ( EMPTYARRAY ) ;
7778Object . freeze ( EMPTYOBJECT ) ;
@@ -411,7 +412,7 @@ exports.keywords = function(content, forSearch, alternative, max_count, max_leng
411412 * @param {Number } timeout Request timeout.
412413 * return {Boolean}
413414 */
414- exports . request = function ( url , flags , data , callback , cookies , headers , encoding , timeout ) {
415+ exports . request = function ( url , flags , data , callback , cookies , headers , encoding , timeout , files ) {
415416
416417 // No data (data is optional argument)
417418 if ( typeof ( data ) === 'function' ) {
@@ -492,7 +493,11 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
492493 break ;
493494
494495 case 'upload' :
495- headers [ 'Content-Type' ] = 'multipart/form-data' ;
496+ type = 4 ;
497+ options . upload = true ;
498+ options . files = files || EMPTYARRAY ;
499+ options . boundary = '----totaljs' + Math . random ( ) . toString ( 16 ) . substring ( 2 ) ;
500+ headers [ 'Content-Type' ] = 'multipart/form-data; boundary=' + options . boundary ;
496501 break ;
497502
498503 case 'post' :
@@ -514,7 +519,7 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
514519 else
515520 method = 'GET' ;
516521
517- if ( type !== 3 ) {
522+ if ( type < 3 ) {
518523 if ( typeof ( data ) !== 'string' )
519524 data = type === 1 ? JSON . stringify ( data ) : Qs . stringify ( data ) ;
520525 else if ( data [ 0 ] === '?' )
@@ -526,10 +531,11 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
526531 }
527532 }
528533
529- if ( data ) {
534+ if ( data && type !== 4 ) {
530535 options . data = data instanceof Buffer ? data : exports . createBuffer ( data , ENCODING ) ;
531536 headers [ 'Content-Length' ] = options . data . length ;
532- }
537+ } else
538+ options . data = data ;
533539
534540 if ( cookies ) {
535541 var builder = '' ;
@@ -584,9 +590,49 @@ function request_call(uri, options) {
584590 } ) ;
585591
586592 req . on ( 'response' , ( response ) => response . req = req ) ;
587- req . end ( options . data ) ;
593+
594+ if ( options . upload ) {
595+ options . first = true ;
596+ options . files . wait ( function ( file , next ) {
597+ // next();
598+ request_writefile ( req , options , file , next ) ;
599+ } , function ( ) {
600+
601+ var keys = Object . keys ( options . data ) ;
602+ for ( var i = 0 , length = keys . length ; i < length ; i ++ ) {
603+ var value = options . data [ keys [ i ] ] ;
604+ if ( value != null ) {
605+ req . write ( ( options . first ? '' : NEWLINE ) + '--' + options . boundary + NEWLINE + 'Content-Disposition: form-data; name="' + keys [ i ] + '"' + NEWLINE + NEWLINE + encodeURIComponent ( value . toString ( ) ) ) ;
606+ if ( options . first )
607+ options . first = false ;
608+ }
609+ }
610+
611+ req . end ( NEWLINE + '--' + options . boundary + '--' ) ;
612+ } ) ;
613+ } else
614+ req . end ( options . data ) ;
588615}
589616
617+ function request_writefile ( req , options , file , next ) {
618+
619+ req . write ( ( options . first ? '' : NEWLINE ) + '--' + options . boundary + NEWLINE + 'Content-Disposition: form-data; name="' + file . name + '"; filename="' + exports . getName ( file . filename ) + '"' + NEWLINE + 'Content-Type: ' + exports . getContentType ( exports . getExtension ( file . filename ) ) + NEWLINE + NEWLINE ) ;
620+
621+ if ( options . first )
622+ options . first = false ;
623+
624+ // Is Buffer
625+ if ( file . buffer ) {
626+ req . write ( file . buffer ) ;
627+ next ( ) ;
628+ } else {
629+ var stream = Fs . createReadStream ( file . filename ) ;
630+ stream . once ( 'close' , next ) ;
631+ stream . pipe ( req , STREAMPIPE ) ;
632+ }
633+ }
634+
635+
590636function request_response ( res , uri , options ) {
591637
592638 res . _buffer = null ;
0 commit comments