Skip to content

Commit cf53fd5

Browse files
committed
New improvements in U.request().
1 parent c788a7f commit cf53fd5

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

builders.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,7 @@ function RESTBuilder(url) {
34233423
// this.$nodnscache = true;
34243424
// this.$cache_expire;
34253425
// this.$cache_nocache;
3426+
// this.$redirect
34263427
}
34273428

34283429
RESTBuilder.make = function(fn) {
@@ -3614,6 +3615,11 @@ RESTBuilder.prototype.xml = function(data) {
36143615
return this;
36153616
};
36163617

3618+
RESTBuilder.prototype.redirect = function(value) {
3619+
this.$redirect = value;
3620+
return this;
3621+
};
3622+
36173623
RESTBuilder.prototype.raw = function(value) {
36183624
this.$data = value && value.$clean ? value.$clean() : value;
36193625
return this;
@@ -3692,6 +3698,7 @@ RESTBuilder.prototype.exec = function(callback) {
36923698

36933699
!self.$nodnscache && flags.push('dnscache');
36943700
self.$length && flags.push('<' + self.$length);
3701+
self.$redirect === false && flags.push('noredirect');
36953702

36963703
switch (self.$type) {
36973704
case 1:
@@ -3701,7 +3708,6 @@ RESTBuilder.prototype.exec = function(callback) {
37013708
flags.push('xml');
37023709
break;
37033710
}
3704-
37053711
self.$flags = flags;
37063712
}
37073713

@@ -3723,7 +3729,7 @@ RESTBuilder.prototype.exec = function(callback) {
37233729

37243730
return U.request(self.$url, flags, self.$data, function(err, response, status, headers, hostname) {
37253731

3726-
var type = err ? '' : headers['content-type'];
3732+
var type = err ? '' : headers['content-type'] || '';
37273733
var output = new RESTBuilderResponse();
37283734

37293735
output.value = type.indexOf('/xml') === -1 ? response.isJSON() ? F.onParseJSON(response) : F.onParseQuery(response) : response.parseXML();

changes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
- updated: middleware `next(false)` cancels calling of next middleware and controller
3232
- updated: `OPERATION(name, value, [callback(err, response, param)], [param])` added a new (helper) argument `param`
3333
- updated: Mail error handling (added Message instance)
34+
- updated: `U.request()` add a new flag `noredirect`
35+
- updated: `RESTBuilder.redirect(true/false)` enables/disables auto-redirect (default: enabled)
3436

3537
- fixed: routing with `upload` flag
3638
- fixed: workers timeout

utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ exports.request = function(url, flags, data, callback, cookies, headers, encodin
485485
method = flags[i].toUpperCase();
486486
break;
487487

488+
case 'noredirect':
489+
options.noredirect = true;
490+
break;
491+
488492
case 'upload':
489493
headers['Content-Type'] = 'multipart/form-data';
490494
break;
@@ -589,6 +593,26 @@ function request_response(res, uri, options) {
589593
// We have redirect
590594
if (res.statusCode === 301 || res.statusCode === 302) {
591595

596+
if (options.noredirect) {
597+
598+
if (options.callback) {
599+
options.callback(null, '', res.statusCode, res.headers, uri.host);
600+
options.callback = null;
601+
}
602+
603+
if (options.evt) {
604+
options.evt.removeAllListeners();
605+
options.evt = null;
606+
}
607+
608+
res.req.removeAllListeners();
609+
res.req = null;
610+
res.removeAllListeners();
611+
res = null;
612+
613+
return;
614+
}
615+
592616
if (options.redirect > 3) {
593617

594618
if (options.callback) {

0 commit comments

Comments
 (0)