Skip to content

Commit

Permalink
Fixes Jeager UI broken when accessing via IPv6 address (#491)
Browse files Browse the repository at this point in the history
Signed-off-by: Mahesh PAI R <mahesh.pai_r@nokia.com>
  • Loading branch information
Mahesh PAI R committed Dec 6, 2019
1 parent 0c175e1 commit f0b5dda
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
24 changes: 23 additions & 1 deletion packages/jaeger-ui/src/utils/prefix-url.test.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

jest.mock('../site-prefix', () => `${global.location.origin}/a/site/prefix/`);

import prefixUrl from './prefix-url';
import prefixUrl, { getPathPrefix } from './prefix-url';

const PATH_PREFIX = '/a/site/prefix';

Expand All @@ -37,3 +37,25 @@ describe('prefixUrl()', () => {
});
});
});

describe('getPathPrefix()', () => {
const tests = [
{ address: '2a00:8a00:4000:751e::1c:8443' },
{ address: '[2a00:8a00:4000:751e::1c]:8443' },
{ address: '[2a00:8a00:4000:751e:0000:0000:0000:001c]:8443' },
{ address: '[::]:443' },
{ address: '127.0.0.1' },
{ address: '127.0.0.1:8443' },
{ address: 'jaeger.mydomain.com:8443' },
{ address: 'jaeger.mydomain.com' },
];

tests.forEach(({ address }) => {
it(`getPathPrefix "${address}" correctly`, () => {
const origin = `https://${address}`;
const sitePrefix = `https://${address}/jaeger`;
const target = '/jaeger';
expect(getPathPrefix(origin, sitePrefix)).toBe(target);
});
});
});
22 changes: 20 additions & 2 deletions packages/jaeger-ui/src/utils/prefix-url.tsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@
import sitePrefix from '../site-prefix';

const origin = process.env.NODE_ENV === 'test' ? global.location.origin : window.location.origin;

/**
* Escape any RegEx characters in a given string
*
* @param {string} value The string which needs to be escaped
* @return {string} Escaped RegEx string
*/
function regExEscape(value?: string) {
const s = value == null ? '' : String(value);
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
}

/**
* Generate the URL prefix from `sitePrefix` and use it for all subsequent calls
* to `prefixUrl()`. `sitePrefix` should be an absolute URL, e.g. with an origin.
Expand All @@ -24,8 +36,14 @@ const origin = process.env.NODE_ENV === 'test' ? global.location.origin : window
* - `"http://localhost:3000/abc/"` to `"/abc"`
* - `"http://localhost:3000/abc/def/"` to `"/abc/def"`
*/
const rx = new RegExp(`^${origin}|/$`, 'ig');
const pathPrefix = sitePrefix.replace(rx, '');
export function getPathPrefix(orig?: string, sitePref?: string) {
const o = orig == null ? '' : String(orig);
const s = sitePref == null ? '' : String(sitePref);
const rx = new RegExp(`^${regExEscape(o)}|/$`, 'ig');
return s.replace(rx, '');
}

const pathPrefix = getPathPrefix(origin, sitePrefix);

/**
* Add the path prefix to the URL. See [site-prefix.js](../site-prefix.js) and
Expand Down

0 comments on commit f0b5dda

Please sign in to comment.