handle php cookies

This commit is contained in:
Nick Carneiro
2016-01-31 13:28:16 -08:00
parent 392bbfd052
commit f4b2c7699b
7 changed files with 92 additions and 32 deletions

View File

@@ -1,19 +1,6 @@
var util = require('../util');
var serializeCookies = function(cookieDict) {
var cookieString = '';
var i = 0;
var cookieCount = Object.keys(cookieDict).length;
for (var cookieName in cookieDict) {
var cookieValue = cookieDict[cookieName];
cookieString += cookieName + '=' + cookieValue;
if (i < cookieCount - 1) {
cookieString += '; ';
}
i++;
}
return cookieString;
};
var toNode = function(curlCommand) {
var request = util.parseCurlCommand(curlCommand);
var nodeCode = 'var request = require(\'request\');\n\n';
@@ -31,7 +18,7 @@ var toNode = function(curlCommand) {
i++;
}
if (request.cookies) {
var cookieString = serializeCookies(request.cookies);
var cookieString = util.serializeCookies(request.cookies);
nodeCode += ' \'Cookie\': \'' + cookieString + '\'\n';
}
nodeCode += '};\n\n';

View File

@@ -17,6 +17,10 @@ var toPython = function(curlCommand) {
}
i++;
}
if (request.cookies) {
var cookieString = util.serializeCookies(request.cookies);
headerDict += ",\n 'Cookie' => '" + cookieString + "'"
}
headerDict += '\n);';
}
@@ -25,7 +29,7 @@ var toPython = function(curlCommand) {
var parsedQueryString = querystring.parse(request.data);
dataString = '$data = array(\n';
var dataCount = Object.keys(parsedQueryString).length;
var i = 0;
i = 0;
for (var key in parsedQueryString) {
var value = parsedQueryString[key];
dataString += " '" + key + "' => '" + value + "'";
@@ -34,7 +38,9 @@ var toPython = function(curlCommand) {
}
i++;
}
dataString += "\n);"
dataString += "\n);";
} else {
dataString = '$data = array();';
}
var requestLine = '$response = Requests::' + request.method + '(\'' + request.url + '\'';
requestLine += ', $headers';
@@ -55,4 +61,5 @@ var toPython = function(curlCommand) {
return phpCode;
};
module.exports = toPython;

View File

@@ -2,18 +2,13 @@
include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
$headers = array(
'Origin' => 'http://fiddle.jshell.net',
'Accept-Encoding' => 'gzip, deflate',
'Accept-Encoding' => 'gzip, deflate, sdch',
'Accept-Language' => 'en-US,en;q=0.8',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept' => '*/*',
'Referer' => 'http://fiddle.jshell.net/_display/',
'X-Requested-With' => 'XMLHttpRequest',
'Connection' => 'keep-alive'
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Referer' => 'http://www.wikipedia.org/',
'Connection' => 'keep-alive',
'Cookie' => 'GeoIP=US:Albuquerque:35.1241:-106.7675:v4; uls-previous-languages=%5B%22en%22%5D; mediaWiki.user.sessionId=VaHaeVW3m0ymvx9kacwshZIDkv8zgF9y; centralnotice_buckets_by_campaign=%7B%22C14_enUS_dsk_lw_FR%22%3A%7B%22val%22%3A%220%22%2C%22start%22%3A1412172000%2C%22end%22%3A1422576000%7D%2C%22C14_en5C_dec_dsk_FR%22%3A%7B%22val%22%3A3%2C%22start%22%3A1417514400%2C%22end%22%3A1425290400%7D%2C%22C14_en5C_bkup_dsk_FR%22%3A%7B%22val%22%3A1%2C%22start%22%3A1417428000%2C%22end%22%3A1425290400%7D%7D; centralnotice_bannercount_fr12=22; centralnotice_bannercount_fr12-wait=14'
);
$data = array(
'msg1' => 'wow',
'msg2' => 'such'
);
$response = Requests::post('http://fiddle.jshell.net/echo/html/', $headers, $data);
$data = array();
$response = Requests::get('http://en.wikipedia.org/', $headers, $data);

16
test/php_output2.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
$headers = array(
'Origin' => 'http://www.w3schools.com',
'Accept-Encoding' => 'gzip, deflate',
'Accept-Language' => 'en-US,en;q=0.8',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Accept' => '*/*',
'Referer' => 'http://www.w3schools.com/ajax/tryit_view.asp?x=0.07944501144811511',
'Connection' => 'keep-alive',
'Content-Length' => '0',
'Cookie' => '_gat=1; ASPSESSIONIDACCRDTDC=MCMDKFMBLLLHGKCGNMKNGPKI; _ga=GA1.2.1424920226.1419478126'
);
$data = array();
$response = Requests::post('http://www.w3schools.com/ajax/demo_post.asp', $headers, $data);

19
test/php_output3.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
$headers = array(
'Origin' => 'http://fiddle.jshell.net',
'Accept-Encoding' => 'gzip, deflate',
'Accept-Language' => 'en-US,en;q=0.8',
'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept' => '*/*',
'Referer' => 'http://fiddle.jshell.net/_display/',
'X-Requested-With' => 'XMLHttpRequest',
'Connection' => 'keep-alive'
);
$data = array(
'msg1' => 'wow',
'msg2' => 'such'
);
$response = Requests::post('http://fiddle.jshell.net/echo/html/', $headers, $data);

View File

@@ -138,10 +138,28 @@ test('post with data-binary - python', function (t) {
});
test('http get - php', function (t) {
var curlHttpGetCommand = fs.readFileSync(__dirname + '/curl1.txt', 'utf-8');
var phpCode = curlconverter.toPhp(curlHttpGetCommand);
var goodPhpCode = fs.readFileSync(__dirname + '/php_output1.php', 'utf-8');
t.equal(phpCode, goodPhpCode);
t.end();
});
test('http post - php', function (t) {
var curlHttpGetCommand = fs.readFileSync(__dirname + '/curl2.txt', 'utf-8');
var phpCode = curlconverter.toPhp(curlHttpGetCommand);
var goodPhpCode = fs.readFileSync(__dirname + '/php_output2.php', 'utf-8');
t.equal(phpCode, goodPhpCode);
t.end();
});
test('http post with data - php', function (t) {
var curlHttpPostCommand = fs.readFileSync(__dirname + '/curl3.txt', 'utf-8');
var phpCode = curlconverter.toPhp(curlHttpPostCommand);
var goodPhpCode = fs.readFileSync(__dirname + '/php_output1.php', 'utf-8').trim();
var goodPhpCode = fs.readFileSync(__dirname + '/php_output3.php', 'utf-8').trim();
t.equal(phpCode, goodPhpCode);
t.end();
});

22
util.js
View File

@@ -65,6 +65,24 @@ var parseCurlCommand = function(curlCommand) {
return request;
};
module.exports = {
parseCurlCommand: parseCurlCommand
var serializeCookies = function(cookieDict) {
var cookieString = '';
var i = 0;
var cookieCount = Object.keys(cookieDict).length;
for (var cookieName in cookieDict) {
var cookieValue = cookieDict[cookieName];
cookieString += cookieName + '=' + cookieValue;
if (i < cookieCount - 1) {
cookieString += '; ';
}
i++;
}
return cookieString;
};
module.exports = {
parseCurlCommand: parseCurlCommand,
serializeCookies: serializeCookies
};