Skip to content

Add redirect filter for domain redirects #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config.fullstack.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ spec_root: &spec_root
title: "The RESTBase root"
# Some more general RESTBase info
x-request-filters:
- path: lib/redirect_filter.js
options:
ar.wikipedia.beta.wmflabs.org: en.wikipedia.beta.wmflabs.org
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would probably be nice to wrap this into a 'domain_redirects' property - just in case we ever want to add some other type of redirects.

- path: lib/security_response_header_filter.js

x-sub-request-filters:
Expand Down
20 changes: 20 additions & 0 deletions lib/redirect_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

const redirectLocation = (redirectTarget, uri) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ain't gonna work... The problem is that we will receive the uri as internal to the cluster, e.g. http://restbase.svc.eqiad.wmnet/en.wikipedia.org etc.

So your location will also become internal, and the client that receives this redirect will have no idea what to do.

So we need to generate absolute redirect for external requests, and relative redirects for internal requests. normalize_title_filter.js L73-96 does this properly. Now that we have a need for to generate absolute redirects in 2 places, you can extract some code from normalizeTitleFilter into mwutil and use it here too. not sure what the API should be, something like mwUtil.createAbsoluteRedirect

const uriArray = uri.split('/');
uriArray[1] = redirectTarget;
return uriArray.join('/');
};

module.exports = (hyper, req, next, options) => {
const redirectSource = req.params.domain || `${req.uri}`.split('/')[1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no domain, don't do anything.

if (redirectSource in options) {
return {
status: 301,
headers: {
location: redirectLocation(options[redirectSource], `${req.uri}`)
}
};
}
return next(hyper, req);
};
17 changes: 17 additions & 0 deletions test/features/redirect_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const assert = require('../utils/assert.js');
const preq = require('preq');
const Server = require('../utils/server.js');

describe('redirect filter', () => {
const server = new Server();
before(() => server.start());
after(() => server.stop());

it('should redirect the request to the set redirect target', () => {
return preq.get({ uri: `${server.config.baseURL('ar.wikipedia.beta.wmflabs.org')}/page/html/Main_Page` })
.then((res) => {
assert.deepEqual(res.status, 200);
assert.checkString(res.headers['content-location'], /en.wikipedia.beta.wmflabs.org/);
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neet a test for internal and external requests.

});