From c51dfa3ae895f98d93a8480221f47d2f0818a6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Endre=C3=9F?= Date: Sun, 28 Nov 2021 18:13:46 +0100 Subject: [PATCH] Fixed Net and Json lib errors --- .../syntaxapi/net/AsyncSocketServer.java | 6 ++ .../syntaxapi/net/SocketServer.java | 9 ++- .../syntaxapi/net/http/HttpSender.java | 8 +- .../syntaxapi/net/http/HttpServer.java | 75 ++++++++++++------- .../syntaxapi/utils/net/ValueIdentifier.java | 3 - .../syntaxapi/json/io/JsonReader.java | 2 +- .../syntaxapi/utils/key/NamespacedKey.java | 2 + 7 files changed, 69 insertions(+), 36 deletions(-) diff --git a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/AsyncSocketServer.java b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/AsyncSocketServer.java index 17bde6d..08d242f 100644 --- a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/AsyncSocketServer.java +++ b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/AsyncSocketServer.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.InetAddress; import java.net.Socket; +import java.net.SocketException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; @@ -93,6 +94,11 @@ protected void handleClient(Socket socket) throws Throwable { } protected void handleExceptionAsync(Throwable throwable) { + if(throwable instanceof SocketException) { + if(throwable.getMessage().contains("Socket closed")) { + return; // Ignore close exception + } + } throwable.printStackTrace(); } diff --git a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/SocketServer.java b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/SocketServer.java index 7a44125..ceff567 100644 --- a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/SocketServer.java +++ b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/SocketServer.java @@ -4,6 +4,7 @@ import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.SocketException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; @@ -98,8 +99,9 @@ public void stop() throws IOException { return; } - serverSocket.close(); + ServerSocket socket = serverSocket; serverSocket = null; + socket.close(); } public boolean isStarted() { @@ -111,6 +113,11 @@ public boolean isStarted() { */ protected void handleException(Throwable throwable) { + if(throwable instanceof SocketException) { + if(throwable.getMessage().contains("Socket closed")) { + return; // Ignore close exception + } + } throwable.printStackTrace(); } diff --git a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpSender.java b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpSender.java index d293b01..71a9ae6 100644 --- a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpSender.java +++ b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpSender.java @@ -1,14 +1,14 @@ package com.syntaxphoenix.syntaxapi.net.http; -import java.io.BufferedReader; +import java.io.InputStream; import java.net.Socket; public class HttpSender { private final Socket client; - private final BufferedReader input; + private final InputStream input; - public HttpSender(Socket client, BufferedReader input) { + public HttpSender(Socket client, InputStream input) { this.client = client; this.input = input; } @@ -17,7 +17,7 @@ public Socket getClient() { return client; } - public BufferedReader getInput() { + public InputStream getInput() { return input; } diff --git a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpServer.java b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpServer.java index 0e3fb1d..3062ea6 100644 --- a/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpServer.java +++ b/net/src/main/java/com/syntaxphoenix/syntaxapi/net/http/HttpServer.java @@ -1,9 +1,9 @@ package com.syntaxphoenix.syntaxapi.net.http; -import java.io.BufferedReader; +import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.PrintStream; import java.net.InetAddress; import java.net.Socket; @@ -151,10 +151,9 @@ protected void handleClientAsync(Socket socket) throws Throwable { HttpWriter writer = new HttpWriter(new PrintStream(socket.getOutputStream())); - InputStream stream = socket.getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); + InputStream stream = new BufferedInputStream(socket.getInputStream()); - String line = reader.readLine(); + String line = readLine(stream); if (line == null) { new NamedAnswer(StandardNamedType.PLAIN).code(ResponseCode.NO_CONTENT).write(writer); @@ -163,6 +162,11 @@ protected void handleClientAsync(Socket socket) throws Throwable { } String[] info = line.split(" "); + if (info.length < 2) { + new NamedAnswer(StandardNamedType.PLAIN).code(ResponseCode.NO_CONTENT).write(writer); + socket.close(); + return; + } String[] path = info[1].startsWith("/") ? info[1].substring(1).split("/") : info[1].split("/"); String[] parameters = null; @@ -177,25 +181,25 @@ protected void handleClientAsync(Socket socket) throws Throwable { ReceivedRequest request = new ReceivedRequest(RequestType.fromString(info[0]), path); + if (!supported.isEmpty() && !supported.contains(request.getType())) { + new NamedAnswer(StandardNamedType.PLAIN).setResponse("Unsupported request method!").code(ResponseCode.BAD_REQUEST) + .write(writer); + writer.close(); + socket.close(); + return; + } + if (parameters != null) { request.parseParameters(parameters); } - while ((line = reader.readLine()) != null) { - if (line.isEmpty()) { + while ((line = readLine(stream)) != null) { + if (line.isBlank()) { break; } request.parseHeader(line); } - - if (!supported.isEmpty() && !supported.contains(request.getType())) { - new NamedAnswer(StandardNamedType.PLAIN).setResponse("Unsupported request method!").code(ResponseCode.BAD_REQUEST) - .write(writer); - reader.close(); - writer.close(); - socket.close(); - return; - } + stream.reset(); if (gate != null) { RequestState state = gate.acceptRequest(writer, request); @@ -210,7 +214,6 @@ protected void handleClientAsync(Socket socket) throws Throwable { new NamedAnswer(StandardNamedType.PLAIN).setResponse("Method or contenttype is not supported") .code(ResponseCode.BAD_REQUEST).write(writer); } - reader.close(); writer.close(); socket.close(); return; @@ -238,7 +241,6 @@ protected void handleClientAsync(Socket socket) throws Throwable { if (content.message()) { new NamedAnswer(StandardNamedType.PLAIN).setResponse("No content length given!").code(ResponseCode.LENGTH_REQUIRED) .write(writer); - reader.close(); writer.close(); socket.close(); return; @@ -247,13 +249,15 @@ protected void handleClientAsync(Socket socket) throws Throwable { int length = ((Number) request.getHeader("Content-Length")).intValue(); ByteArrayOutputStream array = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; + + int overflow = stream.available() - length; + if (overflow > 0) { + stream.skip(overflow); + } + int remain = length; - while (remain != 0) { - int amount = remain > 1024 ? 1024 : remain; - stream.read(buffer, 0, amount); - array.write(buffer, 0, amount); - remain -= amount; + while (--remain != 0) { + array.write(stream.read()); } byte[] data = array.toByteArray(); if (serializer != null) { @@ -263,7 +267,6 @@ protected void handleClientAsync(Socket socket) throws Throwable { System.out.println(Exceptions.stackTraceToString(exp)); new NamedAnswer(StandardNamedType.PLAIN).setResponse("Failed to serialize data") .code(ResponseCode.INTERNAL_SERVER_ERROR).write(writer); - reader.close(); writer.close(); socket.close(); return; @@ -275,13 +278,12 @@ protected void handleClientAsync(Socket socket) throws Throwable { RequestExecution execution = null; try { - execution = handleHttpRequest(new HttpSender(socket, reader), writer, request); + execution = handleHttpRequest(new HttpSender(socket, stream), writer, request); } catch (Exception e) { execution = RequestExecution.error(e); } if ((execution == null ? RequestExecution.CLOSE : execution).close()) { - reader.close(); writer.close(); socket.close(); if (execution.hasThrowable()) { @@ -291,6 +293,25 @@ protected void handleClientAsync(Socket socket) throws Throwable { } + private String readLine(InputStream stream) throws IOException { + stream.mark(8192); + int character; + StringBuilder builder = new StringBuilder(); + while ((character = stream.read()) != '\r' && character != -1) { + if (character == '\n') { + continue; + } + if (!Character.isValidCodePoint(character)) { + break; + } + builder.append((char) character); + } + if (stream.available() > 0) { + stream.skip(1); + } + return builder.toString(); + } + protected RequestExecution handleHttpRequest(HttpSender sender, HttpWriter writer, ReceivedRequest request) throws Exception { return RequestExecution.CLOSE; } diff --git a/net/src/main/java/com/syntaxphoenix/syntaxapi/utils/net/ValueIdentifier.java b/net/src/main/java/com/syntaxphoenix/syntaxapi/utils/net/ValueIdentifier.java index 8356337..6f05068 100644 --- a/net/src/main/java/com/syntaxphoenix/syntaxapi/utils/net/ValueIdentifier.java +++ b/net/src/main/java/com/syntaxphoenix/syntaxapi/utils/net/ValueIdentifier.java @@ -10,9 +10,6 @@ public final class ValueIdentifier { private ValueIdentifier() {} public static Object identify(String value) { - if (value.length() == 1) { - return value.charAt(0); - } if (Strings.isBoolean(value)) { return Boolean.valueOf(value); } diff --git a/utils/json/json-lib/src/main/java/com/syntaxphoenix/syntaxapi/json/io/JsonReader.java b/utils/json/json-lib/src/main/java/com/syntaxphoenix/syntaxapi/json/io/JsonReader.java index bfaf9f9..2f5e021 100644 --- a/utils/json/json-lib/src/main/java/com/syntaxphoenix/syntaxapi/json/io/JsonReader.java +++ b/utils/json/json-lib/src/main/java/com/syntaxphoenix/syntaxapi/json/io/JsonReader.java @@ -378,7 +378,7 @@ protected char readEscapeCharacter() throws IOException, JsonSyntaxException, Nu output += (check - 'a' + 10); continue; } - if (check >= 'a' && check <= 'F') { + if (check >= 'A' && check <= 'F') { output += (check - 'A' + 10); continue; } diff --git a/utils/key/src/main/java/com/syntaxphoenix/syntaxapi/utils/key/NamespacedKey.java b/utils/key/src/main/java/com/syntaxphoenix/syntaxapi/utils/key/NamespacedKey.java index 50ca59a..2834f28 100644 --- a/utils/key/src/main/java/com/syntaxphoenix/syntaxapi/utils/key/NamespacedKey.java +++ b/utils/key/src/main/java/com/syntaxphoenix/syntaxapi/utils/key/NamespacedKey.java @@ -68,5 +68,7 @@ public Namespace getNamespace() { public String getKey() { return key; } + + }