1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add redirectList property in msg of http-request node

This commit is contained in:
nakanishi 2018-12-04 15:39:01 +00:00
parent 2060af8a92
commit 4eb3bd496b
4 changed files with 81 additions and 10 deletions

View File

@ -131,8 +131,16 @@ module.exports = function(RED) {
if (msg.hasOwnProperty('followRedirects')) { if (msg.hasOwnProperty('followRedirects')) {
opts.followRedirect = msg.followRedirects; opts.followRedirect = msg.followRedirects;
} }
var redirectList = [];
if (!opts.hasOwnProperty('followRedirect') || opts.followRedirect) { if (!opts.hasOwnProperty('followRedirect') || opts.followRedirect) {
opts.followRedirect = function(res) { opts.followRedirect = function(res) {
var redirectInfo = {
location: res.headers.location,
};
if (res.headers.hasOwnProperty('set-cookie')) {
redirectInfo.cookies = extractCookies(res.headers['set-cookie']);
}
redirectList.push(redirectInfo);
if (this.headers.cookie) { if (this.headers.cookie) {
delete this.headers.cookie; delete this.headers.cookie;
} }
@ -256,17 +264,9 @@ module.exports = function(RED) {
msg.headers = res.headers; msg.headers = res.headers;
msg.responseUrl = res.request.uri.href; msg.responseUrl = res.request.uri.href;
msg.payload = body; msg.payload = body;
msg.redirectList = redirectList;
if (msg.headers.hasOwnProperty('set-cookie')) { if (msg.headers.hasOwnProperty('set-cookie')) {
msg.responseCookies = {}; msg.responseCookies = extractCookies(msg.headers['set-cookie']);
msg.headers['set-cookie'].forEach(function(c) {
var parsedCookie = cookie.parse(c);
var eq_idx = c.indexOf('=');
var key = c.substr(0, eq_idx).trim()
parsedCookie.value = parsedCookie[key];
delete parsedCookie[key];
msg.responseCookies[key] = parsedCookie;
});
} }
msg.headers['x-node-red-request-node'] = hashSum(msg.headers); msg.headers['x-node-red-request-node'] = hashSum(msg.headers);
// msg.url = url; // revert when warning above finally removed // msg.url = url; // revert when warning above finally removed
@ -299,6 +299,19 @@ module.exports = function(RED) {
this.on("close",function() { this.on("close",function() {
node.status({}); node.status({});
}); });
function extractCookies(setCookie) {
var cookies = {};
setCookie.forEach(function(c) {
var parsedCookie = cookie.parse(c);
var eq_idx = c.indexOf('=');
var key = c.substr(0, eq_idx).trim()
parsedCookie.value = parsedCookie[key];
delete parsedCookie[key];
cookies[key] = parsedCookie;
});
return cookies;
}
} }
RED.nodes.registerType("http request",HTTPRequest,{ RED.nodes.registerType("http request",HTTPRequest,{

View File

@ -53,6 +53,8 @@
Otherwise, the url of the original request.</dd> Otherwise, the url of the original request.</dd>
<dt>responseCookies <span class="property-type">object</span></dt> <dt>responseCookies <span class="property-type">object</span></dt>
<dd>If the response includes cookies, this propery is an object of name/value pairs for each cookie.</dd> <dd>If the response includes cookies, this propery is an object of name/value pairs for each cookie.</dd>
<dt>redirectList <span class="property-type">array</span></dt>
<dd>If the request was redirected one or more times, the accumulated information will be added to this property. `location` is the next redirect destination. `cookies` is the cookies returned from the redirect source.</dd>
</dl> </dl>
<h3>Details</h3> <h3>Details</h3>
<p>When configured within the node, the URL property can contain <a href="http://mustache.github.io/mustache.5.html" target="_blank">mustache-style</a> tags. These allow the <p>When configured within the node, the URL property can contain <a href="http://mustache.github.io/mustache.5.html" target="_blank">mustache-style</a> tags. These allow the

View File

@ -46,6 +46,8 @@
<dd>リクエストの処理時にリダイレクトが発生した場合、このプロパティが最後にリダイレクトされたURLを表します。リダイレクトが起こらなかった場合、最初リクエストのURLを表します。</dd> <dd>リクエストの処理時にリダイレクトが発生した場合、このプロパティが最後にリダイレクトされたURLを表します。リダイレクトが起こらなかった場合、最初リクエストのURLを表します。</dd>
<dt>responseCookies <span class="property-type">オブジェクト</span></dt> <dt>responseCookies <span class="property-type">オブジェクト</span></dt>
<dd>レスポンスがクッキーを含む場合、このプロパティは各クッキーの名前/値を含むオブジェクトを表します。</dd> <dd>レスポンスがクッキーを含む場合、このプロパティは各クッキーの名前/値を含むオブジェクトを表します。</dd>
<dt>redirectList <span class="property-type">配列</span></dt>
<dd>リクエストが一回以上リダイレクトされた場合は、このプロパティに情報が蓄積されます。`location`は、リダイレクト先を示します。`cookies`は、リダイレクト元から返されたクッキー情報です。</dd>
</dl> </dl>
<h3>詳細</h3> <h3>詳細</h3>
<p>ードの設定でurlプロパティを指定する場合、<a href="http://mustache.github.io/mustache.5.html" target="_blank">mustache形式</a>のタグを含めることができます。これにより、URLを入力メッセージの値から構成することができます。例えば、urlが<code>example.com/{{{topic}}}</code>の場合、<code>msg.topic</code>の値による置き換えを自動的に行います。{{{...}}}表記を使うと、/、&といった文字をmustacheがエスケープするのを抑止できます。</p> <p>ードの設定でurlプロパティを指定する場合、<a href="http://mustache.github.io/mustache.5.html" target="_blank">mustache形式</a>のタグを含めることができます。これにより、URLを入力メッセージの値から構成することができます。例えば、urlが<code>example.com/{{{topic}}}</code>の場合、<code>msg.topic</code>の値による置き換えを自動的に行います。{{{...}}}表記を使うと、/、&といった文字をmustacheがエスケープするのを抑止できます。</p>

View File

@ -207,6 +207,12 @@ describe('HTTP Request Node', function() {
res.cookie('redirectToDifferentDomain','different1'); res.cookie('redirectToDifferentDomain','different1');
res.redirect(getDifferentTestURL('/redirectReturn')); res.redirect(getDifferentTestURL('/redirectReturn'));
}); });
testApp.get('/redirectMultipleTimes', function(req, res) {
var key = req.headers.host + req.url;
receivedCookies[key] = req.cookies;
res.cookie('redirectMultipleTimes','multiple1');
res.redirect(getTestURL('/redirectToDifferentDomain'));
});
testApp.get('/redirectReturn', function(req, res) { testApp.get('/redirectReturn', function(req, res) {
var key = req.headers.host + req.url; var key = req.headers.host + req.url;
receivedCookies[key] = req.cookies; receivedCookies[key] = req.cookies;
@ -277,6 +283,7 @@ describe('HTTP Request Node', function() {
msg.should.have.property('headers'); msg.should.have.property('headers');
msg.headers.should.have.property('content-length',''+('hello'.length)); msg.headers.should.have.property('content-length',''+('hello'.length));
msg.headers.should.have.property('content-type').which.startWith('text/html'); msg.headers.should.have.property('content-type').which.startWith('text/html');
msg.redirectList.length.should.equal(0);
done(); done();
} catch(err) { } catch(err) {
done(err); done(err);
@ -1510,6 +1517,10 @@ describe('HTTP Request Node', function() {
done(new Error('Invalid cookie(path:/rediectReurn)')); done(new Error('Invalid cookie(path:/rediectReurn)'));
return; return;
} }
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://localhost:'+testPort+'/redirectReturn');
redirect1.cookies.redirectToSameDomainCookie.Path.should.equals('/');
redirect1.cookies.redirectToSameDomainCookie.value.should.equals('same1');
done(); done();
}); });
n1.receive({}); n1.receive({});
@ -1533,6 +1544,10 @@ describe('HTTP Request Node', function() {
done(new Error('Invalid cookie(path:/rediectReurn)')); done(new Error('Invalid cookie(path:/rediectReurn)'));
return; return;
} }
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://127.0.0.1:'+testPort+'/redirectReturn');
redirect1.cookies.redirectToDifferentDomain.Path.should.equals('/');
redirect1.cookies.redirectToDifferentDomain.value.should.equals('different1');
done(); done();
}); });
n1.receive({}); n1.receive({});
@ -1559,6 +1574,10 @@ describe('HTTP Request Node', function() {
done(new Error('Invalid cookie(path:/rediectReurn)')); done(new Error('Invalid cookie(path:/rediectReurn)'));
return; return;
} }
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://localhost:'+testPort+'/redirectReturn');
redirect1.cookies.redirectToSameDomainCookie.Path.should.equals('/');
redirect1.cookies.redirectToSameDomainCookie.value.should.equals('same1');
done(); done();
}); });
n1.receive({ n1.receive({
@ -1585,6 +1604,10 @@ describe('HTTP Request Node', function() {
done(new Error('Invalid cookie(path:/rediectReurn)')); done(new Error('Invalid cookie(path:/rediectReurn)'));
return; return;
} }
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://127.0.0.1:'+testPort+'/redirectReturn');
redirect1.cookies.redirectToDifferentDomain.Path.should.equals('/');
redirect1.cookies.redirectToDifferentDomain.value.should.equals('different1');
done(); done();
}); });
n1.receive({ n1.receive({
@ -1613,6 +1636,10 @@ describe('HTTP Request Node', function() {
done(new Error('Invalid cookie(path:/rediectReurn)')); done(new Error('Invalid cookie(path:/rediectReurn)'));
return; return;
} }
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://localhost:'+testPort+'/redirectReturn');
redirect1.cookies.redirectToSameDomainCookie.Path.should.equals('/');
redirect1.cookies.redirectToSameDomainCookie.value.should.equals('same1');
done(); done();
}); });
n1.receive({ n1.receive({
@ -1639,6 +1666,33 @@ describe('HTTP Request Node', function() {
done(new Error('Invalid cookie(path:/rediectReurn)')); done(new Error('Invalid cookie(path:/rediectReurn)'));
return; return;
} }
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://127.0.0.1:'+testPort+'/redirectReturn');
redirect1.cookies.redirectToDifferentDomain.Path.should.equals('/');
redirect1.cookies.redirectToDifferentDomain.value.should.equals('different1');
done();
});
n1.receive({
headers: { cookie: 'requestCookie=request1' }
});
});
});
it('should return all redirect information when redirected multiple times', function(done) {
var flow = [{id:'n1',type:'http request',wires:[['n2']],method:'GET',ret:'obj',url:getTestURL('/redirectMultipleTimes')},
{id:"n2", type:"helper"}];
receivedCookies = {};
helper.load(httpRequestNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
var redirect1 = msg.redirectList[0];
redirect1.location.should.equal('http://localhost:'+testPort+'/redirectToDifferentDomain');
redirect1.cookies.redirectMultipleTimes.Path.should.equals('/');
redirect1.cookies.redirectMultipleTimes.value.should.equals('multiple1');
var redirect2 = msg.redirectList[1];
redirect2.location.should.equal('http://127.0.0.1:'+testPort+'/redirectReturn');
redirect2.cookies.redirectToDifferentDomain.Path.should.equals('/');
redirect2.cookies.redirectToDifferentDomain.value.should.equals('different1');
done(); done();
}); });
n1.receive({ n1.receive({