Updated to latest Protocol Version
This commit is contained in:
@@ -104,6 +104,108 @@ public final class BrowserTab extends OACPanel {
|
||||
return sw.toString();
|
||||
}
|
||||
|
||||
private static String normalizeMime(String contentType) {
|
||||
String ct = (contentType == null || contentType.isBlank()) ? "application/octet-stream" : contentType.trim();
|
||||
int semi = ct.indexOf(';');
|
||||
String base = (semi >= 0 ? ct.substring(0, semi) : ct).trim();
|
||||
return base.isEmpty() ? "application/octet-stream" : base;
|
||||
}
|
||||
|
||||
private static boolean isHtml(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.equals("text/html") || ct.equals("application/xhtml+xml");
|
||||
}
|
||||
|
||||
private static boolean isText(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.startsWith("text/") || ct.equals("application/json") || ct.equals("application/xml") || ct.endsWith("+json") || ct.endsWith("+xml");
|
||||
}
|
||||
|
||||
private static boolean isImage(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.startsWith("image/");
|
||||
}
|
||||
|
||||
private static boolean isPdf(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.equals("application/pdf");
|
||||
}
|
||||
|
||||
private static Charset charsetFromContentType(String contentType, Charset def) {
|
||||
if (contentType == null) return def;
|
||||
String[] parts = contentType.split(";");
|
||||
for (String p : parts) {
|
||||
String s = p.trim();
|
||||
if (s.toLowerCase(Locale.ROOT).startsWith("charset=")) {
|
||||
String name = s.substring("charset=".length()).trim();
|
||||
try {
|
||||
return Charset.forName(name);
|
||||
} catch (Exception ignored) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
private static String extractFilenameFromContentDisposition(Map<String, String> headers) {
|
||||
if (headers == null || headers.isEmpty()) return null;
|
||||
|
||||
String cd = null;
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
if (e.getKey() != null && e.getKey().equalsIgnoreCase("content-disposition")) {
|
||||
cd = e.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cd == null || cd.isBlank()) return null;
|
||||
|
||||
String lower = cd.toLowerCase(Locale.ROOT);
|
||||
int fn = lower.indexOf("filename=");
|
||||
if (fn < 0) return null;
|
||||
|
||||
String v = cd.substring(fn + "filename=".length()).trim();
|
||||
if (v.startsWith("\"")) {
|
||||
int end = v.indexOf('"', 1);
|
||||
if (end > 1) return v.substring(1, end);
|
||||
return null;
|
||||
}
|
||||
int semi = v.indexOf(';');
|
||||
if (semi >= 0) v = v.substring(0, semi).trim();
|
||||
return v.isBlank() ? null : v;
|
||||
}
|
||||
|
||||
private static String sanitizeFilename(String name) {
|
||||
String s = name.replace('\\', '_').replace('/', '_');
|
||||
s = s.replace("..", "_");
|
||||
s = s.replace(':', '_').replace('*', '_').replace('?', '_').replace('"', '_')
|
||||
.replace('<', '_').replace('>', '_').replace('|', '_');
|
||||
return s.isBlank() ? "download.bin" : s;
|
||||
}
|
||||
|
||||
private static String extensionFromContentType(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
|
||||
if (ct.equals("application/pdf")) return ".pdf";
|
||||
if (ct.equals("application/zip")) return ".zip";
|
||||
if (ct.equals("application/x-7z-compressed")) return ".7z";
|
||||
if (ct.equals("application/x-rar-compressed")) return ".rar";
|
||||
if (ct.equals("application/gzip")) return ".gz";
|
||||
if (ct.equals("application/json")) return ".json";
|
||||
if (ct.equals("application/xml") || ct.endsWith("+xml")) return ".xml";
|
||||
|
||||
if (ct.startsWith("image/")) {
|
||||
int slash = ct.indexOf('/');
|
||||
if (slash > 0 && slash < ct.length() - 1) {
|
||||
String ext = ct.substring(slash + 1).trim();
|
||||
if (!ext.isEmpty()) return "." + ext;
|
||||
}
|
||||
}
|
||||
|
||||
if (ct.startsWith("text/")) return ".txt";
|
||||
return ".bin";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stable tab key.
|
||||
*
|
||||
@@ -234,6 +336,8 @@ public final class BrowserTab extends OACPanel {
|
||||
return getEngineLocation();
|
||||
}
|
||||
|
||||
// -------------------- Stream render/save helpers --------------------
|
||||
|
||||
/**
|
||||
* Returns current engine location.
|
||||
*
|
||||
@@ -470,8 +574,6 @@ public final class BrowserTab extends OACPanel {
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Stream render/save helpers --------------------
|
||||
|
||||
private void showSaveAsDialogAndWriteBytes(String suggestedFilename, String contentType, byte[] data) {
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
Window parent = SwingUtilities.getWindowAncestor(this);
|
||||
@@ -539,108 +641,6 @@ public final class BrowserTab extends OACPanel {
|
||||
});
|
||||
}
|
||||
|
||||
private static String normalizeMime(String contentType) {
|
||||
String ct = (contentType == null || contentType.isBlank()) ? "application/octet-stream" : contentType.trim();
|
||||
int semi = ct.indexOf(';');
|
||||
String base = (semi >= 0 ? ct.substring(0, semi) : ct).trim();
|
||||
return base.isEmpty() ? "application/octet-stream" : base;
|
||||
}
|
||||
|
||||
private static boolean isHtml(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.equals("text/html") || ct.equals("application/xhtml+xml");
|
||||
}
|
||||
|
||||
private static boolean isText(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.startsWith("text/") || ct.equals("application/json") || ct.equals("application/xml") || ct.endsWith("+json") || ct.endsWith("+xml");
|
||||
}
|
||||
|
||||
private static boolean isImage(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.startsWith("image/");
|
||||
}
|
||||
|
||||
private static boolean isPdf(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
return ct.equals("application/pdf");
|
||||
}
|
||||
|
||||
private static Charset charsetFromContentType(String contentType, Charset def) {
|
||||
if (contentType == null) return def;
|
||||
String[] parts = contentType.split(";");
|
||||
for (String p : parts) {
|
||||
String s = p.trim();
|
||||
if (s.toLowerCase(Locale.ROOT).startsWith("charset=")) {
|
||||
String name = s.substring("charset=".length()).trim();
|
||||
try {
|
||||
return Charset.forName(name);
|
||||
} catch (Exception ignored) {
|
||||
return def;
|
||||
}
|
||||
}
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
private static String extractFilenameFromContentDisposition(Map<String, String> headers) {
|
||||
if (headers == null || headers.isEmpty()) return null;
|
||||
|
||||
String cd = null;
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
if (e.getKey() != null && e.getKey().equalsIgnoreCase("content-disposition")) {
|
||||
cd = e.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cd == null || cd.isBlank()) return null;
|
||||
|
||||
String lower = cd.toLowerCase(Locale.ROOT);
|
||||
int fn = lower.indexOf("filename=");
|
||||
if (fn < 0) return null;
|
||||
|
||||
String v = cd.substring(fn + "filename=".length()).trim();
|
||||
if (v.startsWith("\"")) {
|
||||
int end = v.indexOf('"', 1);
|
||||
if (end > 1) return v.substring(1, end);
|
||||
return null;
|
||||
}
|
||||
int semi = v.indexOf(';');
|
||||
if (semi >= 0) v = v.substring(0, semi).trim();
|
||||
return v.isBlank() ? null : v;
|
||||
}
|
||||
|
||||
private static String sanitizeFilename(String name) {
|
||||
String s = name.replace('\\', '_').replace('/', '_');
|
||||
s = s.replace("..", "_");
|
||||
s = s.replace(':', '_').replace('*', '_').replace('?', '_').replace('"', '_')
|
||||
.replace('<', '_').replace('>', '_').replace('|', '_');
|
||||
return s.isBlank() ? "download.bin" : s;
|
||||
}
|
||||
|
||||
private static String extensionFromContentType(String contentType) {
|
||||
String ct = normalizeMime(contentType).toLowerCase(Locale.ROOT);
|
||||
|
||||
if (ct.equals("application/pdf")) return ".pdf";
|
||||
if (ct.equals("application/zip")) return ".zip";
|
||||
if (ct.equals("application/x-7z-compressed")) return ".7z";
|
||||
if (ct.equals("application/x-rar-compressed")) return ".rar";
|
||||
if (ct.equals("application/gzip")) return ".gz";
|
||||
if (ct.equals("application/json")) return ".json";
|
||||
if (ct.equals("application/xml") || ct.endsWith("+xml")) return ".xml";
|
||||
|
||||
if (ct.startsWith("image/")) {
|
||||
int slash = ct.indexOf('/');
|
||||
if (slash > 0 && slash < ct.length() - 1) {
|
||||
String ext = ct.substring(slash + 1).trim();
|
||||
if (!ext.isEmpty()) return "." + ext;
|
||||
}
|
||||
}
|
||||
|
||||
if (ct.startsWith("text/")) return ".txt";
|
||||
return ".bin";
|
||||
}
|
||||
|
||||
public void bindProtocolClient() {
|
||||
protocolClient.getLibImpl().bindTab(this);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user