fix bugs that end connection randomly

This commit is contained in:
Daniella / Tove 2022-07-06 19:56:18 +02:00
parent 487d1660e2
commit 9959d33290
3 changed files with 23 additions and 5 deletions

View file

@ -31,7 +31,7 @@ public class HTTPRequestReader {
i = stream.read();
} catch (SocketException e) {
if(e.getMessage().equals("Connection reset"))
throw new Stop();
throw new Stop("Connection closed by client");
throw e;
}
if(i == -1) {
@ -46,11 +46,21 @@ public class HTTPRequestReader {
i = stream.read(bytes);
} catch (SocketException e) {
if(e.getMessage().equals("Connection reset"))
throw new Stop();
throw new Stop("Connection closed by client");
throw e;
}
while(i != bytes.length) {
try {
int n = stream.read(bytes, i, bytes.length - i);
if(n == 0) {
throw new Stop("0 bytes read");
}
i += n;
} catch (SocketException e) {
if(e.getMessage().equals("Connection reset"))
throw new Stop("Connection closed by client");
throw e;
}
if(i != bytes.length) {
throw new Stop();
}
return i;
}

View file

@ -56,7 +56,7 @@ public class Server {
throw stop;
} catch(Throwable e) {
if(e instanceof Nothing)
return;
throw new Stop("Nothing to do");
else {
e.printStackTrace();
break;
@ -93,6 +93,12 @@ public class Server {
break;
}
} catch (Stop stop) {
if(stop.getMessage() != null) {
System.out.println("Connection stopped: " + stop.getMessage());
}
else {
System.out.println("Connection stopped");
}
}
try {
socket.close();

View file

@ -1,4 +1,6 @@
package de.tudbut.tryumph.server.http;
public class Stop extends RuntimeException {
public Stop() {}
public Stop(String reason) { super(reason); }
}