Skip to content

Commit 119bcc6

Browse files
author
Vladimir Upirov
committed
Merge pull request #30 from Backendless/urlPathEscapeForNodeJS
fix (nodejs-ajax) error for not escaped urls
2 parents 38c52d6 + c460add commit 119bcc6

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

libs/backendless.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -380,26 +380,25 @@
380380

381381
Backendless._ajax_for_nodejs = function(config) {
382382
config.data = config.data || "";
383+
config.asyncHandler = config.asyncHandler || {};
384+
config.isAsync = (typeof config.isAsync == 'boolean') ? config.isAsync : false;
385+
386+
if (!config.isAsync) {
387+
throw new Error('Use Async type of request using Backendless with NodeJS. Add Backendless.Async(successCallback, errorCallback) as last argument');
388+
}
383389

384390
if (typeof config.data !== "string") {
385391
config.data = JSON.stringify(config.data);
386392
}
387393

388-
config.asyncHandler = config.asyncHandler || {};
389-
config.isAsync = (typeof config.isAsync == 'boolean') ? config.isAsync : false;
390-
391-
var protocol = config.url.substr(0, config.url.indexOf('/', 8)).substr(0, config.url.indexOf(":"));
392-
var https = protocol === 'https';
393-
394-
var uri = config.url.substr(0, config.url.indexOf('/', 8)).substr(config.url.indexOf("/") + 2),
395-
host = uri.substr(0, (uri.indexOf(":") == -1 ? uri.length : uri.indexOf(":"))),
396-
port = uri.indexOf(":") != -1 ? parseInt(uri.substr(uri.indexOf(":") + 1)) : (https ? 443 : 80);
394+
var u = require('url').parse(config.url);
395+
var https = u.protocol === 'https:';
397396

398397
var options = {
399-
host : host,
400-
port : port,
398+
host : u.host,
399+
port : u.port || https ? 443 : 80,
401400
method : config.method || "GET",
402-
path : config.url.substr(config.url.indexOf('/', 8)),
401+
path : u.path,
403402
headers: {
404403
"Content-Length" : config.data ? Buffer.byteLength(config.data) : 0,
405404
"Content-Type" : config.data ? 'application/json' : 'application/x-www-form-urlencoded',
@@ -413,10 +412,6 @@
413412
options.headers["user-token"] = currentUser["user-token"];
414413
}
415414

416-
if (!config.isAsync) {
417-
throw new Error('Use Async type of request using Backendless with NodeJS. Add Backendless.Async(successCallback, errorCallback) as last argument');
418-
}
419-
420415
var buffer;
421416
var httpx = require(https ? 'https' : 'http');
422417
var req = httpx.request(options, function(res) {
@@ -440,8 +435,7 @@
440435
});
441436

442437
req.on('error', function(e) {
443-
config.asyncHandler.fault || (config.asyncHandler.fault = function() {});
444-
config.asyncHandler.fault(e);
438+
config.asyncHandler.fault && config.asyncHandler.fault(e);
445439
});
446440

447441
req.write(config.data);

0 commit comments

Comments
 (0)