From 7287514e4a90ceef61e74e18105af0a0c8d2831c Mon Sep 17 00:00:00 2001 From: Topi Vienonen Date: Tue, 23 Nov 2021 14:20:31 +0200 Subject: [PATCH] feat: allows resolving relative public urls into absolute urls --- docs/usage.rst | 6 +++++ src/utils.js | 11 +++++++- test/utils.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/utils.js diff --git a/docs/usage.rst b/docs/usage.rst index 9d74b86..d2bae45 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -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 ====== diff --git a/src/utils.js b/src/utils.js index 6037e20..c028f08 100644 --- a/src/utils.js +++ b/src/utils.js @@ -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) => { diff --git a/test/utils.js b/test/utils.js new file mode 100644 index 0000000..bc82564 --- /dev/null +++ b/test/utils.js @@ -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); + }); + }); +}) \ No newline at end of file