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

ui: ensures resources loaded by relative URL when ZIPKIN_UI_BASEPATH set #3746

Merged
merged 1 commit into from
Mar 5, 2024
Merged
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
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/">
Copy link
Member Author

Choose a reason for hiding this comment

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

the actual source has no trailing slash

<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
Loading