From 002972e9de731678035d56304d75a6d9e8233635 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Fri, 15 May 2020 13:03:20 +0200 Subject: [PATCH] update from master --- Main.java | 36 ++++++++++++++++++++++---- library/LocalLibrary.java | 16 ++++++++++-- library/WebLibrary.java | 15 ++++++++++- library/WebLibraryServer.java | 24 +++++++++++++---- library/WebLibraryServerHtml.java | 5 ++-- library/WebLibraryUrls.java | 15 +++++++---- searchable/Fanfiction.java | 8 +++++- searchable/MangaLel.java | 8 +++++- supported/BasicSupportHelper.java | 8 +++++- supported/BasicSupportImages.java | 8 +++++- supported/BasicSupport_Deprecated.java | 8 +++++- supported/Cbz.java | 8 +++++- supported/Epub.java | 8 +++++- supported/FimfictionApi.java | 8 +++++- supported/MangaLel.java | 12 ++++++--- 15 files changed, 156 insertions(+), 31 deletions(-) diff --git a/Main.java b/Main.java index e48f4f5..3536544 100644 --- a/Main.java +++ b/Main.java @@ -643,10 +643,36 @@ public class Main { break; case STOP_SERVER: // Can be given via "--remote XX XX XX" - if (key == null) - key = Instance.getInstance().getConfig().getString(Config.SERVER_KEY); - if (port == null) + if (key == null) { + key = Instance.getInstance().getConfig() + .getString(Config.SERVER_KEY); + + // If a subkey in RW mode exists, use it + for (String subkey : Instance.getInstance().getConfig() + .getList(Config.SERVER_ALLOWED_SUBKEYS, + new ArrayList())) { + if ((subkey + "|").contains("|rw|")) { + key = key + "|" + subkey; + break; + } + } + } + + if (port == null) { port = Instance.getInstance().getConfig().getInteger(Config.SERVER_PORT); + } + + if (host == null) { + String mode = Instance.getInstance().getConfig() + .getString(Config.SERVER_MODE, "fanfix"); + if ("http".equals(mode)) { + host = "http://localhost"; + } else if ("https".equals(mode)) { + host = "https://localhost"; + } else if ("fanfix".equals(mode)) { + host = "fanfix://localhost"; + } + } if (port == null) { System.err.println("No port given nor configured in the config file"); @@ -1069,7 +1095,7 @@ public class Main { * @param key * the key to contact the Fanfix server * @param host - * the host on which it runs (NULL means localhost) + * the host on which it runs * @param port * the port on which it runs * @@ -1078,7 +1104,7 @@ public class Main { * @throws SSLException * when the key was not accepted */ - private void stopServer(String key, String host, Integer port) + private void stopServer(String key, String host, int port) throws IOException, SSLException { if (host.startsWith("http://") || host.startsWith("https://")) { new WebLibrary(key, host, port).stop(); diff --git a/library/LocalLibrary.java b/library/LocalLibrary.java index 7220a39..f655d4d 100644 --- a/library/LocalLibrary.java +++ b/library/LocalLibrary.java @@ -254,7 +254,13 @@ public class LocalLibrary extends BasicLibrary { in = new FileInputStream(cover); try { synchronized (lock) { - sourceCovers.put(source, new Image(in)); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + sourceCovers.put(source, img); } } finally { in.close(); @@ -298,7 +304,13 @@ public class LocalLibrary extends BasicLibrary { in = new FileInputStream(cover); try { synchronized (lock) { - authorCovers.put(author, new Image(in)); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + authorCovers.put(author, img); } } finally { in.close(); diff --git a/library/WebLibrary.java b/library/WebLibrary.java index 7566877..9af38e2 100644 --- a/library/WebLibrary.java +++ b/library/WebLibrary.java @@ -133,7 +133,17 @@ public class WebLibrary extends BasicLibrary { * in case of I/O errors */ public void stop() throws IOException { - post(WebLibraryUrls.EXIT_URL, null).close(); + try { + post(WebLibraryUrls.EXIT_URL, null).close(); + } catch (Exception e) { + try { + Thread.sleep(200); + } catch (InterruptedException e1) { + } + if (getStatus() != Status.UNAVAILABLE) { + throw new IOException("Cannot exit the library", e); + } + } } @Override @@ -164,6 +174,7 @@ public class WebLibrary extends BasicLibrary { try { Image img = new Image(in); if (img.getSize() > 0) { + img.close(); return img; } @@ -179,6 +190,7 @@ public class WebLibrary extends BasicLibrary { try { Image img = new Image(in); if (img.getSize() > 0) { + img.close(); return img; } @@ -194,6 +206,7 @@ public class WebLibrary extends BasicLibrary { try { Image img = new Image(in); if (img.getSize() > 0) { + img.close(); return img; } diff --git a/library/WebLibraryServer.java b/library/WebLibraryServer.java index 50dc3d9..22f36e9 100644 --- a/library/WebLibraryServer.java +++ b/library/WebLibraryServer.java @@ -110,7 +110,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, NanoHTTPD.MIME_PLAINTEXT, "Server is already exiting..."); } - + exiting = true; Instance.getInstance().getTraceHandler().trace("Exiting"); @@ -131,6 +131,20 @@ public class WebLibraryServer extends WebLibraryServerHtml { doStop(); + new Thread(new Runnable() { + @Override + public void run() { + try { + Thread.sleep(1500); + } catch (InterruptedException e) { + } + + Instance.getInstance().getTraceHandler() + .trace("Exit timeout: force-quit"); + System.exit(0); + } + }, "Exit program after timeout of 1500 ms").start(); + return NanoHTTPD.newFixedLengthResponse(Status.OK, NanoHTTPD.MIME_PLAINTEXT, "Exited"); } @@ -341,7 +355,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "SET story part not allowed"); } - + if (exiting) { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); @@ -426,7 +440,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Cover request not allowed"); } - + if (exiting) { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); @@ -457,7 +471,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Import not allowed"); } - + if (exiting) { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); @@ -529,7 +543,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { return NanoHTTPD.newFixedLengthResponse(Status.FORBIDDEN, NanoHTTPD.MIME_PLAINTEXT, "Delete not allowed"); } - + if (exiting) { return NanoHTTPD.newFixedLengthResponse(Status.SERVICE_UNAVAILABLE, NanoHTTPD.MIME_PLAINTEXT, "Server is exiting..."); diff --git a/library/WebLibraryServerHtml.java b/library/WebLibraryServerHtml.java index 7351d0d..8895fb8 100644 --- a/library/WebLibraryServerHtml.java +++ b/library/WebLibraryServerHtml.java @@ -173,13 +173,14 @@ abstract class WebLibraryServerHtml implements Runnable { } Response rep = null; - if (!login.isSuccess() && WebLibraryUrls.isSupportedUrl(uri)) { + if (!login.isSuccess() + && WebLibraryUrls.isSupportedUrl(uri, true)) { rep = loginPage(login, uri); } if (rep == null) { try { - if (WebLibraryUrls.isSupportedUrl(uri)) { + if (WebLibraryUrls.isSupportedUrl(uri, false)) { if (WebLibraryUrls.INDEX_URL.equals(uri)) { rep = root(session, cookies, login); } else if (WebLibraryUrls.VERSION_URL.equals(uri)) { diff --git a/library/WebLibraryUrls.java b/library/WebLibraryUrls.java index c02fc38..2b75779 100644 --- a/library/WebLibraryUrls.java +++ b/library/WebLibraryUrls.java @@ -103,11 +103,16 @@ class WebLibraryUrls { .replace("{luid}", luid); } - static public boolean isSupportedUrl(String url) { - return INDEX_URL.equals(url) || VERSION_URL.equals(url) - || LOGOUT_URL.equals(url) || EXIT_URL.equals(url) - || isViewUrl(url) || isStoryUrl(url) || isListUrl(url) - || isCoverUrl(url) || isImprtUrl(url) || isDeleteUrl(url); + static public boolean isSupportedUrl(String url, + boolean requiresLoginOnly) { + if (requiresLoginOnly) { + return INDEX_URL.equals(url) || LOGOUT_URL.equals(url) + || EXIT_URL.equals(url) || isViewUrl(url) || isStoryUrl(url) + || isListUrl(url) || isCoverUrl(url) || isImprtUrl(url) + || isDeleteUrl(url); + } + + return isSupportedUrl(url, true) || VERSION_URL.equals(url); } static public String getCoverUrlStory(String luid) { diff --git a/searchable/Fanfiction.java b/searchable/Fanfiction.java index e2fba1f..cbc927b 100644 --- a/searchable/Fanfiction.java +++ b/searchable/Fanfiction.java @@ -287,7 +287,13 @@ class Fanfiction extends BasicSearchable { try { InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), true); try { - meta.setCover(new Image(in)); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + meta.setCover(img); } finally { in.close(); } diff --git a/searchable/MangaLel.java b/searchable/MangaLel.java index 5ba21a0..354edb4 100644 --- a/searchable/MangaLel.java +++ b/searchable/MangaLel.java @@ -140,7 +140,13 @@ class MangaLel extends BasicSearchable { InputStream in = Instance.getInstance().getCache().open(new URL(coverUrl), getSupport(), true); try { - meta.setCover(new Image(in)); + Image ii = new Image(in); + if (ii.getSize() == 0) { + ii.close(); + throw new IOException( + "Empty image not accepted"); + } + meta.setCover(ii); } finally { in.close(); } diff --git a/supported/BasicSupportHelper.java b/supported/BasicSupportHelper.java index 7768052..f3c30bc 100644 --- a/supported/BasicSupportHelper.java +++ b/supported/BasicSupportHelper.java @@ -85,7 +85,13 @@ public class BasicSupportHelper { InputStream in = null; try { in = Instance.getInstance().getCache().open(url, support, true); - return new Image(in); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + return img; } catch (IOException e) { } finally { if (in != null) { diff --git a/supported/BasicSupportImages.java b/supported/BasicSupportImages.java index 576cb17..f56b50c 100644 --- a/supported/BasicSupportImages.java +++ b/supported/BasicSupportImages.java @@ -55,7 +55,13 @@ public class BasicSupportImages { InputStream in = null; try { in = Instance.getInstance().getCache().open(url, support, true); - return new Image(in); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + return img; } catch (IOException e) { } finally { if (in != null) { diff --git a/supported/BasicSupport_Deprecated.java b/supported/BasicSupport_Deprecated.java index a50ee3c..40ff3fc 100644 --- a/supported/BasicSupport_Deprecated.java +++ b/supported/BasicSupport_Deprecated.java @@ -651,7 +651,13 @@ public abstract class BasicSupport_Deprecated extends BasicSupport { InputStream in = null; try { in = Instance.getInstance().getCache().open(url, getSupport(url), true); - return new Image(in); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + return img; } catch (IOException e) { } finally { if (in != null) { diff --git a/supported/Cbz.java b/supported/Cbz.java index 7fe496d..a5391d0 100644 --- a/supported/Cbz.java +++ b/supported/Cbz.java @@ -97,7 +97,13 @@ class Cbz extends Epub { if (imageEntry) { String uuid = meta.getUuid() + "_" + entry.getName(); try { - images.put(uuid, new Image(zipIn)); + Image img = new Image(zipIn); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + images.put(uuid, img); } catch (Exception e) { Instance.getInstance().getTraceHandler().error(e); } diff --git a/supported/Epub.java b/supported/Epub.java index 965a27a..783d724 100644 --- a/supported/Epub.java +++ b/supported/Epub.java @@ -128,7 +128,13 @@ class Epub extends InfoText { // Cover if (getCover() && cover == null) { try { - cover = new Image(zipIn); + Image img = new Image(zipIn); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + cover = img; } catch (Exception e) { Instance.getInstance().getTraceHandler() .error(e); diff --git a/supported/FimfictionApi.java b/supported/FimfictionApi.java index 43d01d1..e6dd611 100644 --- a/supported/FimfictionApi.java +++ b/supported/FimfictionApi.java @@ -147,7 +147,13 @@ class FimfictionApi extends BasicSupport { try { InputStream in = Instance.getInstance().getCache().open(coverImageUrl, null, true); try { - meta.setCover(new Image(in)); + Image img = new Image(in); + if (img.getSize() == 0) { + img.close(); + throw new IOException( + "Empty image not accepted"); + } + meta.setCover(img); } finally { in.close(); } diff --git a/supported/MangaLel.java b/supported/MangaLel.java index de0b871..5910a37 100644 --- a/supported/MangaLel.java +++ b/supported/MangaLel.java @@ -138,11 +138,17 @@ class MangaLel extends BasicSupport { if (img != null) { String coverUrl = img.absUrl("src"); - InputStream coverIn; try { - coverIn = Instance.getInstance().getCache().open(new URL(coverUrl), this, true); + InputStream coverIn = Instance.getInstance().getCache() + .open(new URL(coverUrl), this, true); try { - return new Image(coverIn); + Image ii = new Image(coverIn); + if (ii.getSize() == 0) { + ii.close(); + throw new IOException("Empty image not accepted"); + } + + return ii; } finally { coverIn.close(); } -- 2.27.0