Skip to content

Commit

Permalink
Fixed Net and Json lib errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Nov 28, 2021
1 parent 2dfa93a commit c51dfa3
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,8 +99,9 @@ public void stop() throws IOException {
return;
}

serverSocket.close();
ServerSocket socket = serverSocket;
serverSocket = null;
socket.close();
}

public boolean isStarted() {
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -17,7 +17,7 @@ public Socket getClient() {
return client;
}

public BufferedReader getInput() {
public InputStream getInput() {
return input;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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()) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ public Namespace getNamespace() {
public String getKey() {
return key;
}



}

0 comments on commit c51dfa3

Please sign in to comment.