From bb7d9ea97cc614a04dd45f342a4caeeb7dcb23fe Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 3 Jun 2020 15:55:39 +0200 Subject: [PATCH] update from master --- library/WebLibraryServer.java | 13 ++++++++++--- supported/BasicSupport.java | 3 +++ supported/Text.java | 28 +++++++++++++++++++--------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/library/WebLibraryServer.java b/library/WebLibraryServer.java index 1fce3911..6260f6a5 100644 --- a/library/WebLibraryServer.java +++ b/library/WebLibraryServer.java @@ -4,7 +4,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; -import java.net.URLDecoder; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -388,7 +387,11 @@ public class WebLibraryServer extends WebLibraryServerHtml { protected Response getCover(String uri, WLoginResult login) throws IOException { String[] uriParts = uri.split("/"); + int startAt = 0; int off = 2; // "" and "cover" + for (int i = 0; i < off; i++) { + startAt += uriParts[i].length() + "/".length(); + } if (uriParts.length < off + 2) { return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST, @@ -396,7 +399,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { } String type = uriParts[off + 0]; - String id = URLDecoder.decode(uriParts[off + 1], "UTF-8"); + String id = uri.substring(startAt + type.length() + "/".length()); InputStream in = null; @@ -430,6 +433,10 @@ public class WebLibraryServer extends WebLibraryServerHtml { throws IOException { String[] uriParts = uri.split("/"); int off = 2; // "" and "cover" + int startAt = 0; + for (int i = 0; i < off; i++) { + startAt += uriParts[i].length() + "/".length(); + } if (uriParts.length < off + 2) { return NanoHTTPD.newFixedLengthResponse(Status.BAD_REQUEST, @@ -447,7 +454,7 @@ public class WebLibraryServer extends WebLibraryServerHtml { } String type = uriParts[off + 0]; - String id = URLDecoder.decode(uriParts[off + 1], "UTF-8"); + String id = uri.substring(startAt + type.length() + "/".length()); if ("source".equals(type)) { sourceCover(id, login, luid); diff --git a/supported/BasicSupport.java b/supported/BasicSupport.java index ff9ecaf9..9b56f885 100644 --- a/supported/BasicSupport.java +++ b/supported/BasicSupport.java @@ -354,6 +354,9 @@ public abstract class BasicSupport { Scanner scan = new Scanner(in); scan.useDelimiter("\0"); try { + if(!scan.hasNext()) + throw new IOException("The given input was empty"); + return new JSONObject(scan.next()); } catch (JSONException e) { throw new IOException(e); diff --git a/supported/Text.java b/supported/Text.java index 45d970a6..45b761a1 100644 --- a/supported/Text.java +++ b/supported/Text.java @@ -100,10 +100,12 @@ class Text extends BasicSupport { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - scan.next(); // Title - scan.next(); // Author (Date) - String chapter0 = scan.next(); // empty or Chapter 0 - while (chapter0.isEmpty()) { + if (scan.hasNext()) + scan.next(); // Title + if (scan.hasNext()) + scan.next(); // Author (Date) + String chapter0 = ""; + while (scan.hasNext() && chapter0.isEmpty()) { chapter0 = scan.next(); } @@ -126,15 +128,20 @@ class Text extends BasicSupport { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - return scan.next(); + if (scan.hasNext()) + return scan.next(); + return ""; } private String getAuthor() { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - scan.next(); - String authorDate = scan.next(); + if (scan.hasNext()) + scan.next(); + String authorDate = ""; + if (scan.hasNext()) + authorDate = scan.next(); String author = authorDate; int pos = authorDate.indexOf('('); @@ -149,8 +156,11 @@ class Text extends BasicSupport { @SuppressWarnings("resource") // cannot close, or we loose getInput()! Scanner scan = new Scanner(getInput(), "UTF-8"); scan.useDelimiter("\\n"); - scan.next(); - String authorDate = scan.next(); + if (scan.hasNext()) + scan.next(); + String authorDate = ""; + if (scan.hasNext()) + authorDate = scan.next(); String date = ""; int pos = authorDate.indexOf('('); -- 2.27.0