From a1ed14692437af7a594ae5110c08369e6ed0447a Mon Sep 17 00:00:00 2001 From: FireMasterK <20838718+FireMasterK@users.noreply.github.com> Date: Wed, 21 Jul 2021 02:33:44 +0530 Subject: [PATCH] Handle invalid requests gracefully. --- src/main/java/me/kavin/piped/utils/ResponseHelper.java | 10 ++++++++++ .../kavin/piped/utils/resp/InvalidRequestResponse.java | 7 +++++++ 2 files changed, 17 insertions(+) create mode 100644 src/main/java/me/kavin/piped/utils/resp/InvalidRequestResponse.java diff --git a/src/main/java/me/kavin/piped/utils/ResponseHelper.java b/src/main/java/me/kavin/piped/utils/ResponseHelper.java index 310f045..b6cc9e3 100644 --- a/src/main/java/me/kavin/piped/utils/ResponseHelper.java +++ b/src/main/java/me/kavin/piped/utils/ResponseHelper.java @@ -84,6 +84,7 @@ import me.kavin.piped.utils.resp.AcceptedResponse; import me.kavin.piped.utils.resp.AlreadyRegisteredResponse; import me.kavin.piped.utils.resp.AuthenticationFailureResponse; import me.kavin.piped.utils.resp.IncorrectCredentialsResponse; +import me.kavin.piped.utils.resp.InvalidRequestResponse; import me.kavin.piped.utils.resp.LoginResponse; import me.kavin.piped.utils.resp.SubscribeStatusResponse; @@ -263,6 +264,9 @@ public class ResponseHelper { public static final byte[] trendingResponse(String region) throws ParsingException, ExtractionException, IOException { + if (region == null) + return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse()); + final List relatedStreams = new ObjectArrayList<>(); KioskList kioskList = Constants.YOUTUBE_SERVICE.getKioskList(); @@ -515,6 +519,9 @@ public class ResponseHelper { public static final byte[] registerResponse(String user, String pass) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { + if (user == null || pass == null) + return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse()); + user = user.toLowerCase(); Session s = DatabaseSessionFactory.createSession(); @@ -544,6 +551,9 @@ public class ResponseHelper { public static final byte[] loginResponse(String user, String pass) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { + if (user == null || pass == null) + return Constants.mapper.writeValueAsBytes(new InvalidRequestResponse()); + user = user.toLowerCase(); Session s = DatabaseSessionFactory.createSession(); diff --git a/src/main/java/me/kavin/piped/utils/resp/InvalidRequestResponse.java b/src/main/java/me/kavin/piped/utils/resp/InvalidRequestResponse.java new file mode 100644 index 0000000..e64852f --- /dev/null +++ b/src/main/java/me/kavin/piped/utils/resp/InvalidRequestResponse.java @@ -0,0 +1,7 @@ +package me.kavin.piped.utils.resp; + +public class InvalidRequestResponse { + + public String message = "Invalid request sent."; + +}