@@ -8,55 +8,64 @@ module.exports = function (req, opts) {
88 code . push ( '$curl = curl_init();' ) ;
99
1010 var options = [ {
11+ escape : true ,
12+ name : 'CURLOPT_port' ,
13+ value : req . uriObj . port
14+ } , {
1115 escape : true ,
1216 name : 'CURLOPT_URL' ,
1317 value : req . url
14- } ,
15- {
18+ } , {
1619 escape : false ,
1720 name : 'CURLOPT_RETURNTRANSFER' ,
1821 value : 'true'
19- } ,
20- {
22+ } , {
2123 escape : false ,
2224 name : 'CURLOPT_HTTP_VERSION' ,
2325 value : req . httpVersion === 'HTTP/1.0' ? 'CURL_HTTP_VERSION_1_0' : 'CURL_HTTP_VERSION_1_1'
24- } , {
26+ } , {
2527 escape : true ,
2628 name : 'CURLOPT_CUSTOMREQUEST' ,
2729 value : req . method
28- } , {
30+ } , {
2931 escape : true ,
3032 name : 'CURLOPT_POSTFIELDS' ,
3133 value : req . postData ? req . postData . text : undefined
3234 } ] ;
3335
36+ code . push ( 'curl_setopt_array($curl, array(' ) ;
37+
38+ var curlopts = [ ] ;
39+
3440 options . map ( function ( option ) {
3541 if ( option . value ) {
36- code . push ( util . format ( 'curl_setopt($curl, %s, %s); ' , option . name , option . escape ? JSON . stringify ( option . value ) : option . value ) ) ;
42+ curlopts . push ( util . format ( '%s => %s, ' , option . name , option . escape ? JSON . stringify ( option . value ) : option . value ) ) ;
3743 }
3844 } ) ;
3945
40-
41- /**
46+ // construct cookies
4247 if ( req . cookies && req . cookies . length ) {
43- var cookies = req.cookies.map(function(cookie) {
48+ var cookies = req . cookies . map ( function ( cookie ) {
4449 return encodeURIComponent ( cookie . name ) + '=' + encodeURIComponent ( cookie . value ) ;
4550 } ) ;
4651
47- code .push(util.format('-b "%s"', cookies.join('& ')));
52+ curlopts . push ( util . format ( 'CURLOPT_COOKIE => "%s", ' , cookies . join ( '; ' ) ) ) ;
4853 }
4954
55+ // construct cookies
5056 if ( req . headers && req . headers . length ) {
51- req.headers.map(function(header) {
52- code.push( util.format('-H "%s: %s"', header.name, header.value) );
57+ var headers = req . headers . map ( function ( header ) {
58+ return util . format ( '"%s: %s"' , header . name , header . value ) ;
5359 } ) ;
54- }
5560
56- if (req.postData) {
57- code.push(util.format('-d "%s"', req.postData.text));
61+ curlopts . push ( util . format ( 'CURLOPT_HTTPHEADER => array(\n\t\t%s\n\t),' , headers . join ( ',\n\t\t' ) ) ) ;
5862 }
59- */
63+
64+ code . push ( '\t' + curlopts . join ( '\n\t' ) ) ;
65+
66+ code . push ( '));' ) ;
67+ code . push ( '$response = curl_exec($curl);' ) ;
68+ code . push ( 'curl_close($curl);' ) ;
6069
6170 return code . join ( '\n' ) ;
62- }
71+ } ;
0 commit comments