Skip to content
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

feat: allows resolving relative public urls into absolute urls #558

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
6 changes: 6 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ Getting started
-s, --silent Less verbose output
-v, --version Version info

Resolve relative public url into an absolute url
=====

- Define environment variable TILESERVER_GL_RESOLVE_RELATIVE_PUBLIC_URL=true
- Using this configuration will cause relative resource urls to be converted to absolute urls. This is needed for example to use tileserver-gl with mapbox-gl.
- The absolute url will be resolved from the request protocol and host header and prepending them to the public_url option.

Default preview style and configuration
======
Expand Down
11 changes: 10 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ const clone = require('clone');
const glyphCompose = require('@mapbox/glyph-pbf-composite');


module.exports.getPublicUrl = (publicUrl, req) => publicUrl || `${req.protocol}://${req.headers.host}/`;
module.exports.getPublicUrl = (publicUrl, req) => {
if (publicUrl) {
const isRelative = !publicUrl.startsWith('http');
if (isRelative && process.env.TILESERVER_GL_RESOLVE_RELATIVE_PUBLIC_URL === 'true') {
return `${req.protocol}://${req.headers.host}${publicUrl}`;
}
return publicUrl;
}
return `${req.protocol}://${req.headers.host}/`;
}

module.exports.getTileUrls = (req, domains, path, format, publicUrl, aliases) => {

Expand Down
68 changes: 68 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const { getPublicUrl } = require('../src/utils');
const should = require('should');

describe('Utils', () => {
describe('getPublicUrl', () => {
it('No public url provided returns request protocol and host', () => {
const req = {
protocol: 'http',
headers: {
host: 'example.com'
}
};
const expected = 'http://example.com/';

const value = getPublicUrl(undefined, req);
should.equal(value, expected);
});
it('Absolute public url returns just the public url as it is', () => {
const req = {
protocol: 'http',
headers: {
host: 'example.com'
}
};
const expected = 'http://as1.example.com/test/';
const publicUrl = 'http://as1.example.com/test/';

const value = getPublicUrl(publicUrl, req);

should.equal(value, expected);
});
it('Relative public url returns public url as an absolute url', () => {
try {
const req = {
protocol: 'http',
headers: {
host: 'example.com'
}
};
const expected = 'http://example.com/test/';

const publicUrl = '/test/';
process.env.TILESERVER_GL_RESOLVE_RELATIVE_PUBLIC_URL = 'true';

const value = getPublicUrl(publicUrl, req);

should.equal(value, expected);
} finally {
process.env.TILESERVER_GL_RESOLVE_RELATIVE_PUBLIC_URL = undefined;
}
});
it('Relative public url returns public url as an relative url', () => {
const req = {
protocol: 'http',
headers: {
host: 'example.com'
}
};
const expected = '/test/';

const publicUrl = '/test/';

const value = getPublicUrl(publicUrl, req);

should.equal(value, expected);
});
});
})