More qdnBase improvements, to hopefully handle all cases correctly.

This commit is contained in:
CalDescent 2023-04-29 17:13:50 +01:00
parent aed1823afb
commit f044166b81
2 changed files with 13 additions and 7 deletions

View File

@ -13,8 +13,8 @@ public class HTMLParser {
private static final Logger LOGGER = LogManager.getLogger(HTMLParser.class); private static final Logger LOGGER = LogManager.getLogger(HTMLParser.class);
private String linkPrefix;
private String qdnBase; private String qdnBase;
private String qdnBaseWithPath;
private byte[] data; private byte[] data;
private String qdnContext; private String qdnContext;
private String resourceId; private String resourceId;
@ -22,12 +22,13 @@ public class HTMLParser {
private String identifier; private String identifier;
private String path; private String path;
private String theme; private String theme;
private boolean usingCustomRouting;
public HTMLParser(String resourceId, String inPath, String prefix, boolean usePrefix, byte[] data, public HTMLParser(String resourceId, String inPath, String prefix, boolean usePrefix, byte[] data,
String qdnContext, Service service, String identifier, String theme) { String qdnContext, Service service, String identifier, String theme, boolean usingCustomRouting) {
String inPathWithoutFilename = inPath.contains("/") ? inPath.substring(0, inPath.lastIndexOf('/')) : ""; String inPathWithoutFilename = inPath.contains("/") ? inPath.substring(0, inPath.lastIndexOf('/')) : "";
this.linkPrefix = usePrefix ? String.format("%s/%s%s", prefix, resourceId, inPathWithoutFilename) : "";
this.qdnBase = usePrefix ? String.format("%s/%s", prefix, resourceId) : ""; this.qdnBase = usePrefix ? String.format("%s/%s", prefix, resourceId) : "";
this.qdnBaseWithPath = usePrefix ? String.format("%s/%s%s", prefix, resourceId, inPathWithoutFilename) : "";
this.data = data; this.data = data;
this.qdnContext = qdnContext; this.qdnContext = qdnContext;
this.resourceId = resourceId; this.resourceId = resourceId;
@ -35,6 +36,7 @@ public class HTMLParser {
this.identifier = identifier; this.identifier = identifier;
this.path = inPath; this.path = inPath;
this.theme = theme; this.theme = theme;
this.usingCustomRouting = usingCustomRouting;
} }
public void addAdditionalHeaderTags() { public void addAdditionalHeaderTags() {
@ -58,11 +60,13 @@ public class HTMLParser {
String identifier = this.identifier != null ? this.identifier.replace("\"","\\\"") : ""; String identifier = this.identifier != null ? this.identifier.replace("\"","\\\"") : "";
String path = this.path != null ? this.path.replace("\"","\\\"") : ""; String path = this.path != null ? this.path.replace("\"","\\\"") : "";
String theme = this.theme != null ? this.theme.replace("\"","\\\"") : ""; String theme = this.theme != null ? this.theme.replace("\"","\\\"") : "";
String qdnContextVar = String.format("<script>var _qdnContext=\"%s\"; var _qdnTheme=\"%s\"; var _qdnService=\"%s\"; var _qdnName=\"%s\"; var _qdnIdentifier=\"%s\"; var _qdnPath=\"%s\"; var _qdnBase=\"%s\";</script>", this.qdnContext, theme, service, name, identifier, path, this.qdnBase); String qdnContextVar = String.format("<script>var _qdnContext=\"%s\"; var _qdnTheme=\"%s\"; var _qdnService=\"%s\"; var _qdnName=\"%s\"; var _qdnIdentifier=\"%s\"; var _qdnPath=\"%s\"; var _qdnBase=\"%s\"; var _qdnBaseWithPath=\"%s\";</script>", this.qdnContext, theme, service, name, identifier, path, this.qdnBase, this.qdnBaseWithPath);
head.get(0).prepend(qdnContextVar); head.get(0).prepend(qdnContextVar);
// Add base href tag // Add base href tag
String baseElement = String.format("<base href=\"%s/\">", this.linkPrefix); // Exclude the path if this request was routed back to the index automatically
String baseHref = this.usingCustomRouting ? this.qdnBase : this.qdnBaseWithPath;
String baseElement = String.format("<base href=\"%s/\">", baseHref);
head.get(0).prepend(baseElement); head.get(0).prepend(baseElement);
// Add meta charset tag // Add meta charset tag

View File

@ -126,7 +126,8 @@ public class ArbitraryDataRenderer {
try { try {
String filename = this.getFilename(unzippedPath, inPath); String filename = this.getFilename(unzippedPath, inPath);
Path filePath = Paths.get(unzippedPath, filename); Path filePath = Paths.get(unzippedPath, filename);
boolean usingCustomRouting = false;
// If the file doesn't exist, we may need to route the request elsewhere, or cleanup // If the file doesn't exist, we may need to route the request elsewhere, or cleanup
if (!Files.exists(filePath)) { if (!Files.exists(filePath)) {
if (inPath.equals("/")) { if (inPath.equals("/")) {
@ -148,6 +149,7 @@ public class ArbitraryDataRenderer {
// Forward request to index file // Forward request to index file
filePath = indexPath; filePath = indexPath;
filename = indexFile; filename = indexFile;
usingCustomRouting = true;
break; break;
} }
} }
@ -157,7 +159,7 @@ public class ArbitraryDataRenderer {
if (HTMLParser.isHtmlFile(filename)) { if (HTMLParser.isHtmlFile(filename)) {
// HTML file - needs to be parsed // HTML file - needs to be parsed
byte[] data = Files.readAllBytes(filePath); // TODO: limit file size that can be read into memory byte[] data = Files.readAllBytes(filePath); // TODO: limit file size that can be read into memory
HTMLParser htmlParser = new HTMLParser(resourceId, inPath, prefix, usePrefix, data, qdnContext, service, identifier, theme); HTMLParser htmlParser = new HTMLParser(resourceId, inPath, prefix, usePrefix, data, qdnContext, service, identifier, theme, usingCustomRouting);
htmlParser.addAdditionalHeaderTags(); htmlParser.addAdditionalHeaderTags();
response.addHeader("Content-Security-Policy", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; media-src 'self' data: blob:; img-src 'self' data: blob:;"); response.addHeader("Content-Security-Policy", "default-src 'self' 'unsafe-inline' 'unsafe-eval'; media-src 'self' data: blob:; img-src 'self' data: blob:;");
response.setContentType(context.getMimeType(filename)); response.setContentType(context.getMimeType(filename));