Skip to content

Commit

Permalink
Serialize data into the URL once. Avoid modifying config.data and con…
Browse files Browse the repository at this point in the history
…fig.method within send()
  • Loading branch information
chrisklaiber committed Nov 8, 2011
1 parent 2ddeeb2 commit 1473f43
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions lib/yui3-io.js
Expand Up @@ -11,11 +11,6 @@ YUI.add('io-nodejs', function(Y) {
https = YUI.require('https');
}

var _concat = function(uri, data) {
uri += (uri.indexOf('?') === -1 ? '?' : '&') + data;
return uri;
};

var NodeTransport = {
id: 'nodejs',
src: {
Expand All @@ -26,12 +21,10 @@ YUI.add('io-nodejs', function(Y) {
Y.io.xdrResponse('start', transactionObject, config);

var urlInfo = url.parse(uri, parseQueryString=false),
p = YUI.urlInfoPort(urlInfo);
p = YUI.urlInfoPort(urlInfo),
method = config.method ? config.method.toUpperCase() : 'GET',
data = config.data;

if (!config.method) {
config.method = 'GET';
}
config.method = config.method.toUpperCase();
if (!config.headers) {
config.headers = {};
}
Expand All @@ -44,33 +37,34 @@ YUI.add('io-nodejs', function(Y) {
if (urlInfo.search) {
req_url += urlInfo.search;
}
if (config.data) {
if (Y.Lang.isObject(config.data) && Y.QueryString) {
if (data) {
if (Y.Lang.isObject(data) && Y.QueryString) {
Y.log('Converting config.data Object to String', 'info', 'io');
config.data = Y.QueryString.stringify(config.data);
data = Y.QueryString.stringify(data);
}
switch (config.method) {
switch (method) {
case 'GET':
case 'HEAD':
case 'DELETE':
req_url = _concat(req_url, config.data);
config.data = '';
Y.log('HTTP' + config.method + ' with data. The querystring is: ' + uri, 'info', 'io');
// The data object was serialized into the query
// string by send() in io-base.
data = '';
Y.log('HTTP' + method + ' with data. The querystring is: ' + uri, 'info', 'io');
break;
case 'POST':
case 'PUT':
// If Content-Type is defined in the configuration object, or
// or as a default header, it will be used instead of
// 'application/x-www-form-urlencoded; charset=UTF-8'
config.headers['Content-Length'] = config.data.length;
config.headers['Content-Length'] = data.length;
config.headers = Y.merge({
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}, config.headers);
break;
}
}

Y.log('Requesting (' + config.method + '): ' + urlInfo.hostname, 'info', 'nodeio');
Y.log('Requesting (' + method + '): ' + urlInfo.hostname, 'info', 'nodeio');
if (http.get) { //0.4.0
Y.log('Using new 0.4.0 http.request', 'info', 'nodeio');
var h = ((urlInfo.protocol === 'https') || p === 443) ? https : http;
Expand All @@ -81,14 +75,14 @@ YUI.add('io-nodejs', function(Y) {
var request = h.request({
host: urlInfo.hostname,
port: p,
method: config.method,
method: method,
path: req_url,
headers: config.headers
});
var s = request;
} else {
var host = http.createClient(p, urlInfo.hostname, ((p === 443) ? true : false));
var request = host.request(config.method, req_url, config.headers);
var request = host.request(method, req_url, config.headers);
var s = request.socket || request;
}
Y.log('URL: ' + req_url, 'info', 'nodeio');
Expand Down Expand Up @@ -163,8 +157,8 @@ YUI.add('io-nodejs', function(Y) {
});

});
if (config.method !== 'GET') {
request.write(config.data);
if (data) {
request.write(data);
}
if (request.end) {
request.end();
Expand Down

0 comments on commit 1473f43

Please sign in to comment.