Skip to content

Commit

Permalink
ui: ensures resources loaded by relative URL when baseUrl property set (
Browse files Browse the repository at this point in the history
#3746)

Signed-off-by: Adrian Cole <adrian@tetrate.io>
  • Loading branch information
codefromthecrypt authored Mar 5, 2024
1 parent 85145c2 commit 7352fb1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ static String maybeResource(String basePath, Resource resource) throws IOExcepti
String content = StreamUtils.copyToString(stream, UTF_8);
if (DEFAULT_BASEPATH.equals(basePath)) return content;

// Rebase any href or src in index.html that starts with DEFAULT_BASEPATH
String baseTagValue = "/".equals(basePath) ? "" : basePath;
return content.replaceAll("=\"" + DEFAULT_BASEPATH, "=\"" + baseTagValue);
// relativize any href or src in index.html
// TODO: see if vite config can make these relative by default!
return content.replace("=\"" + DEFAULT_BASEPATH, "=\".")
// set the base href, used in js, to absolute
.replace("<base href=\".", "<base href=\"" + basePath);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import com.linecorp.armeria.server.Server;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.stream.Stream;
import java.util.List;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
Expand Down Expand Up @@ -83,7 +83,7 @@ class ITZipkinUiConfiguration {

client = new OkHttpClient.Builder().followRedirects(true).build();

Stream.of("/zipkin", "/").forEach(path -> {
List.of("/zipkin", "/").forEach(path -> {
try {
assertThat(get(path).body().string()).isEqualTo(index);
} catch (IOException e) {
Expand All @@ -94,7 +94,7 @@ class ITZipkinUiConfiguration {

/** Browsers honor conditional requests such as eTag. Let's make sure the server does */
@Test void conditionalRequests() {
Stream.of("/zipkin/config.json", "/zipkin/index.html", "/zipkin/test.txt").forEach(path -> {
List.of("/zipkin/config.json", "/zipkin/index.html", "/zipkin/test.txt").forEach(path -> {
try {
String etag = get(path).header("etag");
assertThat(conditionalGet(path, etag).code())
Expand Down Expand Up @@ -124,10 +124,10 @@ class ITZipkinUiConfiguration {
<!-- simplified version of /zipkin-lens/index.html -->
<html>
<head>
<base href="/foozipkin/">
<link rel="icon" href="/foozipkin/favicon.ico">
<script type="module" crossorigin="" src="/foozipkin/static/js/index.js"></script>
<link rel="stylesheet" href="/foozipkin/static/css/index.css">
<base href="/foozipkin">
<link rel="icon" href="./favicon.ico">
<script type="module" crossorigin="" src="./static/js/index.js"></script>
<link rel="stylesheet" href="./static/css/index.css">
</head>
<body>zipkin-lens</body>
</html>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ class ZipkinUiConfigurationTest {
<!-- simplified version of /zipkin-lens/index.html -->
<html>
<head>
<base href="/foo/bar/">
<link rel="icon" href="/foo/bar/favicon.ico">
<script type="module" crossorigin="" src="/foo/bar/static/js/index.js"></script>
<link rel="stylesheet" href="/foo/bar/static/css/index.css">
<base href="/foo/bar">
<link rel="icon" href="./favicon.ico">
<script type="module" crossorigin="" src="./static/js/index.js"></script>
<link rel="stylesheet" href="./static/css/index.css">
</head>
<body>zipkin-lens</body>
</html>
Expand All @@ -175,17 +175,17 @@ class ZipkinUiConfigurationTest {
.contains("zipkin-lens");
}

@Test void canOverrideProperty_specialCaseRoot() {
@Test void canOverrideProperty_root() {
context = createContextWithOverridenProperty("zipkin.ui.basepath:/");

assertThat(serveIndex().contentUtf8()).isEqualTo("""
<!-- simplified version of /zipkin-lens/index.html -->
<html>
<head>
<base href="/">
<link rel="icon" href="/favicon.ico">
<script type="module" crossorigin="" src="/static/js/index.js"></script>
<link rel="stylesheet" href="/static/css/index.css">
<link rel="icon" href="./favicon.ico">
<script type="module" crossorigin="" src="./static/js/index.js"></script>
<link rel="stylesheet" href="./static/css/index.css">
</head>
<body>zipkin-lens</body>
</html>
Expand Down
2 changes: 1 addition & 1 deletion zipkin-server/src/test/resources/zipkin-lens/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- simplified version of /zipkin-lens/index.html -->
<html>
<head>
<base href="/zipkin/">
<base href="/zipkin">
<link rel="icon" href="/zipkin/favicon.ico">
<script type="module" crossorigin="" src="/zipkin/static/js/index.js"></script>
<link rel="stylesheet" href="/zipkin/static/css/index.css">
Expand Down

0 comments on commit 7352fb1

Please sign in to comment.